20-abstract_classes - CSE143 Lecture20 Abstractclasses...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 20 Abstract classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Circle public class Circle { private double radius; public Circle( double radius) { this .radius = radius; } public double area() { return Math.PI * radius * radius; } public String toString() { return "circle of area " + area(); } }
Background image of page 2
3 Rectangle public class Rectangle { private double length; private double width; public Rectangle( double length, double width) { this .length = length; this .width = width; } public double area() { return length * width; } public String toString() { return "rectangle of area " + area(); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Square public class Square { private double length; public Square( double length) { this .length = length; } public double area() { return length * length; } public String toString() { return "square of area " + area(); } }
Background image of page 4
5 Shape client code import java.util.*; public class ShapeTest { public static void main(String[] args) { Object[] data = { new Square(12), new Rectangle(15, 3.2), new Circle(8.4), new Circle(1.5), new Square(8.7), new Rectangle(7.2, 3.2), new Square(2.4), new Circle(3.7), new Circle(7.9)}; for (Object s : data) { System.out.println(s); } System.out.println(); Arrays.sort(data); for (Object s : data) { System.out.println(s); } } } Recall that the Object class is at the top of the inheritance hierachy
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Comparable? Client code crashes when trying to sort the elements ÏÏ Exception in thread "main" java.lang.ClassCastException: Square cannot be cast to java.lang.Comparable ÏÏ§Ï at java.util.Arrays.mergeSort(Arrays.java:1144) ÏÏ§Ï at java.util.Arrays.mergeSort(Arrays.java:1155) ÏÏ§Ï at java.util.Arrays.sort(Arrays.java:1079) ÏÏ§Ï at ShapeTest.main(ShapeTest.java:13) Sort method tries to cast object to Comparable so it can call  compareTo  method. BTW, notice that Java uses merge sort to sort!
Background image of page 6
7 Implementing Comparable public class Square implements Comparable<Square> { ... Writing the  compareTo  method… public int compareTo(Square other) { return area() - other.area(); } The previous method has the wrong return type: public int compareTo(Square other) { return ( int )(area() - other.area()); }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Writing compareTO This previous method also fails!  If the difference is between -1 and  1.  Casting to  int  rounds it to 0. Correct answer:
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

20-abstract_classes - CSE143 Lecture20 Abstractclasses...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online