ok this one has got me lost and I just cant seem to figure it out at all.
basically I have a program where a user creates three shapes and they are then displayed in the order inputted and then the program is suppose to oranize them by area, smallest to biggest. This chapter has to do with polymorphism and inheritance if that helps at all. I have 3 shapes I can make, Circle, Rectangle, and a Square. The base class is called Shape. The program that these classes actually run in is this:
so what has me lost is, how do I organize the shapes in the last part of the program by calling the area() method using dynamic binding?
BTW, I have read the small section on dynamic binding in my book (Java, Savitch 2008) and my professors notes and I am totally lost. So I'd appreciate it if you could keep it simple.
posted 7 years ago
I assume that you have an abstract method called "getArea" (or similar) in class Shape, correct? In that case, "slist.getArea()" would invoke whichever method is appropriate for the shape at position 0.
Are you supposed to implement your own sorting method? If not, you can use the java.util.Arrays.sort(Object) method. All you'd need to do would be to have each shape implement the Comparable interface, and use the objects' area in the compareTo method. [ March 14, 2008: Message edited by: Ulf Dittmer ]