Module 8 Exercises¶
Exercise : 8.25. Examples of a list¶
You do not write code in this exercise. Instead, consider each numbered question below and write down some responses that come to mind. You are at this point quite new to lists, so take your time and try to think of the best responses you can.
To record your work here, create and edit a new Java class ThinkingAboutLists
in the exercises8
package of your exercises
folder. You can enter the text in comments there.
What are some examples of lists? In the introductory video, I spoke about a list of books that I read this summer and a list of temperature readings. Those might be declared ast
List<String>
andList<Double>
, respectively. TheString
could represent the title of each book I read, and theDouble
could represent each temperature reading.
Think of possible applications of the other two types of lists covered in the video:
List<Color>
List<Account>
For each of the above, try to find an example where at least one of the following is true:
For each of the above, try to find an example where at least one of the following is true:
The order of elements in the list is important.
There can be duplicates in the list.
Suppose we spin a Roulette wheel some number of times, and we must report the numbers that show on the wheel when it stops. Note that:
The same number may appear multiple times.
The order of the numbers’ appearance is important.
Thus, we need a List<?>
, but what type of element should be in this list?
Can you think of a problem where having
List<Boolean>
would be useful?Suppose we have some kind of a
List
. What kinds of operations might we want to perform on thatList
?
Exercise : 8.33. Using List¶
Let’s try using the code from the end of the video. If you get stuck, look at the solution video, but only as far as it takes you to get unstuck, then try going forward on your own again.
To get started, open the UsingLists
class in the exercises8
package of the exercises
folder.
Some important points:
We declare the list using the abstract type
List
.We instantiate it using a concrete type, such as
LinkedList
orArrayList
.
For now, use LinkedList
.
IntelliJ will say it doesn’t know what those types are. You have to mouse-over the offensive code and allow IntelliJ to suggest what you should do.
The classes you want are found in java.util
so be sure to choose those.
The
List
needs a type parameter, which in this case should beString
.Using your list, add the following:
"open mouth"
"insert food"
"chew"
"chew"
"swallow"
Print the list and verify that it’s right
Add some more things to the list and print it again
Delete one thing and print it again
Once you are done, change the implementation from LinkedList
to ArrayList
, and verify that you see no difference in the program’s behavior (this part is not on the video).
Exercise : 8.35. Investigate the Set ADT¶
Often in designing or writing software, it helps to use code that others have written. We have done exactly that with the List
interface and its LinkedList
implementation.
In this exercise you will investigate the Set
interface using Javadoc documentation. You will then write code to use the Set
interface via its HashSet
implementation, and observe what happens with the eating
example from the previous exercise.
You will no doubt find some of the documentation confusing, as it refers to concepts we have not studied. Nonetheless, browsing such documentation is commonplace at all levels when searching for code that may be of use as you develop software.
Try to skim over the confusing parts to pick up the essential parts of the documentation:
What methods does this interface or class offer?
What parameters do those methods need?
What kind of return result can be expected?
The methods are listed alphabetically, which does not necessarily cover the most important methods first. Skim to get what you need.
Browse this documentation for the
Set
interface.Find methods that resemble those you used for
List
.
Can you find the Javadoc documentation for the List
interface on your own, using Google? Such documentation is usually found by searching for the fully qualified class, as: java util list
.
Remind yourself of the differences between a
Set
and aList
:Order is important in a
List
. The elements of aSet
occur in no particular order.A
List
can have duplicates. Attempts to add an already present element to aSet
do not succeed.
In the documentation, find the list of All Known Implementing Classes. All of these implement the
Set
interface, but:The method of implementation will vary
The performance of the implementation will vary
Some implementations may exceed the Set interface by offering additional functionality.
Browse the documentation for
HashSet
, an implementation of theSet
interface.Open the
UsingSet
class of theexercises8
package in theexercises
folder.Copy and paste the code from
UsingList
, but change the type of the collection * from List to Set * fromLinkedList
toHashSet
Exercise : 8.73. List and Set of Point¶
In this exercise, you use Eclipse to auto-generate useful parts of a class. Try this on your own, but as you need help, run the solution video only as far as necessary for you to move forward again on your own.
Find and open the
Point
class in theexercises8
package of theexercises
folder.Based on the comment you see there concerning has-a, declare the instance variables for this class, calling them
x
andy
.Auto-generate the constructor by choosing
Source
, thenGenerate Constructor using Fields
, then selectConstructor
from the pop-up menu.Choose the fields
x
andy
, andOK
to generate the constructor.These fields will be immutable, so edit the declarations for
x
andy
and add thefinal
attribute. This ensures that the values ofx
andy
can be set only by the constructor.Now use Eclipse to generate the getters for
x
andy
.In the
main()
method, construct and print aPoint
or two and verify that things look good.
They should not look so good. When you print a Point
, you getan ugly and uninformative String
. To get something better, we must override the toString()
method.
Use Eclipse similarly to generate a simple
toString()
method.
This, and the subsequent work below, is accomplished also via the Source
menu.
OK we are ready to make a
Set
of some points. Put the following code in themain
method:
Set<Point> set = new HashSet<Point>();
set.add(new Point(131, 132));
set.add(new Point(131,132));
System.out.println("Set has " + set);
Recalling that sets should have no duplicates, you the output from the above code should show the set having the same point twice.
This is because Java is using its built-in notion of equality: are two objects exactly the same object (as in, the same reference in memory)?
We need to change this behavior, so read on.
Now let’s use Eclipse to generate
hashCode()
andequals(Object other)
methods.
You must pick the attributes (instance variable names) upon which you wish equality to be based.
Rerun your code and make sure that two
Point
objects with the same coordinates cannot both be contained in the same set.
Further exploration
What happens if
hashCode()
returns a random integer? Try:
public int hashCode() {
return (int)(Math.random()*100000);
}
What behavior do you see in the set?
Why do you see that behavior?
The above code makes hashCode()
inconsistent, breaking a portion of the contract concerning object equality.
What happens if
hashCode()
always returns the same result?
public int hashCode() {
return 0;
}
Does this work?
What impact does the above
hashCode()
have on performance?
Now try lists
Go back and change the Set
and HashSet
of your code to use List
and LinkedList
, respectively.
You should see duplicates. But that’s OK for lists.
Intentionally break
.equals(Object other)
by returningfalse
always.
What aspect of the contract does this break for `.equals(Object other)?
What behavior do you see?
Adding to a list does not consult .equals(Other object)
, so you should see no difference.
Now ask whether the list
.contains(new Point(131,132))
.
Nothing should be found in the list with the broken .equals(Object other)
.
Revert the code back to its proper
hashCode
and.equals(Object other)
.
Exercise : 8.75. Design StockHolding from a user story¶
OK now you are pretty much on your own:
I will tell you a story.
You will design an object.
Create this object from scratch in the
exercises8
package of theexercises
folder.
As usual, if stuck, consult portions of the solutions video.
The story
A StockHolding
object:
has-a
immutable
String
identifying its name (e.g.,IBM
)immutable
String
identifying its owner (e.g.,Ari
)number of share (
int
) that can change over timeprice per share (
int
) that can change over time
and it needs:
A constructor
A
toString()
hashCode()
andequals(Object other)
You should generate the above automatically, not by hand!
Exercise : 8.85. Application of the interface¶
Do your work in the exercises8
package of the exercises
folder.
The story
Define a PersonalProperty
object:
has-a
immutable
int
for its initial valueimmutable
int
representing the years since its initial value was established
and it needs:
A constructor that takes in values for the two instance variables
A method
int depreciatedValue()
that returns the value of the property according to the formula:
initialValue
* 0.80 yearsOld
A
toString()
Your task
After defining the above class, adapt it to implement the Valuable
interface:
public interface Valuable {
public int getLiquidValue();
}