COP 3503 FALL 2012 Shayan Javed Lecture 7

162
1 / 162 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 7 Programming Fundamentals using Java 1

description

COP 3503 FALL 2012 Shayan Javed Lecture 7. Programming Fundamentals using Java. More With Interfaces. Interfaces. Language construct for specifying functionality without implementation Method specifications but no implementations. Interfaces in Software Engineering. - PowerPoint PPT Presentation

Transcript of COP 3503 FALL 2012 Shayan Javed Lecture 7

Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 7

1 / 162

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 7

Programming Fundamentals using Java

1

Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 7

2 / 162

More With Interfaces

Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 7

3 / 162

Interfaces

Language construct for specifying functionality without implementation

Method specifications but no implementations.

Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 7

4 / 162

Interfaces in Software Engineering

In large projects you have a lot of classes interacting with each other

Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 7

5 / 162

Interfaces in Software Engineering

In large projects you have a lot of classes interacting with each other

Need formal descriptions of what a class does, and how it interacts with other classes.

Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 7

6 / 162

Interfaces in Software Engineering

In large projects you have a lot of classes interacting with each other

Need formal descriptions of what a class does, and how it interacts with other classes.

Interfaces are a way to define expected behavior.

Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 7

7 / 162

Interfaces in Software Engineering

In large projects you have a lot of classes interacting with each other

Need formal descriptions of what a class does, and how it interacts with other classes.

Interfaces are a way to define expected behavior.

Useful when multiple teams working in tandem.

Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 7

8 / 162

Interfaces

Some interfaces already defined in Java Widely used

Comparable

Comparator

Cloneable

Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 7

9 / 162

The Comparable interface

Defined in the java.lang Package

Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 7

10 / 162

The Comparable interface

Defined in the java.lang Package

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

}

Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 7

11 / 162

The Comparable interface

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

}

Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 7

12 / 162

The Comparable interface

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

}

Comparisons between objects of the same type

Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 7

13 / 162

The Comparable interface

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

}

Comparisons between objects of the same type Object1.compareTo(Object2)

Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 7

14 / 162

The Comparable interface

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

}

Comparisons between objects of the same type Object1.compareTo(Object2) Return values:

< 0 = Object 1 < Object 2 (usually -1)

Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 7

15 / 162

The Comparable interface

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

}

Comparisons between objects of the same type Object1.compareTo(Object2) Return values:

< 0 = Object 1 < Object 2 (usually -1) == 0 = Object 1 == Object 2

Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 7

16 / 162

The Comparable interface

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

}

Comparisons between objects of the same type Object1.compareTo(Object2) Return values:

< 0 = Object 1 < Object 2 (usually -1) == 0 = Object 1 == Object 2 > 0 = Object 1 > Object 2 (usually 1)

Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 7

17 / 162

The Comparable interface

Used commonly.

Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 7

18 / 162

The Comparable interface

Used commonly.

For ex. the String and Date classes.

Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 7

19 / 162

The Comparable interface

public String implements Comparable,… {public int compareTo(Object o) {

// compares the two Strings lexicographically

}}

Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 7

20 / 162

The Comparable interface

public String implements Comparable,… {public int compareTo(Object o) {

// compares the two Strings lexicographically

}}

Ex.: "computer".compareTo ("comparison")

Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 7

21 / 162

The Comparable interface

public String implements Comparable,… {public int compareTo(Object o) {

// compares the two Strings lexicographically

}}

Ex.: "computer".compareTo ("comparison")

Returns 20

Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 7

22 / 162

The Comparable interface

"computer".compareTo ("comparison")

Returns 20

Provides the first non-zero difference in ASCII values.

Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 7

23 / 162

The Comparable interface

"computer".compareTo ("comparison")

Returns 20

Provides the first non-zero difference in ASCII values. “c”, “o”, “m”, “p” are all equal

Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 7

24 / 162

The Comparable interface

"computer".compareTo ("comparison")

Returns 20

Provides the first non-zero difference in ASCII values. “c”, “o”, “m”, “p” are all equal

Returns: (int)‘u’ - (int)‘a’

Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 7

25 / 162

The Comparable interface

"computer".compareTo ("comparison")

Returns 20

Provides the first non-zero difference in ASCII values. “c”, “o”, “m”, “p” are all equal

Returns: (int)‘u’ - (int)‘a’

So “computer” > “comparison”

Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 7

26 / 162

The Comparable interface

Implement it for the Rectangle class

Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 7

27 / 162

The Comparable interface

Implement it for the Rectangle class Comparisons based on area

Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 7

28 / 162

The Comparable interface

Implement it for the Rectangle class Comparisons based on area

public class Rectangle extends GeometricObject implements Comparable {public int compareTo(Object ob) {

Rectangle r = (Rectangle)ob;if (this.getArea() > r.getArea())

return 1;else if (r.getArea() > this.getArea())

return -1;else

return 0;}

}

Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 7

29 / 162

The Comparable interface

What if a non-Rectangle object is passed in?

Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 7

30 / 162

The Comparable interface

What if a non-Rectangle object is passed in?

A ClassCastException is thrown

Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 7

31 / 162

The Comparable interface

What if a non-Rectangle object is passed in?

A ClassCastException is thrown

Will look at it later

Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 7

32 / 162

The Comparable interface

Can also use Comparable as a data type

Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 7

33 / 162

The Comparable interface

Can also use Comparable as a data type

String s = “aString”;(s instanceof Comparable) // returns true!

Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 7

34 / 162

The Comparable interface

Can also use Comparable as a data type

String s = “aString”;(s instanceof Comparable) // returns true!

Comparable[] compObjects = new Comparable[5];

Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 7

35 / 162

The Comparable interface

So now we can compare objects

Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 7

36 / 162

The Comparable interface

So now we can compare objects

Would be nice if we could sort them using this info

Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 7

37 / 162

Sorting

Always need data sorted by certain requirements

Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 7

38 / 162

Sorting

Always need data sorted by certain requirements

How do you sort arrays?

Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 7

39 / 162

Sorting

Always need data sorted by certain requirements

How do you sort arrays?

Will look at specific sorting algorithms later on.

Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 7

40 / 162

Sorting

Sorting on Amazon’s website

Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 7

41 / 162

Sorting

By different categories

Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 7

42 / 162

Comparing Bookspublic Book implements Comparable{

String title;int popularity;double price, avgCustomerReview;Date publicationDate;

}

Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 7

43 / 162

Comparing Bookspublic Book implements Comparable{

String title;int popularity;double price, avgCustomerReview;Date publicationDate;

// comparison based on pricepublic int compareTo(Object o) {

Book b = (Book)o;if (price > b.getPrice())

return 1;else if (price < b.getPrice())

return -1;else

return 0;}

}

Page 44: COP 3503 FALL 2012 Shayan Javed Lecture 7

44 / 162

Arrays.sort()

An easy way to sort an array

Page 45: COP 3503 FALL 2012 Shayan Javed Lecture 7

45 / 162

Arrays.sort()

An easy way to sort an array Works directly with most of the primitive types –

int, char, double, long, etc.

Page 46: COP 3503 FALL 2012 Shayan Javed Lecture 7

46 / 162

Arrays.sort()

An easy way to sort an array Works directly with most of the primitive types –

int, char, double, long, etc.

int numbers[] = {4, 1, 19, 8};Arrays.sort(numbers);

Page 47: COP 3503 FALL 2012 Shayan Javed Lecture 7

47 / 162

Arrays.sort()

An easy way to sort an array Works directly with most of the primitive types –

int, char, double, long, etc.

int numbers[] = {4, 1, 19, 8};Arrays.sort(numbers);

numbers = [1, 4, 8, 19]

Page 48: COP 3503 FALL 2012 Shayan Javed Lecture 7

48 / 162

Arrays.sort()

Can also sort arrays of Objects

Page 49: COP 3503 FALL 2012 Shayan Javed Lecture 7

49 / 162

Arrays.sort()

Can also sort arrays of Objects

Arrays.sort(Object[] o)

Page 50: COP 3503 FALL 2012 Shayan Javed Lecture 7

50 / 162

Arrays.sort()

Can also sort arrays of Objects

Arrays.sort(Object[] o)

Make sure that the objects in the array: Implement Comparable

Page 51: COP 3503 FALL 2012 Shayan Javed Lecture 7

51 / 162

Arrays.sort()

Can also sort arrays of Objects

Arrays.sort(Object[] o)

Make sure that the objects in the array: Implement Comparable Are of the same type (no ClassCastException thrown)

Page 52: COP 3503 FALL 2012 Shayan Javed Lecture 7

52 / 162

Arrays.sort()

Let’s sort the Books by price

Page 53: COP 3503 FALL 2012 Shayan Javed Lecture 7

53 / 162

Arrays.sort()

Let’s sort the Books by price

Book book1 = new Book(“Game Of Thrones”, 9.99);Book book2 = new Book(“The Help”, 12.99);Book book3 = new Book(“The Road”, 8.99);Book[] books = {book1, book2, book3};

Page 54: COP 3503 FALL 2012 Shayan Javed Lecture 7

54 / 162

Arrays.sort()

Let’s sort the Books by price

Book book1 = new Book(“Game Of Thrones”, 9.99);Book book2 = new Book(“The Help”, 12.99);Book book3 = new Book(“The Road”, 8.99);Book[] books = {book1, book2, book3};

Arrays.sort(books);books = [book3, book1, book2];

Page 55: COP 3503 FALL 2012 Shayan Javed Lecture 7

55 / 162

Arrays.sort()

Let’s sort Rectangles

Page 56: COP 3503 FALL 2012 Shayan Javed Lecture 7

56 / 162

Arrays.sort()

Let’s sort Rectangles

Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};

Page 57: COP 3503 FALL 2012 Shayan Javed Lecture 7

57 / 162

Arrays.sort()

Let’s sort Rectangles

Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};Arrays.sort(objs);

Page 58: COP 3503 FALL 2012 Shayan Javed Lecture 7

58 / 162

Arrays.sort()

Let’s sort Rectangles

Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};Arrays.sort(objs);

objs = [r2, r3, r1]

Page 59: COP 3503 FALL 2012 Shayan Javed Lecture 7

59 / 162

Arrays.sort()

Now we have sorted by area (for Rectangles) and by price (for Books)

Page 60: COP 3503 FALL 2012 Shayan Javed Lecture 7

60 / 162

Arrays.sort()

Now we have sorted by area (for Rectangles) and by price (for Books)

What if we want to sort by another criteria?

Page 61: COP 3503 FALL 2012 Shayan Javed Lecture 7

61 / 162

The Comparator interface

Provides an alternative way of comparison

Page 62: COP 3503 FALL 2012 Shayan Javed Lecture 7

62 / 162

The Comparator interface

Provides an alternative way of comparison

public interface Comparator {public int compare(Object o1, Object

o2);}

// To use the interface:

import java.util.Comparator;

Page 63: COP 3503 FALL 2012 Shayan Javed Lecture 7

63 / 162

The Comparator interface

public interface Comparator {public int compare(Object o1, Object o2);

}

Comparisons between objects of the same type

Page 64: COP 3503 FALL 2012 Shayan Javed Lecture 7

64 / 162

The Comparator interface

public interface Comparator {public int compare(Object o1, Object o2);

}

Comparisons between objects of the same type Object1.compare(Object1, Object2)

Page 65: COP 3503 FALL 2012 Shayan Javed Lecture 7

65 / 162

The Comparator interface

public interface Comparator {public int compare(Object o1, Object o2);

}

Comparisons between objects of the same type Object1.compare(Object1, Object2) Object3.compare(Object1, Object2)

Page 66: COP 3503 FALL 2012 Shayan Javed Lecture 7

66 / 162

The Comparator interface

public interface Comparator {public int compare(Object o1, Object o2);

}

Comparisons between objects of the same type Object1.compare(Object1, Object2) Object3.compare(Object1, Object2) Return values:

< 0 = Object 1 < Object 2 (usually -1) == 0 = Object 1 == Object 2 > 0 = Object 1 > Object 2 (usually 1)

Page 67: COP 3503 FALL 2012 Shayan Javed Lecture 7

67 / 162

The Comparator interface

Now let’s sort books by popularity.

public Book implements Comparable{String title;int popularity;double price, avgCustomerReview;Date publicationDate;

}

Page 68: COP 3503 FALL 2012 Shayan Javed Lecture 7

68 / 162

The Comparator interfacepublic Book implements Comparable, Comparator{

// Properties..// compareTo…

// comparison based on popularitypublic int compare (Object o1, Object o2) {

}}

Page 69: COP 3503 FALL 2012 Shayan Javed Lecture 7

69 / 162

The Comparator interfacepublic Book implements Comparable, Comparator{

// Properties..// compareTo…

// comparison based on popularitypublic int compare (Object o1, Object o2) {

Book b1 = (Book)o1;

}}

Page 70: COP 3503 FALL 2012 Shayan Javed Lecture 7

70 / 162

The Comparator interfacepublic Book implements Comparable, Comparator{

// Properties..// compareTo…

// comparison based on popularitypublic int compare (Object o1, Object o2) {

Book b1 = (Book)o1;Book b2 = (Book)o2;

}}

Page 71: COP 3503 FALL 2012 Shayan Javed Lecture 7

71 / 162

The Comparator interfacepublic Book implements Comparable, Comparator{

// Properties..// compareTo…

// comparison based on popularitypublic int compare (Object o1, Object o2) {

Book b1 = (Book)o1;Book b2 = (Book)o2;if (b1.getPopularity() > b2.getPopularity())

return 1;if (b1.getPopularity() < b2.getPopularity())

return -1;else

return 0;}

}

Page 72: COP 3503 FALL 2012 Shayan Javed Lecture 7

72 / 162

The Comparator interface

Sort using Comparator

Page 73: COP 3503 FALL 2012 Shayan Javed Lecture 7

73 / 162

The Comparator interface

Sort using Comparator

Arrays.sort(Object[] array, Comparator object)

object = object of a class which implements Comparator

Page 74: COP 3503 FALL 2012 Shayan Javed Lecture 7

74 / 162

The Comparator interface

Sorting Books based on popularity

Page 75: COP 3503 FALL 2012 Shayan Javed Lecture 7

75 / 162

The Comparator interface

Sorting Books based on popularity

Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};

Page 76: COP 3503 FALL 2012 Shayan Javed Lecture 7

76 / 162

The Comparator interface

Sorting Books based on popularity

Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};

Arrays.sort(books, book1);

Page 77: COP 3503 FALL 2012 Shayan Javed Lecture 7

77 / 162

The Comparator interface

Sorting Books based on popularity

Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};

Arrays.sort(books, book1);books = [book2, book1, book3];

Page 78: COP 3503 FALL 2012 Shayan Javed Lecture 7

78 / 162

The Comparator interface

Sorting Books based on popularity

Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};

Arrays.sort(books, book1); // Replace book1 with book2?

Page 79: COP 3503 FALL 2012 Shayan Javed Lecture 7

79 / 162

The Comparator interface

Sorting Books based on popularity

Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};

Arrays.sort(books, book2); // Replace book1 with book2?

Page 80: COP 3503 FALL 2012 Shayan Javed Lecture 7

80 / 162

The Comparator interface

Sorting Books based on popularity

Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};

Arrays.sort(books, book2); // Replace book1 with book2?

books = [book2, book1, book3]; // same output

Page 81: COP 3503 FALL 2012 Shayan Javed Lecture 7

81 / 162

The Comparator interface

Sorting Books based on popularity

Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};

Arrays.sort(books, book2); // Replace book1 with book2?

books = [book2, book1, book3]; // same outputObject passed in doesn’t matter

Page 82: COP 3503 FALL 2012 Shayan Javed Lecture 7

82 / 162

The Comparator interface

Implement it for the Rectangle class Comparisons based on width

Page 83: COP 3503 FALL 2012 Shayan Javed Lecture 7

83 / 162

The Comparator interface

public class Rectangle extends GeometricObject implements Comparator {

public int compare(Object ob1, Object ob2) {Rectangle r1 = (Rectangle)ob1;Rectangle r2 = (Rectangle)ob2;if (r1.getWidth() > r2.getWidth() )

return 1;else if (r1.getWidth() < r2.getWidth()

return -1;else

return 0;}

}

Page 84: COP 3503 FALL 2012 Shayan Javed Lecture 7

84 / 162

The Comparator interface

Rectangle(width, height)

Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};

Page 85: COP 3503 FALL 2012 Shayan Javed Lecture 7

85 / 162

The Comparator interface

Rectangle(width, height)

Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};Arrays.sort(objs, r1); // can also pass in r2 or r3

Page 86: COP 3503 FALL 2012 Shayan Javed Lecture 7

86 / 162

The Comparator interface

Rectangle(width, height)

Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};Arrays.sort(objs, r1); // can also pass in r2 or r3

objs = [r2, r3, r1]

Page 87: COP 3503 FALL 2012 Shayan Javed Lecture 7

87 / 162

The Comparator interface

Now we have sorted by area and width (for Rectangles) and by price and popularity (for Books)

Page 88: COP 3503 FALL 2012 Shayan Javed Lecture 7

88 / 162

The Comparator interface

Now we have sorted by area and width (for Rectangles) and by price and popularity (for Books)

What if we still want to sort by another criteria?

Page 89: COP 3503 FALL 2012 Shayan Javed Lecture 7

89 / 162

The Comparator interface

Already implemented Comparator in the Rectangle/Book classes though…

Page 90: COP 3503 FALL 2012 Shayan Javed Lecture 7

90 / 162

The Comparator interface

Already implemented Comparator in the Rectangle/Book classes though…

Solution: Create a new, empty class which implements Comparable

Page 91: COP 3503 FALL 2012 Shayan Javed Lecture 7

91 / 162

The Comparator interface

Already implemented Comparator in the Rectangle/Book classes though…

Solution: Create a new, empty class which implements Comparable

Comparisons based on other criteria

Page 92: COP 3503 FALL 2012 Shayan Javed Lecture 7

92 / 162

The Comparator interface

Book comparisons based on “Avg. Customer Review” (avgCustomerReview)

Page 93: COP 3503 FALL 2012 Shayan Javed Lecture 7

93 / 162

The Comparator interface

public class ReviewComparison implements Comparator {

}

Page 94: COP 3503 FALL 2012 Shayan Javed Lecture 7

94 / 162

The Comparator interface

public class ReviewComparison implements Comparator {public int compare (Object o1, Object o2) {

Book b1 = (Book)o1;Book b2 = (Book)o2;if (b1.getACR() > b2.getACR())

return 1;if (b1.getACR() < b2.getACR())

return -1;else

return 0;}

}

ACR = Average Customer Review

Page 95: COP 3503 FALL 2012 Shayan Javed Lecture 7

95 / 162

The Comparator interface

To sort based on average customer review:

Page 96: COP 3503 FALL 2012 Shayan Javed Lecture 7

96 / 162

The Comparator interface

To sort based on average customer review:

Arrays.sort(books, new ReviewComparison() )

Page 97: COP 3503 FALL 2012 Shayan Javed Lecture 7

97 / 162

The Comparator interface

So we can use Comparator for comparing objects based on different criteria.

And use Arrays.sort() based on that.

Page 98: COP 3503 FALL 2012 Shayan Javed Lecture 7

98 / 162

Comparable vs. Comparator

Use Comparable ( the compareTo() method):

Page 99: COP 3503 FALL 2012 Shayan Javed Lecture 7

99 / 162

Comparable vs. Comparator

Use Comparable ( the compareTo() method): want to compare based on default natural ordering

Page 100: COP 3503 FALL 2012 Shayan Javed Lecture 7

100 / 162

Comparable vs. Comparator

Use Comparable ( the compareTo() method): want to compare based on default natural ordering have control of the object (source code)

Page 101: COP 3503 FALL 2012 Shayan Javed Lecture 7

101 / 162

Comparable vs. Comparator

Use Comparable ( the compareTo() method): want to compare based on default natural ordering have control of the object (source code)

Use Comparator ( the compare() method):

Page 102: COP 3503 FALL 2012 Shayan Javed Lecture 7

102 / 162

Comparable vs. Comparator

Use Comparable ( the compareTo() method): want to compare based on default natural ordering have control of the object (source code)

Use Comparator ( the compare() method): want to compare based on different behavior from

the default

Page 103: COP 3503 FALL 2012 Shayan Javed Lecture 7

103 / 162

Comparable vs. Comparator

Use Comparable ( the compareTo() method): want to compare based on default natural ordering have control of the object (source code)

Use Comparator ( the compare() method): want to compare based on different behavior from

the default do NOT have control of the object (source code)

Page 104: COP 3503 FALL 2012 Shayan Javed Lecture 7

104 / 162

Object duplication

In large systems, often deal with objects of the same type.

Page 105: COP 3503 FALL 2012 Shayan Javed Lecture 7

105 / 162

Object duplication

In large systems, often deal with objects of the same type.

Need a way to easily create clones of objects.

Page 106: COP 3503 FALL 2012 Shayan Javed Lecture 7

106 / 162

Object duplication

Example: (Halo videogame)

Page 107: COP 3503 FALL 2012 Shayan Javed Lecture 7

107 / 162

Object duplication

Would be tedious to instantiate each one individually

Page 108: COP 3503 FALL 2012 Shayan Javed Lecture 7

108 / 162

Object duplication

Easier to clone (and then maybe modify)

Page 109: COP 3503 FALL 2012 Shayan Javed Lecture 7

109 / 162

Object duplication

Products of multiple sizes/colors

Page 110: COP 3503 FALL 2012 Shayan Javed Lecture 7

110 / 162

Object duplication

Products of multiple sizes/colors

Page 111: COP 3503 FALL 2012 Shayan Javed Lecture 7

111 / 162

Object duplication

Easier to clone (and then modify color)

Page 112: COP 3503 FALL 2012 Shayan Javed Lecture 7

112 / 162

Object duplication

Two types of copies:

Page 113: COP 3503 FALL 2012 Shayan Javed Lecture 7

113 / 162

Object duplication

Two types of copies:

1. Shallow copy

Page 114: COP 3503 FALL 2012 Shayan Javed Lecture 7

114 / 162

Object duplication

Two types of copies:

1. Shallow copy

2. Deep Copy

Page 115: COP 3503 FALL 2012 Shayan Javed Lecture 7

115 / 162

Shallow copy

Bit-wise copy of an object

Page 116: COP 3503 FALL 2012 Shayan Javed Lecture 7

116 / 162

Shallow copy

Bit-wise copy of an object

New object created with exact copy of the values.

Page 117: COP 3503 FALL 2012 Shayan Javed Lecture 7

117 / 162

Shallow copy

Bit-wise copy of an object

New object created with exact copy of the values.

Will create new primitives with same values (int, char, double, etc.)

Page 118: COP 3503 FALL 2012 Shayan Javed Lecture 7

118 / 162

Shallow copy

Bit-wise copy of an object

New object created with exact copy of the values.

Will create new primitives with same values (int, char, double, etc.)

But: Will just copy over references to objects.

Page 119: COP 3503 FALL 2012 Shayan Javed Lecture 7

119 / 162

Shallow copy

References to objects copied.

Page 120: COP 3503 FALL 2012 Shayan Javed Lecture 7

120 / 162

Shallow copy

References to objects copied.

Problem: Modifying objects in clone will also modify objects in the original!

Page 121: COP 3503 FALL 2012 Shayan Javed Lecture 7

121 / 162

Shallow copy

public Book {String title;int popularity;double price, avgCustomerReview;Date publicationDate;

}

Page 122: COP 3503 FALL 2012 Shayan Javed Lecture 7

122 / 162

Shallow copy

public Book {String title;int popularity;double price, avgCustomerReview;Date publicationDate;

}

What happens if you create a shallow copy?

Page 123: COP 3503 FALL 2012 Shayan Javed Lecture 7

123 / 162

Shallow copy

Book1double price

double avg

Date publicationDate

String title

Memory

Page 124: COP 3503 FALL 2012 Shayan Javed Lecture 7

124 / 162

Shallow copy

Book1double price

double avg

Date publicationDateShallow

copy

String title

Memory

Page 125: COP 3503 FALL 2012 Shayan Javed Lecture 7

125 / 162

Shallow copy

Book1double price

double avg

Date publicationDate

Book2

String title

double price

double avg

Shallow copy

String title

Memory

Date?

Page 126: COP 3503 FALL 2012 Shayan Javed Lecture 7

126 / 162

Shallow copy

Book1double price

double avg

Date publicationDate

Book2

String title

double price

double avg

Shallow copy

String title

Memory

Page 127: COP 3503 FALL 2012 Shayan Javed Lecture 7

127 / 162

Shallow copy

Both Books point to the same Date!

Page 128: COP 3503 FALL 2012 Shayan Javed Lecture 7

128 / 162

Shallow copy

Both Books point to the same Date!

Modifying Date object in clone will also modify Date in original.

Page 129: COP 3503 FALL 2012 Shayan Javed Lecture 7

129 / 162

Shallow copy

Both Books point to the same Date!

Modifying Date object in clone will also modify Date in original.

Solution?

Page 130: COP 3503 FALL 2012 Shayan Javed Lecture 7

130 / 162

Deep Copy

Complete copy of an object

Page 131: COP 3503 FALL 2012 Shayan Javed Lecture 7

131 / 162

Deep Copy

Complete copy of an object

References are not copied – new objects are created. (all the way down the hierarchy)

Page 132: COP 3503 FALL 2012 Shayan Javed Lecture 7

132 / 162

Deep Copy

Complete copy of an object

References are not copied – new objects are created. (all the way down the hierarchy)

Of course primitives are copied too (int, char, double, etc.)

Page 133: COP 3503 FALL 2012 Shayan Javed Lecture 7

133 / 162

Deep Copy

Book1double price

double avg

Date publicationDate

String title

Memory

Page 134: COP 3503 FALL 2012 Shayan Javed Lecture 7

134 / 162

Deep Copy

Book1double price

double avg

Date publicationDateDeep

Copy

String title

Memory

Page 135: COP 3503 FALL 2012 Shayan Javed Lecture 7

135 / 162

Deep Copy

Book1double price

double avg

Date publicationDateDeep

Copy

String title

Memory

Book2double price

double avg

Date publicationDate

String title

Page 136: COP 3503 FALL 2012 Shayan Javed Lecture 7

136 / 162

The Cloneable interface

A Marker interface

Page 137: COP 3503 FALL 2012 Shayan Javed Lecture 7

137 / 162

The Cloneable interface

A Marker interface Does not contain constants or methods Used to indicate that it’s legal to clone this object

Page 138: COP 3503 FALL 2012 Shayan Javed Lecture 7

138 / 162

The Cloneable interface

A Marker interface Does not contain constants or methods Used to indicate that it’s legal to clone this object

Defined in the java.lang.package:

package java.lang;public interface Cloneable {

}

Page 139: COP 3503 FALL 2012 Shayan Javed Lecture 7

139 / 162

The Cloneable interface

Use the .clone() method

Page 140: COP 3503 FALL 2012 Shayan Javed Lecture 7

140 / 162

The Cloneable interface

Use the .clone() method

Defined in the Object class:

protected Object clone() throws CloneNotSupportedException

Page 141: COP 3503 FALL 2012 Shayan Javed Lecture 7

141 / 162

The Cloneable interface

Java classes (like Date and Calendar) implement Cloneable

Page 142: COP 3503 FALL 2012 Shayan Javed Lecture 7

142 / 162

The Cloneable interface

Java classes (like Date and Calendar) implement Cloneable

Example:

Calendar calendar = new GregorianCalendar(2004, 2, 1);

Calendar copy = (Calendar)calendar.clone();

Page 143: COP 3503 FALL 2012 Shayan Javed Lecture 7

143 / 162

The Cloneable interface

Java classes (like Date and Calendar) implement Cloneable

Example:

Calendar calendar = new GregorianCalendar(2004, 2, 1);

Calendar copy = (Calendar)calendar.clone();

calendar == copy // ?

Page 144: COP 3503 FALL 2012 Shayan Javed Lecture 7

144 / 162

The Cloneable interface

Java classes (like Date and Calendar) implement Cloneable

Example:

Calendar calendar = new GregorianCalendar(2004, 2, 1);

Calendar copy = (Calendar)calendar.clone();

calendar == copy // ? false

Page 145: COP 3503 FALL 2012 Shayan Javed Lecture 7

145 / 162

The Cloneable interface

Java classes (like Date and Calendar) implement Cloneable

Example:

Calendar calendar = new GregorianCalendar(2004, 2, 1);

Calendar copy = (Calendar)calendar.clone();

calendar == copy // ? falsecalendar.equals(copy) // ?

Page 146: COP 3503 FALL 2012 Shayan Javed Lecture 7

146 / 162

The Cloneable interface

Java classes (like Date and Calendar) implement Cloneable

Example:

Calendar calendar = new GregorianCalendar(2004, 2, 1);

Calendar copy = (Calendar)calendar.clone();

calendar == copy // ? falsecalendar.equals(copy) // ? true

Page 147: COP 3503 FALL 2012 Shayan Javed Lecture 7

147 / 162

The Cloneable interface

Did the clone() method create a shallow or deep copy?

Page 148: COP 3503 FALL 2012 Shayan Javed Lecture 7

148 / 162

The Cloneable interface

Did the clone() method create a shallow or deep copy?

Deep – since they are distinct objects

Page 149: COP 3503 FALL 2012 Shayan Javed Lecture 7

149 / 162

The Cloneable interface

A class that implements Cloneablemust override the clone() method

Page 150: COP 3503 FALL 2012 Shayan Javed Lecture 7

150 / 162

The Cloneable interface

Let’s clone the Book class

Page 151: COP 3503 FALL 2012 Shayan Javed Lecture 7

151 / 162

The Cloneable interface

Let’s clone the Book class

Page 152: COP 3503 FALL 2012 Shayan Javed Lecture 7

152 / 162

The Cloneable interface

public Book implements Cloneable {String title;int popularity;double price, avgCustomerReview;Date publicationDate;

public Object clone() throws CloneNotSupportedException {return super.clone();

}}

Page 153: COP 3503 FALL 2012 Shayan Javed Lecture 7

153 / 162

The Cloneable interface

Will that create a shallow or deep copy?

Page 154: COP 3503 FALL 2012 Shayan Javed Lecture 7

154 / 162

The Cloneable interface

Will that create a shallow or deep copy?

Shallow – just calling super.clone() creates a shallow copy.

Page 155: COP 3503 FALL 2012 Shayan Javed Lecture 7

155 / 162

The Cloneable interface

Will that create a shallow or deep copy?

Shallow – just calling super.clone() creates a shallow copy.

Need to clone references properly to create a deep copy

Page 156: COP 3503 FALL 2012 Shayan Javed Lecture 7

156 / 162

Shallow copy

Book1double price

double avg

Date publicationDate

Book2

String title

double price

double avg

Shallow copy

String title

Memory

Page 157: COP 3503 FALL 2012 Shayan Javed Lecture 7

157 / 162

The Cloneable interface

public Book implements Cloneable {Date publicationDate;

public Object clone() throws CloneNotSupportedException {

}}

Page 158: COP 3503 FALL 2012 Shayan Javed Lecture 7

158 / 162

The Cloneable interface

public Book implements Cloneable {Date publicationDate;

public Object clone() throws CloneNotSupportedException { Book book = (Book)super.clone(); // shallow copy

first

}}

Page 159: COP 3503 FALL 2012 Shayan Javed Lecture 7

159 / 162

The Cloneable interface

public Book implements Cloneable {Date publicationDate;

public Object clone() throws CloneNotSupportedException { Book book = (Book)super.clone(); // shallow copy

first book.publicationDate =

(Date)publicationDate.clone(); // deep copy

}}

Page 160: COP 3503 FALL 2012 Shayan Javed Lecture 7

160 / 162

The Cloneable interface

public Book implements Cloneable {Date publicationDate;

public Object clone() throws CloneNotSupportedException { Book book = (Book)super.clone(); // shallow copy

first book.publicationDate =

(Date)publicationDate.clone(); // deep copy return book;

}}

Page 161: COP 3503 FALL 2012 Shayan Javed Lecture 7

161 / 162

The Cloneable interface

All mutable fields should be cloned for a deep copy

Page 162: COP 3503 FALL 2012 Shayan Javed Lecture 7

162 / 162

The Cloneable interface

All mutable fields should be cloned for a deep copy

Strings are immutable, so no need to clone them separately.