CS100J Lecture 26 - PowerPoint PPT Presentation

About This Presentation
Title:

CS100J Lecture 26

Description:

Rhomboid: all sides. equal length. Paralleloid: opposite sides. equal ... class Rhombus extends Rhomboid. double theta; Rhombus(double side, double theta) ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 17
Provided by: Mill1
Category:

less

Transcript and Presenter's Notes

Title: CS100J Lecture 26


1
CS100J Lecture 26
  • Previous Lecture
  • Application of inheritance
  • Higher-order" methods
  • Abstract classes
  • Visibility modifier protected
  • This Lecture
  • Interfaces
  • Comparable
  • Reflection
  • super
  • Reading
  • Lewis Loftus, Section 5.5
  • Savitch, Appendix 7

2
Interfaces
  • 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
  • Syntax of interfaces
  • interface interface-name
  • list-of-method-signatures
  • Extended syntax of classes
  • class class-name1
  • extends class-name2
  • implements list-of-interface-names
  • . . .

3
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)
  • The following predefined classes all implement
    the Comparable interface
  • Byte
  • Character
  • Double
  • Float
  • Integer
  • Long
  • Short
  • String

4
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"

5
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 lt m k )
  • / Given that A0..k-1 is finished,
  • finish A0..k. /
  • int minLoc // subscript of
  • // smallest in Ak..m
  • / Set minLoc so AminLoc is
  • smallest in Ak..m. /
  • minLoc k
  • for (int ik1 i lt m i)
  • if ( Ai lt AminLoc )
  • minLoc i
  • / Exchange Ak and AminLoc /

6
A polymorphic sort method
  • / Sort A into non-decreasing order. /
  • static void sort( Comparable A )
  • int m A.length - 1
  • for ( int k 0 k lt m k )
  • / Given that A0..k-1 is finished,
  • finish A0..k. /
  • int minLoc // subscript of
  • // smallest in Ak..m
  • / Set minLoc so AminLoc is
  • smallest in Ak..m. /
  • minLoc k
  • for (int ik1 i lt m i)
  • if (Ai.compareTo(AminLoc)lt 0 )
  • minLoc i
  • / Exchange Ak and AminLoc /

7
Sorting an array of Colors
  • / Print the elements of an arbitrary array of
    objects. /
  • static void printArray(Object A)
  • System.out.println("-------------")
  • for (int i0 i lt A.length i)
  • System.out.println(Ai " ")
  • System.out.println("-------------")
  • Color C
  • new Color("blue"),
  • new Color("white"),
  • new Color("red")
  • sort(C) printArray(C)
  • Output

8
Sorting an array of Integers
  • Integer B
  • new Integer(3),
  • new Integer(1),
  • new Integer(2)
  • sort(B) printArray(B)
  • Output
  • -------------
  • 1
  • 2
  • 3
  • -------------
  • Note two kinds of polymorphism
  • Inheritance hierarchy
  • In printArray(B), Integer is a subclass of Object

9
Taxonomy of Polygons
10
A taxonomy of Quadrilaterals
Parallelopoid opposite sides parallel
Paralleloid opposite sides equal length
Rhomboid all sides equal length
Rectangle
11
A different taxonomy of Quadrilaterals
Parallelopoid opposite sides parallel
all angles equal
opposite angles equal
Rectangle
12
Interface example Shape
  • // Objects with area and perimeter.
  • interface Shape
  • public double area()
  • public double perimeter()

13
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)
  • double d area() -((Shape)obj).area()
  • return (d lt 0) ? -1 (d gt 0) ? 1 0
  • public String toString()
  • return getClass().getName() ""
  • " area " (float)area()
  • " perimeter " (float)perimeter()

14
class Rhomboid
  • abstract class Rhomboid extends Parallelopoid
  • double side
  • public double perimeter()
  • return 4side
  • Rhomboid(double side)
  • this.side side
  • class Square extends Rhomboid
  • Square(double side)
  • super(side)
  • public double area()
  • return sideside
  • class Rhombus extends Rhomboid

15
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 side1side2
  • class Parallelogram extends Paralleloid

16
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)
  • 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
  • -------------
Write a Comment
User Comments (0)
About PowerShow.com