Download presentation
Presentation is loading. Please wait.
Published byShona Norton Modified over 8 years ago
1
CS 100Lecture 261 CS100J Lecture 26 n Previous Lecture –Application of inheritance –“Higher-order" methods –Abstract classes –Visibility modifier: protected n This Lecture –Interfaces –Comparable –Reflection –super –Reading: n Lewis & Loftus, re-read Section 5.4 n Savitch, Appendix 7
2
CS 100Lecture 262 Interfaces n Interface - Like an abstract class: –A collection of abstract methods that must be defined by any class that implements the interface. –Primitive types do not implement interfaces n Syntax of interfaces interface interface-name {list-of-method-signatures} n Extended syntax of classes class class-name 1 extends class-name 2 implements list-of-interface-names {... }
3
CS 100Lecture 263 Interface example: Comparable // Linearly ordered objects. interface Comparable { /* Compare this object with obj: - if this before obj, return negative, - if this equals obj, return 0, - if this after obj, return positive. */ public int compareTo(Object obj); } n The following predefined classes all implement the Comparable interface: –Byte –Character –Double –Float –Integer –Long –Short –String –java.math.BigDecimal –java.math.BigInteger –etc.
4
CS 100Lecture 264 An implementation of Comparable class Color implements Comparable { private int c; public Color(String s) { if (s.equals("red")) c = 0; else if (s.equals("white")) c = 1; else if (s.equals("blue")) c = 2; else throw new RuntimeException("bad color"); } public String toString() { if (c == 0) return "red"; else if (c == 1) return "white"; else return "blue"; } public int compareTo(Object obj) { return c-((Color)obj).c; }
5
CS 100Lecture 265 Recall the sort method /* Sort A into non-decreasing order. */ static void sort( int[] A ) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) { /* Given that A[0..k-1] is finished, /* Given that A[0..k-1] is finished, finish A[0..k]. */ finish A[0..k]. */ int minLoc; // subscript of int minLoc; // subscript of // smallest in A[k..m] // smallest in A[k..m] /* Set minLoc so A[minLoc] is /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ smallest in A[k..m]. */ minLoc = k; minLoc = k; for (int i=k+1; i <= m; i++) for (int i=k+1; i <= m; i++) if ( A[i] < A[minLoc] ) if ( A[i] < A[minLoc] ) minLoc = i; /* Exchange A[k] and A[minLoc] */ /* Exchange A[k] and A[minLoc] */{ int temp = A[k]; int temp = A[k]; A[k] = A[minLoc]; A[k] = A[minLoc]; A[minLoc] = temp; A[minLoc] = temp;}}}
6
CS 100Lecture 266 /* Sort A into non-decreasing order. */ static void sort( Comparable[] A ) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) { /* Given that A[0..k-1] is finished, /* Given that A[0..k-1] is finished, finish A[0..k]. */ finish A[0..k]. */ int minLoc; // subscript of int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ smallest in A[k..m]. */ minLoc = k; minLoc = k; for (int i=k+1; i <= m; i++) for (int i=k+1; i <= m; i++) if (A[i].compareTo(A[minLoc])< 0 ) if (A[i].compareTo(A[minLoc])< 0 ) minLoc = i; /* Exchange A[k] and A[minLoc] */ /* Exchange A[k] and A[minLoc] */{ Comparable temp = A[k]; Comparable temp = A[k]; A[k] = A[minLoc]; A[k] = A[minLoc]; A[minLoc] = temp; A[minLoc] = temp;}}} A polymorphic sort method
7
CS 100Lecture 267 Sorting an array of Colors /* Print the elements of an arbitrary array of objects. */ static void printArray(Object[] A) {System.out.println("-------------"); for (int i=0; i < A.length; i++) System.out.println(A[i] + " "); System.out.println("-------------");} Color[] C = { new Color("blue"), new Color("white"), new Color("red") }; sort(C); printArray(C); n Output: -------------redwhiteblue-------------
8
CS 100Lecture 268 Sorting an array of Integers Integer[] B = { new Integer(3), new Integer(1), new Integer(2) }; sort(B); printArray(B); n Output: -------------123------------- n Note two kinds of polymorphism –Inheritance hierarchy In printArray(B), Integer is a subclass of Object In printArray(B), Integer is a subclass of Object –Interface implementations In Sort(B), Integer implements Comparable In Sort(B), Integer implements Comparable
9
CS 100Lecture 269 Taxonomy of Polygons Polygon Triangle Isosceles Equilateral Rhombus Square Trapezoid Parallelogram Quadrilateral
10
CS 100Lecture 2610 A taxonomy of Quadrilaterals Parallelogram Rhombus Square Rectangle Rhomboid: all sides equal length Paralleloid: opposite sides equal length Parallelopoid : opposite sides parallel
11
CS 100Lecture 2611 A different taxonomy of Quadrilaterals Parallelogram Rhombus Square Rectangle all angles equal opposite angles equal Parallelopoid : opposite sides parallel
12
CS 100Lecture 2612 Interface example: Shape // Objects with area and perimeter. interface Shape { public double area(); public double perimeter(); }
13
CS 100Lecture 2613 A class can implement multiple interfaces import java.lang.reflect.*; abstract class Parallelopoid implements Shape, Comparable { // Compare two shapes based on areas. public int compareTo(Object obj) public int compareTo(Object obj){ double d = area() -((Shape)obj).area(); double d = area() -((Shape)obj).area(); return (d 0) ? +1 : 0; return (d 0) ? +1 : 0;} public String toString() { return getClass().getName() + ":" + return getClass().getName() + ":" + " area: " + (float)area() + " perimeter: " + (float)perimeter(); }}
14
CS 100Lecture 2614 class Rhomboid abstract class Rhomboid extends Parallelopoid { double side; public double perimeter() { return 4*side; } Rhomboid(double side) { this.side = side; } } class Square extends Rhomboid { Square(double side) { super(side); } public double area() { return side*side; } } class Rhombus extends Rhomboid { double theta; Rhombus(double side, double theta) { super(side); this.theta = theta; } public double area() { return side*side*Math.sin(theta); } }
15
CS 100Lecture 2615 class Paralleloid abstract class Paralleloid extends Parallelopoid { double side1, side2; Paralleloid( double side1, double side2) { this.side1 = side1; this.side2 = side2; } public double perimeter() { return 2*(side1 + side2); } } class Rectangle extends Paralleloid { Rectangle(double side1, double side2) { super(side1, side2); } public double area() { return side1*side2; } } class Parallelogram extends Paralleloid { double theta; Parallelogram (double side1, (double side1, double side2, double theta ) { super(side1, side2); this.theta= theta; } public double area() { return side1 * side2 * Math.sin(theta); } }
16
CS 100Lecture 2616 Sorting an array of Parallelopoids Parallelopoid[] A = { new Square(1.0), new Rhombus(1.0, Math.PI/4), new Rectangle(1.0, 2.0), new Parallelogram(1.0, 2.0, Math.PI/4), }; sort(A); printArray(A); n Output: ------------- Rhombus: area: 0.70710677 perimeter: 4.0 Square: area: 1.0 perimeter: 4.0 Parallelogram: area: 1.4142135 perimeter: 6.0 Rectangle: area: 2.0 perimeter: 6.0 -------------
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.