Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us...
Transcript of Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us...
![Page 1: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/1.jpg)
Module 8: Abstract Data Types (ADTs)
Copyright Ron K. Cytron 2013
Ron K. Cytron *
Prepared for 2u
Semester Online
Department of Computer Science and Engineering Washington University in Saint Louis
Thanks to Alan Waldman for comments that improved these slides
*
![Page 2: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/2.jpg)
8.0 Introduction
• Objects allow us to bundle related functionality into a single type – Vector, Point, Color
• For some data types, we may have: – A formal definition of allowable behaviors – Differing implementations of those behaviors
• The formal definition is abstract – We can see the signatures of allowable behaviors – But the implementation is not there at all – In Java, these are interfaces or abstract classes
• A concrete class can then implement the interface • In this module we look at some common ADTs:
– List – Set – Map
• We use these as implemented in Java for this module • We implement them ourselves in a later module
2
Monolog 0
End of Monologue
![Page 3: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/3.jpg)
8.1 Lists
• A list is – An ordered collection of elements – Duplication of elements is allowed in a list
• Analogies – Things I need to do today
3
Oyster 1
![Page 4: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/4.jpg)
8.1 Lists
• A list is – An ordered collection of elements – Duplication of elements is allowed in a list
• Analogies – Things I need to do today
• Eat, study, eat, study, exercise, eat, sleep
4
![Page 5: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/5.jpg)
8.1 Lists
• A list is – An ordered collection of elements – Duplication of elements is allowed in a list
• Analogies – Things I need to do today
• Eat, study, eat, study, exercise, eat, sleep – Notice the duplication
5
![Page 6: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/6.jpg)
8.1 Lists
• A list is – An ordered collection of elements – Duplication of elements is allowed in a list
• Analogies – Things I need to do today
• Eat, study, eat, study, exercise, eat, sleep – Notice the duplication – Notice the order is important
» Exercise before I eat » Sleep is the last thing I do
6
![Page 7: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/7.jpg)
8.1 Lists
• A list is – An ordered collection of elements – Duplication of elements is allowed in a list
• Analogies – Things I need to do today
• Eat, study, eat, study, exercise, eat, sleep – Notice the duplication – Notice the order is important
– Errands I need to run • Get food, drop food off at neighbor's, pick up mail
– No duplication, but order is important
7
![Page 8: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/8.jpg)
8.1 Lists
• A list is – An ordered collection of elements – Duplication of elements is allowed in a list
• Analogies – Things I need to do today
• Eat, study, eat, study, exercise, eat, sleep – Notice the duplication – Notice the order is important
– Errands I need to run • Get food, drop food off at neighbor's, pick up mail
– No duplication, but order is important – Cannot drop off food until it is first obtained
8
![Page 9: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/9.jpg)
8.1 Lists
• A list of what? – Colors, Strings, doubles, Accounts?
• List is a parametric type – It is parameterized by the kind of thing in the list
• Syntax: – List<Color> colors;!– List<String> names;!– List<Double> values;!– List<Account> customers;!
• Pronunciation – List of Doubles – List of Strings – List of Doubles – List of Accounts
9
![Page 10: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/10.jpg)
8.1 Lists
• A list of what? – Colors, Strings, doubles, Accounts?
• List is a parametric type – It is parameterized by the kind of thing in the list
• Syntax: – List<Color> colors;!– List<String> names;!– List<Double> values;!– List<Account> customers;!
• Pronunciation – List of Doubles – List of Strings – List of Doubles – List of Accounts
10
New syntax
List<T>!
![Page 11: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/11.jpg)
8.1 Lists
• A list of what? – Colors, Strings, doubles, Accounts?
• List is a parametric type – It is parameterized by the kind of thing in the list
• Syntax: – List<Color> colors;!– List<String> names;!– List<Double> values;!– List<Account> customers;!
• Pronunciation – List of Doubles – List of Strings – List of Doubles – List of Accounts
11
We would normally use double for this type, but for Lists and such, we need the "object" form of double, which is spelled Double!
![Page 12: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/12.jpg)
8.1 Lists
• A list of what? – Colors, Strings, doubles, Accounts?
• List is a parametric type • A List<T> contains elements only of type T!
– As we learn later, T can be a general type – Example: List<Number> can contain
• Double, Integer, Long, etc. – We say that Number is polymorphic
• Many-shaped • Could be Double, Integer, Long, etc.
• Object is the most general type of all – So, List<Object> can contain any type of object
• For now, we will use a very specific type – But we call it type T, where T is the parametric type
12
![Page 13: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/13.jpg)
8.1 Lists
• A list of what? – Colors, Strings, doubles, Accounts?
• List is a parametric type • A List<T> contains elements only of type T!
– As we learn later, T can be a general type – Example: List<Number> can contain
• Double, Integer, Long, etc. – We say that Number is polymorphic
• Many-shaped • Could be Double, Integer, Long, etc.
• Object is the most general type of all – So, List<Object> can contain any type of object
• For now, we will use a very specific type – But we call it type T, where T is the parametric type
13
![Page 14: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/14.jpg)
8.1 Lists
• A list of what? – Colors, Strings, doubles, Accounts?
• List is a parametric type • A List<T> contains elements only of type T!
– As we learn later, T can be a general type – Example: List<Number> can contain
• Double, Integer, Long, etc. – We say that Number is polymorphic
• Many-shaped • Could be Double, Integer, Long, etc.
• Object is the most general type of all – So, List<Object> can contain any type of object
• For now, we will use a very specific type – But we call it type T, where T is the parametric type
14
![Page 15: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/15.jpg)
8.1 Lists
• A list of what? – Colors, Strings, doubles, Accounts?
• List is a parametric type • A List<T> contains elements only of type T!
– As we learn later, T can be a general type – Example: List<Number> can contain
• Double, Integer, Long, etc. – We say that Number is polymorphic
• Many-shaped • Could be Double, Integer, Long, etc.
• Object is the most general type of all – So, List<Object> can contain any type of object
• For now, we will use a very specific type – But we call it type T, where T is the parametric type
15
![Page 16: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/16.jpg)
8.1 Lists
• A list of what? – Colors, Strings, doubles, Accounts?
• List is a parametric type • A List<T> contains elements only of type T!
– As we learn later, T can be a general type – Example: List<Number> can contain
• Double, Integer, Long, etc. – We say that Number is polymorphic
• Many-shaped • Could be Double, Integer, Long, etc.
• Object is the most general type of all – So, List<Object> can contain any type of object
• For now, we will use a very specific type – But we call it type T, where T is the parametric type
16 End of Oyster
![Page 17: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/17.jpg)
Roundtable • What are some examples of Lists?
– What is the type of the things on the list • Examples
– List of books I read this summer, in order of their completion (String) – List of hourly temperature readings
• Pause and ask student • Double
– List of Roulette Wheel results • Pause and ask student • Integer
• Where would the following be useful? – List of Boolean – Pause – Heads tails results
• Back to a list of things to do – What kinds of things would you want to do with a list? – Ask student – Live response at roundtable
17
Roundtable
End of Roundtable
![Page 18: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/18.jpg)
8.1 Lists
• If we have a list, what can we do to it – Add things to the list – Find the nth thing on the list – Find out where some element occurs on the list – Remove something from the list – Find out the length of the list
• No matter how the List is implemented, we should be able to perform these operations to any List – Specific implementations may differ in how they
carry out the above operations • Let's define the interface, and then use some
specific implementations of it
18
Oyster 1b
![Page 19: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/19.jpg)
8.1 Lists
• If we have a list, what can we do to it – Add things to the list – Find the nth thing on the list – Find out where some element occurs on the list – Remove something from the list – Find out the length of the list
• No matter how the List is implemented, we should be able to perform these operations to any List – Specific implementations may differ in how they
carry out the above operations • Let's define the interface, and then use some
specific implementations of it
19
![Page 20: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/20.jpg)
8.1 Lists
• If we have a list, what can we do to it – Add things to the list – Find the nth thing on the list – Find out where some element occurs on the list – Remove something from the list – Find out the length of the list
• No matter how the List is implemented, we should be able to perform these operations to any List – Specific implementations may differ in how they
carry out the above operations • Let's define the interface, and then use some
specific implementations of it
20
![Page 21: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/21.jpg)
8.1 Lists
• If we have a list, what can we do to it – Add things to the list – Find the nth thing on the list – Find out where some element occurs on the list – Remove something from the list – Find out the length of the list
• No matter how the List is implemented, we should be able to perform these operations to any List – Specific implementations may differ in how they
carry out the above operations • Let's define the interface, and then use some
specific implementations of it
21
![Page 22: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/22.jpg)
8.1 Lists
• If we have a list, what can we do to it – Add things to the list – Find the nth thing on the list – Find out where some element occurs on the list – Remove something from the list – Find out the length of the list
• No matter how the List is implemented, we should be able to perform these operations to any List – Specific implementations may differ in how they
carry out the above operations • Let's define the interface, and then use some
specific implementations of it
22
![Page 23: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/23.jpg)
8.1 Lists
• If we have a list, what can we do to it – Add things to the list – Find the nth thing on the list – Find out where some element occurs on the list – Remove something from the list – Find out the length of the list
• No matter how the List is implemented, we should be able to perform these operations to any List – Specific implementations may differ in how they
carry out the above operations • Let's define the interface, and then use some
specific implementations of it
23
![Page 24: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/24.jpg)
8.1 Lists
• If we have a list, what can we do to it – Add things to the list – Find the nth thing on the list – Find out where some element occurs on the list – Remove something from the list – Find out the length of the list
• No matter how the List is implemented, we should be able to perform these operations to any List – Specific implementations may differ in how they
carry out the above operations • Let's define the interface, and then use some
specific implementations of it
24
![Page 25: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/25.jpg)
public interface List<T> {!
! !!
}!
8.1 Lists
25
– Uses interface instead of class!
![Page 26: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/26.jpg)
public interface List<T> {!
! !!
}!
8.1 Lists
26
– Uses interface instead of class!
– Has the parametric type in the class name
![Page 27: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/27.jpg)
public interface List<T> {!
! !!
}!
8.1 Lists
27
– No constructors! – Because this is not
really a class, it just defines the signatures of available methods
![Page 28: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/28.jpg)
public interface List<T> {!
! !!
}!
8.1 Lists
28
– No constructors! – Because this is not
really a class, it just defines the signatures of available methods
– No instance variables – Because no
implementations of methods are here to remember things
![Page 29: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/29.jpg)
public interface List<T> {!
public boolean add(T e);!
}!
8.1 Lists
29
– Add element e to the end of this list – Always returns true
for a list
![Page 30: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/30.jpg)
public interface List<T> {!
public boolean add(T e);!
!public T get(int i);!
}!
8.1 Lists
30
– Return the ith element in this list
![Page 31: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/31.jpg)
public interface List<T> {!
public boolean add(T e);!
!public T get(int i);!
!public int indexOf(T e);!
}!
8.1 Lists
31
– Returns the index at which the specified element first occurs in the list – 0 is the index of the
first element
![Page 32: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/32.jpg)
public interface List<T> {!
public boolean add(T e);!
!public T get(int i);!
!public int indexOf(T e);!
!public boolean remove(T e);!
}!
8.1 Lists
32
– Remove the first occurrence of the specified element
![Page 33: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/33.jpg)
public interface List<T> {!
public boolean add(T e);!
!public T get(int i);!
!public int indexOf(T e);!
!public boolean remove(T e);!
!public T remove(int i);!
}!
8.1 Lists
33
– Remove the first occurrence of the specified element
– Remove (and return) the element at the ith index in the list
![Page 34: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/34.jpg)
public interface List<T> {!
public boolean add(T e);!
!public T get(int i);!
!public int indexOf(T e);!
!public boolean remove(T e);!
!public T remove(int i);!
!public int size();!
}!
8.1 Lists
34
– Return the number of elements in this list
![Page 35: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/35.jpg)
public interface List<T> {!
public boolean add(T e);!
!public T get(int i);!
!public int indexOf(T e);!
!public boolean remove(T e);!
!public T remove(int i);!
!public int size();!
}!
8.1 Lists
35
These operations, and many more, define the ADT (Abstract Data Type) of a List!
(java.util.List)
![Page 36: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/36.jpg)
8.1 Lists
• Interface – Defines the signatures of the methods we want
• "What" not "How" • No instance variables, constructors • Design without implementation
– So that alternative implementations can satisfy the interface
• An implementation – Says it "implements" the interface – Must have actual implementations for all methods – If it is missing any, it cannot be instantiated
36
![Page 37: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/37.jpg)
8.1 Lists
• So we have an interface • What implementations exist? (easy to see online)
– ArrayList • Places elements in an array • Insertion and deletion are relatively awkward • Good choice if the list is relatively fixed • Example: List of America's National Parks
– Does not change frequently – Deletion is rare to non-existant
– LinkedList • Grows and shrinks with ease • Uses "links" between its elements • Insertion and deletion are relatively easy • Example: People waiting in line to ride a roller coaster
– Mostly the list grows, but it grows frequently – Occasionally people drop out—scared or don't want to wait
• Choosing the right data structure is important – A more rigorous, mathematical treatment awaits you in a course on
Analysis of Algorithms
37
![Page 38: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/38.jpg)
List<String> eating = new LinkedList<String>();!
8.1 Lists
38
– We declare eating as a variable of type List<String>!– We can then invoke any method defined in the List interface on eating!
– We could have declared eating to be a LinkedList<String> and the program would have worked just as well
– But List<String> is more general, so we should use that when possible
![Page 39: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/39.jpg)
List<String> eating = new LinkedList<String>();!
8.1 Lists
39
– Here, we cannot say new List<String>()!– Because List is an interface, not a concrete
class – Interfaces cannot be constructed
– So on the right-hand-side, we must assign some object of type List<String> or narrower
– Because LinkedList<String> implements List<String>, it serves just fine
![Page 40: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/40.jpg)
List<String> eating = new ArrayList<String>();!
8.1 Lists
40
– Or we could use an ArrayList!– Because it implements the List interface – Plug-compatible in terms of functionality – But time / space behavior may be different
![Page 41: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/41.jpg)
List<String> eating = new LinkedList<String>();!
eating.add("open mouth");!eating.add("insert food");!eating.add("chew");!eating.add("chew");!eating.add("swallow");!
System.out.println(eating);!
8.1 Lists
41 End of Oyster
![Page 42: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/42.jpg)
8.2 Exercise
• Investigate the ADT Set<T> and its concrete implementation HashSet<T>!
• How does it differ from List? • What do Set and List have in common • Try
– Set<String> eating=new HashSet<String>();!• As before, add the following
eating.add("open mouth");!eating.add("insert food");!eating.add("chew");!eating.add("chew");!eating.add("swallow");!
• Print out the set, and what do you see? 42
BLT public 2
![Page 43: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/43.jpg)
8.2 Exercise
• Video response – Sets do not allow duplicates – The order of adding elements does not matter
43 End of BLT
![Page 44: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/44.jpg)
8.3 Object equality
44
Oyster 3a
• For primitive types, equality is simple – double, int, boolean!– These are equal if they have the same value
• For objects, equality is not as simple. • By equality of a and b, do we mean
• a and b are the exact same object? • a and b are equal in a deeper sense of the concept?
![Page 45: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/45.jpg)
8.3 Object equality
45
By equality of a and b, do we mean – a and b are the exact same object?
5 10
a!
b!
![Page 46: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/46.jpg)
8.3 Object equality
• Let's look at a simple class that represnts an (x,y) pair – A Point
public class Point {!!private int x, y;!
…!
}!
46
5 10
![Page 47: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/47.jpg)
8.3 Object equality
• Let's look at a simple class that represnts an (x,y) pair – A Point – The one shown to the right is (5,10)
public class Point {!!private int x, y;!
…!
}!
47
5 10
![Page 48: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/48.jpg)
8.3 Object equality
48
By equality of a and b, do we mean – a and b are the exact same object? – a and b are equal in a deeper sense of the concept?
5 10
a!
5 10
b!
![Page 49: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/49.jpg)
8.3 Object equality
49
By equality of a and b, do we mean – a and b are the exact same object?
• for this, we test a == b!
5 10
a!
b!
![Page 50: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/50.jpg)
8.3 Object equality
50
By equality of a and b, do we mean – a and b are equal in a deeper sense of the concept?
• for this, we test a.equals(b)!
5 10
a!
5 10
b!
![Page 51: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/51.jpg)
8.3 Object equality
51
• All objects have the method – boolean equals(Object other)!– Just as all objects have String toString()!
• The default implementation is simply: public boolean equals(Object other) {!!return this == other;!}!• The above tests whether the two objects are
exactly the same object – In other words, the live at the same address in the
heap
![Page 52: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/52.jpg)
8.3 Object equality
52
• But we can always override this definition and give our own meaning to equality
• For a String object, this is based on whether the strings: – Have the same length – Match, character-by-character
• For a Color object, equality is (essentially) based on whether the two objects have the same values for: – red – green – blue
• Because – a=Color.BLACK !
• has the same amount of red, green, and blue as – b=new Color(0,0,0),
• a.equals(b) is true
![Page 53: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/53.jpg)
8.3 Object equality
53
• Java objects we have used so far have reasonable definitions for – boolean equals(Object o)!
• Object such as – Color!– String!– Double!– Integer!
• The Set implementation uses an object's .equals(Object) methods to tell whether an element is already in a Set!
![Page 54: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/54.jpg)
8.3 Object equality
54
• Contract for boolean equals(Object o)!– reflexive
• x "is as tall as" x!• x "has same name as" x
– symmetric • if a.equals(b) then b.equals(a)!
– transitive • if a.equals(b) and b.equals(c)
– then a.equals(c)!– consistent
• a.equals(b) returns the same result if invoked multiple times, unless something changes about a or b!
![Page 55: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/55.jpg)
8.3 Object equality
55
• Contract for boolean equals(Object o)!– reflexive
• a.equals(a) is always true – symmetric
• if a.equals(b) then b.equals(a)!– transitive
• if a.equals(b) and b.equals(c) – then a.equals(c)!
– consistent • a.equals(b) returns the same result if invoked
multiple times, unless something changes about a or b!
![Page 56: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/56.jpg)
8.3 Object equality
56
• Contract for boolean equals(Object o)!– reflexive
• a.equals(a) is always true – symmetric
• If you "are a sister of" Sue, Sue "is a sister of" you • x "same color as" y !
– then y "same color as " x!– transitive
• if a.equals(b) and b.equals(c) – then a.equals(c)!
– consistent • a.equals(b) returns the same result if invoked
multiple times, unless something changes about a or b!
![Page 57: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/57.jpg)
8.3 Object equality
57
• Contract for boolean equals(Object o)!– reflexive
• a.equals(a) is always true – symmetric
• if a.equals(b) then b.equals(a)!– transitive
• if a.equals(b) and b.equals(c) – then a.equals(c)!
– consistent • a.equals(b) returns the same result if invoked
multiple times, unless something changes about a or b!
![Page 58: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/58.jpg)
8.3 Object equality
58
• Contract for boolean equals(Object o)!– reflexive
• a.equals(a) is always true – symmetric
• if a.equals(b) then b.equals(a)!– transitive
• if a "same height as" b!• and b "same height as" c!• then a "same height as" c!
– consistent • a.equals(b) returns the same result if invoked
multiple times, unless something changes about a or b!
![Page 59: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/59.jpg)
8.3 Object equality
59
• Contract for boolean equals(Object o)!– reflexive
• a.equals(a) is always true – symmetric
• if a.equals(b) then b.equals(a)!– transitive
• if a.equals(b) and b.equals(c) – then a.equals(c)!
– consistent • a.equals(b) returns the same result if invoked
multiple times, unless something changes about a or b!
![Page 60: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/60.jpg)
8.3 Object equality
60
• Contract for boolean equals(Object o)!– reflexive
• a.equals(a) is always true – symmetric
• if a.equals(b) then b.equals(a)!– transitive
• if a.equals(b) and b.equals(c) – then a.equals(c)!
– consistent • a.equals(b) returns the same result if invoked
multiple times, unless something changes about a or b!
![Page 61: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/61.jpg)
8.3 Object equality
61
• Contract for boolean equals(Object o)!– reflexive
• a.equals(a) is always true – symmetric
• if a.equals(b) then b.equals(a)!– transitive
• if a.equals(b) and b.equals(c) – then a.equals(c)!
– consistent • a.equals(b) returns the same result if invoked
multiple times, unless something changes about a or b!
What would be an inconsistent implementation?
public boolean equals(Object o) {!
!return Math.random() < 0.5;!
}!
![Page 62: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/62.jpg)
8.3 Object equality
62
• These properties make sense for objects we consider in everyday life – Color!– String!
• Moreover – a.equals(null) is always false!– Objects of different concrete types are always different
• if a is a Color and b is a String!– a.equals(b) is always false!
• Eclipse can generate the equals method automatically – We just have to say which fields matter in terms of
equality. This should be considered as we design objects.
![Page 63: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/63.jpg)
8.3 Object equality
63
• These properties make sense for objects we consider in everyday life – Color!– String!
• Moreover – a.equals(null) is always false!– Objects of different concrete types are always different
• if a is a Color and b is a String!– a.equals(b) is always false!
• Eclipse can generate the equals method automatically – We just have to say which fields matter in terms of
equality. This should be considered as we design objects.
![Page 64: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/64.jpg)
8.3 Object equality
64
• These properties make sense for objects we consider in everyday life – Color!– String!
• Moreover – a.equals(null) is always false!– Objects of different concrete types are always different
• if a is a Color and b is a String!– a.equals(b) is always false!
• Eclipse can generate the equals method automatically – We just have to say which fields matter in terms of
equality. This should be considered as we design objects.
![Page 65: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/65.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
65
![Page 66: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/66.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
66
Because
a.equals(a)!
should always be true!
![Page 67: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/67.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
67
Because
a.equals(null)!
should always be false!
![Page 68: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/68.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
68
Because objects of differing types should never equal each other
this.!
![Page 69: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/69.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
69
If we reach this point, then the parameter obj is also a Point object. This cast lets us treat it as such under the name other!
![Page 70: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/70.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
70
If we reach this point, then the parameter obj is also a Point object. This cast lets us treat it as such under the name other. We told eclipse that we want equality based on the instance variables x and y!
![Page 71: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/71.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
71
If we reach this point, then the parameter obj is also a Point object. This cast lets us treat it as such under the name other. We told eclipse that we want equality based on the instance variables x and y!
this.!
![Page 72: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/72.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
72
If we reach this point, then the parameter obj is also a Point object. This cast lets us treat it as such under the name other. We told eclipse that we want equality based on the instance variables x and y!
this.!
![Page 73: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/73.jpg)
public class Point {! private int x, y;!! // Generated automatically by eclipse!
public boolean equals(Object obj) {!! if (this == obj) return true;!! if (obj == null) return false;!! if (getClass() != obj.getClass()) return false;!! Point other = (Point) obj;!! if (x != other.x) return false;!! if (y != other.y) return false;!! return true;!
}!
}!
8.3 Object equality
73
If we reach this point, then the parameter obj is also a Point object. This cast lets us treat it as such under the name other. We told eclipse that we want equality based on the instance variables x and y!
If we make it to this point, the two objects (this and other) agree on their x and y values, so we return true!
End of Oyster
![Page 74: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/74.jpg)
8.3 Object hashCode
• Suppose you have a messy house and it takes you forever to find something
74
0 1 2 3 4 5
Oyster 3b
![Page 75: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/75.jpg)
8.3 Object hashCode
• Suppose you have a messy house and it takes you forever to find something
• One possible strategy is to get some bins – A given object (say, your cell phone) is always put in
the same bucket so it is easier to find – Requires remembering the bucket in which the phone
goes
75
0 1 2 3 4 5
phone
![Page 76: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/76.jpg)
8.3 Object hashCode
• Suppose you have a messy house and it takes you forever to find something
• One possible strategy is to get some bins – A given object (say, your cell phone) is always put in
the same bucket so it is easier to find – Requires remembering the bucket in which the phone
goes – But cuts the search space down to just 1/6 buckets
76
0 1 2 3 4 5
phone
![Page 77: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/77.jpg)
8.3 Object hashCode
• Suppose you have a messy house and it takes you forever to find something
• One possible strategy is to get some bins • Java has this concept implemented for every
object – Called an object's hashCode()!– Maps an object to a (possibly large) integer
77
0 1 2 3 4 5
phone
![Page 78: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/78.jpg)
8.3 Object hashCode
• Suppose you have a messy house and it takes you forever to find something
• One possible strategy is to get some bins • Java has this concept implemented for every
object – phone.hashCode() % 6 computes which bucket
78
0 1 2 3 4 5
phone
![Page 79: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/79.jpg)
8.3 Object hashCode
• Bigger picture – phone.hashCode() % 6 computes which bucket
79
0 1 2 3 4 5
phone
![Page 80: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/80.jpg)
8.3 Object hashCode
• Bigger picture – phone.hashCode() % 6 computes which bucket – phone.equals(thing) is then applied to each thing in that bucket to see if the phone is really there
80
Bucket 2
![Page 81: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/81.jpg)
8.3 Object hashCode
• Bigger picture – phone.hashCode() % 6 computes which bucket – phone.equals(thing) is then applied to each thing in that bucket to see if the phone is really there
81
phone
Taj Mahal
pencil
moss-covered 3-handled family credenza
Bucket 2
![Page 82: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/82.jpg)
8.3 Object hashCode
• Bigger picture – phone.hashCode() % 6 computes which bucket – phone.equals(thing) is then applied to each thing in that bucket to see if the phone is really there
82
phone
Taj Mahal
pencil
moss-covered 3-handled family credenza
Bucket 2
![Page 83: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/83.jpg)
8.3 Object hashCode
• Bigger picture – phone.hashCode() % 6 computes which bucket – phone.equals(thing) is then applied to each thing in that bucket to see if the phone is really there
83
phone
Taj Mahal
pencil
moss-covered 3-handled family credenza
Bucket 2
![Page 84: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/84.jpg)
8.3 Object hashCode
• Bigger picture – phone.hashCode() % 6 computes which bucket – phone.equals(thing) is then applied to each thing in that bucket to see if the phone is really there
84
phone moss-covered 3-handled family credenza
Bucket 2
Taj Mahal
pencil
![Page 85: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/85.jpg)
8.3 Object hashCode
85
• Let's consider a Point object – whose hashCode() is generated based on its x and y coordinates
0 1 2 3 4 5
(3,4) .hashCode()!
![Page 86: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/86.jpg)
8.3 Object hashCode
86
• Some possible hashCode() implementations – All objects map to same bucket, not good
public int hashCode() {!!return 0;!
}!
0 1 2 3 4 5
(4,3) .hashCode()!
![Page 87: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/87.jpg)
8.3 Object hashCode
87
• Some possible hashCode() implementations – All objects map to same bucket, not good
public int hashCode() {!!return 0;!
}!
0 1 2 3 4 5
(3,4) .hashCode()!(4,3) .hashCode()!
![Page 88: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/88.jpg)
8.3 Object hashCode
88
• Some possible hashCode() implementations – All objects map to same bucket, not good
public int hashCode() {!!return 0;!
}!
0 1 2 3 4 5
(3,4) .hashCode()!(4,3) .hashCode()!
(8,2) .hashCode()!
![Page 89: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/89.jpg)
8.3 Object hashCode
89
• Some possible hashCode() implementations – How about this?
public int hashCode() {!!return x+y;!
}!
0 1 2 3 4 5
(3,4) .hashCode()!(4,3) .hashCode()!
(8,2) .hashCode()!
![Page 90: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/90.jpg)
8.3 Object hashCode
90
• Some possible hashCode() implementations – How about this?
public int hashCode() {!!return x+y;!
}!
0 1 2 3 4 5
(3,4) .hashCode()!(4,3) .hashCode()!
(8,2) .hashCode()!
![Page 91: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/91.jpg)
8.3 Object hashCode
91
• Some possible hashCode() implementations – How about this? Better, but (x,y) and (y,x) end up in
same bucket
public int hashCode() {!!return x+y;!
}!
0 1 2 3 4 5
(3,4) .hashCode()!(4,3) .hashCode()!
(8,2) .hashCode()!
![Page 92: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/92.jpg)
8.3 Object hashCode
92
• Some possible hashCode() implementations – How about this one? Spreads out the objects much
more evenly
public int hashCode() {!!return 31*x + 17*y;!
}!
0 1 2 3 4 5
(3,4) .hashCode()!
(4,3) .hashCode()!(8,2) .hashCode()!
![Page 93: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/93.jpg)
8.3 Object hashCode
93
• Some possible hashCode() implementations – How about this one? Spreads out the objects much
more evenly
public int hashCode() {!!return 31*x + 17*y;!
}!
0 1 2 3 4 5
(3,4) .hashCode()!
(4,3) .hashCode()!(8,2) .hashCode()!
![Page 94: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/94.jpg)
8.3 Object hashCode
94
• Some possible hashCode() implementations – How about this one? Spreads out the objects much
more evenly
public int hashCode() {!!return 31*x + 17*y;!
}!
0 1 2 3 4 5
(3,4) .hashCode()!
(4,3) .hashCode()!(8,2) .hashCode()!
![Page 95: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/95.jpg)
8.3 Object hashCode
95
• Impossible to do a perfect job – Many points and only 6 buckets
0 1 2 3 4 5
![Page 96: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/96.jpg)
8.3 Object hashCode
96
• Impossible to do a perfect job – Many points and only 6 buckets
0 1 2 3 4 5
(3,4)
(4,3)
(8,2)
End of Oyster
(5,13)
(7,11)
(51,2)
(51,2)
![Page 97: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/97.jpg)
8.4 Common mistake with equals
97
Roundtable 4
• Code doesn't work because equals was improperly defined by hand – wrong signature – also no hashCode override
• To override a method, its signature must match exactly
• Show student the right thing to do – Say what you want to count for equality – Use eclipse to generate the methods
![Page 98: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/98.jpg)
8.5 Lists and Sets of Points
• Video intro: use Lists and Sets now but of Point objects
• Make a point object with instance variables – x and y, both integers
• Use eclipse to generate – .equals (and it also generates hashCode – this is fine)
• Add different points to the List and Set, but with some duplicates added on purpose
• What do you see? • Response goes over what they should see
– Say something about hashCode
98
BLT public 5a
End of BLT
![Page 99: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/99.jpg)
8.5 A StockHolding object
• Video intro: another object, and we'll let eclipse generate equals and hashCode
• A Stock has-a – String identifying its code (final) – String identifying its owner (who holds the stock)
(final) – A number of shares (integer), may change over time – A price per share (integer), may change over time
• Question card: – On what should equality be based? – Code this up and generate its equals and hashCode
using eclipse • Response: show my solution
99
BLT public 5b
End of BLT
![Page 100: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/100.jpg)
8.6 Interfaces for behavioral abstraction
• We saw that List<T> is an interface – From the top-down
• List<T> defines what we want a list to do – add, remove, size, etc.
– From the bottom-up • List<T> defines the features common among its
implementations – LinkedList, ArrayList, etc.
• We next look at creating the interface from separate objects – So as to leverage the common behaviors of those
objects
100
Oyster 6
![Page 101: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/101.jpg)
8.6 Interfaces for behavioral abstraction
• Object 1 is a BankAccount – We have seen this previously
• deposit, withdraw, getBalance
• Object 2 is a StockHolding – has-a
• number of shares (does not change) • price per share (can change over time)
– behaviors • change the price per share, get current value
• These two objects are fine as separate objects
101
![Page 102: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/102.jpg)
8.6 Interfaces for behavioral abstraction
• Object 1 is a BankAccount – We have seen this previously
• deposit, withdraw, getBalance
• Object 2 is a StockHolding – has-a
• number of shares (does not change) • price per share (can change over time)
– behaviors • change the price per share, get current value
• These two objects are fine as separate objects – Now suppose I want to find out your current liquid
worth in dollars based on stocks and bank accounts 102
![Page 103: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/103.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(thing1);!assets.add(thing2);!// assets is a list of!// all of our stock holdings and bank accounts!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
103
Construct an empty list of objects that implement the Valuable interface. This list is actually a LinkedList of such objects.!
What should it mean to be Valuable? It means that we can find out the current value of the object (say, in dollars), whether it is a BankAccount or a StockHolding.!
We will insist that BankAccount and StockHolding objects implement the Valuable interface.
![Page 104: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/104.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!// assets is a list of!// all of our stock holdings and bank accounts!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
104
We can add anything that implements the Valuable interface to this list
Here, we add a BankAccount that starts with 100 dollars
![Page 105: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/105.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!// assets is a list of!// all of our stock holdings and bank accounts!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
105
We can add anything that implements the Valuable interface to this list
Here, we add a BankAccount that starts with 100 dollars and a StockHolding of 5 shares at initial value 10 dollars per share
![Page 106: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/106.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
106
We can now iterate over the list, adding up the current liquid value of each element
![Page 107: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/107.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
107
We can now iterate over the list, adding up the current liquid value of each element. This loop starts at 0 and goes up to but not including the size of the list.
![Page 108: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/108.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
108
We retrieve element i from the list, and it implements the Valuable interface, whether it is a BankAccount or a StockHolding object
![Page 109: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/109.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
109
We add to worth the value of this asset, so that the loop causes worth to eventually contain the value of all of the assets
![Page 110: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/110.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
110
Finally we can print the result of the computation
![Page 111: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/111.jpg)
8.6 Interfaces for behavioral abstraction
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (int i=0; i<assets.size(); ++i) {!
Valuable asset = assets.get(i);!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
111
Java offers a cleaner way to state this iteration.
![Page 112: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/112.jpg)
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (Valuable asset : assets) {!
//!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
8.6 Interfaces for behavioral abstraction
112
Java offers a cleaner way to state this iteration. Objects such as List and Set offer an iterator that can be used in this way.
![Page 113: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/113.jpg)
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (Valuable asset : assets) {!
//!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
8.6 Interfaces for behavioral abstraction
113
This declares a new variable asset, of type Valuable!
![Page 114: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/114.jpg)
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (Valuable asset : assets) {!
//!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
8.6 Interfaces for behavioral abstraction
114
This declares a new variable asset, of type Valuable, which takes on each element of the assets list in turn!
![Page 115: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/115.jpg)
• Why? • Goal:
List<Valuable> assets=new LinkedList<Valuable>();!assets.add(new BankAccount(100));!assets.add(new StockHolding(5, 10));!int worth = 0;!for (Valuable asset : assets) {!
//!worth += asset.getLiquidValue();!
}!System.out.println("You now have $" + worth);!
8.6 Interfaces for behavioral abstraction
115
For a List, the elements will be retrieved in their list order. If this were a Set, there is no implied ordering of its elements.!
![Page 116: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/116.jpg)
8.6 Interfaces for behavioral abstraction
• How do we achieve this goal? • BankAccount offers
– getBalance()!• StockHolding offers
– getCurrentValue()!• So we define a new method
– getLiquidValue()!• in terms of what each object offers
• And we add the interface – Valuable!
• offers the method getLiquidValue()!• Both objects then implement that interface
116
![Page 117: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/117.jpg)
8.6 Interfaces for behavioral abstraction
117
BankAccount!
int getBalance()!
deposit(int)!
boolean withdraw(int)!
![Page 118: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/118.jpg)
8.6 Interfaces for behavioral abstraction
118
BankAccount!deposit(int)!
StockHolding!setPricePerShare(int)!
boolean withdraw(int)!
int getBalance()!
int getCurrentValue()!
![Page 119: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/119.jpg)
8.6 Interfaces for behavioral abstraction
119
BankAccount!deposit(int)!
StockHolding!
int getCurrentValue()!
setPricePerShare(int)!
boolean withdraw(int)!
int getBalance()!
int getLiquidValue()!
int getLiquidValue()!
![Page 120: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/120.jpg)
8.6 Interfaces for behavioral abstraction
120
BankAccount!deposit(int)!
StockHolding!
int getCurrentValue()!
setPricePerShare(int)!
boolean withdraw(int)!
int getBalance()!
int getLiquidValue()!
int getLiquidValue()!
![Page 121: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/121.jpg)
8.6 Interfaces for behavioral abstraction
121
BankAccount!deposit(int)!
StockHolding!
int getCurrentValue()!
setPricePerShare(int)!
boolean withdraw(int)!
int getBalance()!
int getLiquidValue()!
int getLiquidValue()!
![Page 122: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/122.jpg)
8.6 Interfaces for behavioral abstraction
122
BankAccount!deposit(int)!
StockHolding!
int getCurrentValue()!
setPricePerShare(int)!
boolean withdraw(int)!
int getBalance()!
int getLiquidValue()!
int getLiquidValue()!
Viewed as their own object types, all methods defined on an object are available, including the newly added method!
![Page 123: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/123.jpg)
8.6 Interfaces for behavioral abstraction
123
Valuable!
Valuable!
int getLiquidValue()!
int getLiquidValue()!
Viewed through the interface, only the interface's methods are available!
![Page 124: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/124.jpg)
8.6 Interfaces for behavioral abstraction
124
Although a and v both reference the same object – On a, we can call any method defined for a BankAccount!– On v, we can only call those methods defined for a Valuable!
100 BankAccount a!
Valuable v!
![Page 125: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/125.jpg)
8.6 Interfaces for behavioral abstraction
125
Although a and v both reference the same object – On a, we can call any method defined for a BankAccount!– On v, we can only call those methods defined for a Valuable!
100 BankAccount a!
Valuable v!
deposit, withdraw, getLiquidValue!
![Page 126: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/126.jpg)
8.6 Interfaces for behavioral abstraction
126
Although a and v both reference the same object – On a, we can call any method defined for a BankAccount!– On v, we can only call those methods defined for a Valuable!
100 BankAccount a!
Valuable v!
deposit, withdraw, getLiquidValue!
getLiquidValue!
End of Oyster
![Page 127: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/127.jpg)
8.6b Using the interface
• Define a class PersonalProperty!– has-a initialValue (integer) – has-a yearsOld (integer) – define a method int depreciatedValue()!
• returns the value according to the formula: • initialValue x (.80)yearsOld!
– and cast to an int value
– Remember • Math.pow(a,b) computes ab
• Question Card: adapt this class to implement Valuable!
• Response: show how this is done 127
BLT public 6b
End of BLT
![Page 128: Module 8: Abstract Data Types (ADTs)cytron/cse131/slides/8.pdf8.0 Introduction • Objects allow us to bundle related functionality into a single type – Vector, Point, Color •](https://reader035.fdocuments.in/reader035/viewer/2022070710/5ec737d653b23b74e212edbb/html5/thumbnails/128.jpg)
8.7 Conclusion
• ADTs are defined in terms of their behaviors – They allow for multiple implementations – Java interfaces express the common behaviors
• List, Set, Map are useful ADTs – We have see List in great detail – And Set in some detail – Map deserves further exploration
• Objects need to be able to compare themselves against other objects for equality – .equals method – contract – implentation (automatically using eclipse)
• Interfaces can be define bottom-up to extract a comon behavior of interest – Refactor existing code to introduce common methods – Capture common methods with an interface
128
Monolog 7
End of Monologue