20-abstract_classes - CSE 143 Lecture 20 Abstract classes...

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

View Full Document Right Arrow Icon
1 CSE 143 Lecture 20 Abstract classes 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 1

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

View Full DocumentRight Arrow Icon
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(); } } 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 2
3 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 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 3

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

View Full DocumentRight Arrow Icon
4 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()); } 8 Writing compareTO This previous method also fails! If the difference is between -1 and 1. Casting to int rounds it to 0.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 13

20-abstract_classes - CSE 143 Lecture 20 Abstract classes...

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

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