Lab 19: Interface inheritance

In this lab we will practice interface inheritance.

Problem 1

Start with the Shape interface and associated files that we studied in class and do the following:

  1. Add a class named Square that implements Shape. Make all the necessary variables and methods for this class.

  2. Write a main to test your implementation of Square. You may add it in the Square class or add another class UseSquare with a main.

  3. Modify UseShape to add at least one instance of Square to places where an instance of Circle is used.

  4. Write a method in UseShape that sums the areas of all the geometric objects in an array passed in as an argument. The method signature is as follows:
         public static double sumArea(Shape[] a);
       
    You will need to modify the Shape interface and other classes to implement this method.

  5. Write a method in UseShape that creates an array of 7 objects (2 circles, 2 rectangles, and 3 squares) and computes their total area using the sumArea method that you just defined, and prints the result to the console.

Problem 2

Start with the sorting_objects_2/Selection.java in Lecture 19 and do the following:

  1. Create a class named Account with two attributes: name for account owner's name and balance for account balance. This class is not much different than the Account class that we have used before.

  2. Add other necessary constructor(s), getter(s), setter(s), toString, equals, compareTo, etc. See sorting_objects_2/Point.java in Lecture 19 as an example.

  3. Modify the main of Selection to create an array of at least 5 Account objects this time instead of an array of Point objects.

  4. Sort the array using ssort given in Selection.java, much like the Point array was sorted. Print the array before and after sorting and make sure your account is sorted correctly. You may choose to sort the array by account owner's name or by account balance. Your call!

  5. Perform a search using search given in Selection.java, much like the Point array was used to do a search.

  6. Add a sorting method that implements insertion sort (isort) this time to sort an array of objects in Selection.java. This method should be capable of sorting an array of any objects as long as it implements the Comparable interface, much like the ssort method in Selection.java. Although the file name is Selection.java, we will add an insertion sort method there as well. If that bothers you, rename the file to something that is more appropriate such as Sort.java

  7. Use isort to sort the Account array this time. Print the array before and after sorting as we did with selection sort.

Make sure that your program works correctly. If you have difficulties, please get help during the lab and also outside the lab if you did not finish it in the lab.

When you are done:

When you are done with these, feel free to leave the lab.