20-abstract_classes

# 20-abstract_classes - CSE143 Lecture20 Abstractclasses...

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

CSE 143 Lecture 20 Abstract classes

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

View Full Document
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(); } }
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(); } }

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

View Full Document
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(); } }
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

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

View Full Document
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!
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()); }

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

View Full Document
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:
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 / 26

20-abstract_classes - CSE143 Lecture20 Abstractclasses...

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

View Full Document
Ask a homework question - tutors are online