cis3023: programming fundamentals for cis majors ii summer 2010 ganesh viswanathan interfaces (part...

25
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 “A picture is worth a thousand words. An interface is worth a thousand pictures.” -Ben Shneiderman

Upload: katherine-pont

Post on 28-Mar-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

CIS3023: Programming Fundamentals for CIS Majors IISummer 2010

Ganesh Viswanathan

Interfaces (Part II)

Course Lecture Slides28 June 2010

“A picture is worth a thousand words. An interface is worth a thousand pictures.” -Ben Shneiderman

Page 2: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Interface

• Language construct that specifies functionality without any hint at implementation.

• An interface contains method specifications but no method implementations.

2

Page 3: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Comparable Interface// Interface defined in java.lang package

public interface Comparable { public int compareTo(Object o);

}

3

compareTo( )• Compares this object with the specified object for order. • Returns a negative integer, zero or a positive integer when this object is

less than, equal to or greater than the specified object.• Throws: ClassCastException if the specified object's type prevents it

from being compared to this Object.

Page 4: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Declaring Classes to Implement Comparable

4

Rectangle -

GeometricObject -

«interface» java.lang.Comparable

+compareTo(o: Object): int

Notation: The interface name and the method names are italicized. The dashed lines and hollow triangles are used to point to the interface.

ComparableRectangle -

Page 5: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

5

public class ComparableRectangle extends Rectangle implements Comparable {

public ComparableRectangle(double width, double height){ super(width, height);

}

// Implement the compareTo method defined in Comparable public int compareTo(Object o) {

ComparableRectangle cr = (ComparableRectangle)o; if (getArea() > cr.getArea())

return 1;else if(getArea() < cr.getArea()) return -1;else return 0;}

}

Page 6: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Using Arrays.sort()If you have an array of objects of some class C and you

want to use

Arrays.sort(Object[] a)

or Arrays.sort(Object[] a, int fromIndex, int toIndex)

to sort this array, you must make class C implement Comparable interface

The above sort() methods use the compareTo() method to compare two array elements of reference type

6

Page 7: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Arrays.Sort()

What if you want to sort this array of objects in order of different attributes of class C at different times?

Solution: Use

Arrays.sort(Object[] a, Comparator c)

Or Arrays.sort(Object[] a, int fromIndex, int toIndex, Comparator c)

7

Page 8: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Comparator Interfacepublic interface Comparator {

public int compare(Object o1, Object o2)}

Returns: a negative integer, zero, or a positive integer when the first argument is less than, equal to, or greater than the second.

Throws: ClassCastException - if the arguments' types prevent them from being compared by this Comparator.

8

Page 9: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

SolutionCreate a class that implements the Comparator

interfaceCode the compare() method to define the ordering

between objects of this class

Pass an instance of this class to the Arrays.Sort() method

9

Page 10: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Another scenarioWhat if

you are using a class C which does not implement the Comparable interface

You do not have access to C.java fileYou only have access to the C.class file

And you want to sort an arrays of objects of class C in order of some attribute of the class

10

Page 11: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

SolutionCreate a class that implements the Comparator

interfaceCode the compare() method to define the ordering

between objects of this class

11

Page 12: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Another Advantage of using interfaces as data types

12

public class Max { public static Comparable max(? o1, ? o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; }}

ComparableRectangle rectangle1 = new ComparableRectangle(4, 5);ComparableRectangle rectangle2 = new ComparableRectangle(3, 6);System.out.println(Max.max(rectangle1, rectangle2));

Page 13: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Another Advantage of using interfaces as data types

13

public class Max { public static Comparable max(Comparable o1, Comparable o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; }}

ComparableRectangle rectangle1 = new ComparableRectangle(4, 5);ComparableRectangle rectangle2 = new ComparableRectangle(3, 6);System.out.println(Max.max(rectangle1, rectangle2));

Page 14: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Interfaces, cont

(a)

// Max.java: Find a maximum object public class Max { /** Return the maximum of two objects */ public static Comparable max(Comparable o1, Comparable o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; } }

(b)

// Max.java: Find a maximum object public class Max { /** Return the maximum of two objects */ public static Object max(Object o1, Object o2) { if (((Comparable)o1).compareTo(o2) > 0) return o1; else return o2; } }

Max method in (a) is more robust.

Page 15: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Get more info!

• Tutorial: http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html

• Comparable vs Comparator:

http://grdurand.com/static/presentation_four/comparable.html

• Using comparable:http://onjava.com/pub/a/onjava/2003/03/12/java_comp.html

15

Page 16: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

The Cloneable Interface

It is a Marker Interface i.e.

it does not contain constants or methods.

Defined in the java.lang package as follows:

package java.lang;public interface Cloneable { }

16

Page 17: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

ExamplesMany classes (e.g., Date and Calendar) in the Java library implement Cloneable. Thus, the instances of these classes can be cloned. For example, the following code

displays

Calendar calendar = new GregorianCalendar(2003, 2, 1);Calendar calendarCopy = (Calendar)calendar.clone();

System.out.println("calendar == calendarCopy is " + (calendar == calendarCopy));

System.out.println("calendar.equals(calendarCopy) is " + calendar.equals(calendarCopy));

calendar == calendarCopy is falsecalendar.equals(calendarCopy) is true  

Page 18: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Implementing Cloneable Interface A class that implements the Cloneable interface must override the clone() method in the Object class.

18

Page 19: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

19

public class House implements Cloneable{ private int id; private double area;

public House(int id, double area) {this.id = id;this.area = area;

}

public double getId() { return id; } public double getArea() { return area; }

public Object clone() throws CloneNotSupportedException {

return super.clone(); }}

Page 20: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

20

public class House implements Cloneable{ private int id; private double area; private java.util.Date whenBuilt;

public House(int id, double area) {this.id = id;this.area = area;whenBuilt = new java.util.Date();

}

public double getId() { return id; } public double getArea() { return area; } public java.util.Date getWhenBuilt(){ return whenBuilt;

}

public Object clone() throws CloneNotSupportedException {

return super.clone(); }

}

Page 21: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Shallow vs. Deep Copy

21

house1: House

id

area

whenBuilt

1

whenBuilt: Date date object contents house2 = house1.clone()

1750.50 reference

house2: House

id

area

whenBuilt

1

1750.50 reference

House house1 = new House(1, 1750.50);

House house2 = (House)house1.clone();

Page 22: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

22

public class House implements Cloneable{ private int id; private double area; private java.util.Date whenBuilt;

// other code

public Object clone() throws CloneNotSupportedException { House house = (House)super.clone();

house.whenBuilt = (java.util.Date)(whenBuilt.clone()); return house;

}}

Page 23: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

23

public class House implements Cloneable{ private int id; private double area; private String name; public House(int id, double area) {

this.id = id;this.area = area;whenBuilt = new java.util.Date();

}

public double getId() { return id; } public double getArea() { return area; } public java.util.Date getWhenBuilt(){ return whenBuilt;

}

public Object clone() throws CloneNotSupportedException {

return super.clone(); }}

Is the clone() method doing deep copy?

Page 24: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

Can the clone() method be written as follows?

class House implements Cloneable { private int numberOfRooms; private int squareFeet;

//...

public Object clone() throws CloneNotSupportedException {

return new House(numberOfRooms, squareFeet); }

}

24

Page 25: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is

What happens when the following code is executed?

public class MultiStoryHouse extends House { private int numberOfStories;

public MultiStoryHouse(int numRooms, int sqFootage, int numStories) {super(numRooms, sqFootage); numberofStories = numStories;

}// no clone method defined

}

public class Driver {public static void main(String[] args) {

MultiStoryHouse tsh = new MultiStoryHouse(6, 3000, 2); MultiStoryHouse other = (MultiStoryHouse)tsh.clone(); //Exception

}}

25