These exercises are part of the asynchronous component of this course. Material is taught and demonstrated, but at times you are asked to complete one of the exercises to help you master the new concepts.In the repository you are using for this course, look inside the exercises source folder, and you will see assignments organized by name. Before continuing on to the next portion of the asynchronous material, spend some time on the exercise. When you are finished, commit your code so that credit for the exercise can be given.
It is not so important that you get the exercise exactly right. It is important to work through it as best you can before you look at the solution or continue with other work in this module.
Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.
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.
Think of possible applications of the other two types of lists covered in the video:
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!
Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.
To get started, open the UsingLists class in the exercises8 package of the exercises folder.
Some important points:
For now, use LinkedList.
The classes you want are found in java.util so be sure to choose those.
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!
Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.
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:
The methods are listed alphabetically, which does not necessarily cover the most important methods first. Skim to get what you need.
- What methods does this interface or class offer?
- What parameters do those methods need?
- What kind of return result can be expected?
It is no accident that the same methods are here: Java implements its collection objects with great consistency.
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.
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!
Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.
Admire the work eclipse just did for you.
This ensures that the values of x and y can be set only by the constructor.
Usually the menu you are presented would also offer to generate setters but eclipse knows the relevant variables are final and cannot be set after construction.
They should not look so good. When you print a Point, you get an ugly and uninformative String. To get something better, we must override the toString() method.
This, and the subsequent work below, is accomplished also via the Source menu.
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.
You must pick the attributes (instance variable names) upon which you wish equality to be based.
public int hashCode() {
return (int)(Math.random()*100000);
}
The above code makes hashCode() inconsistent, breaking a portion of the contract concerning object equality.
- What behavior do you see in the set?
- Why do you see that behavior?
public int hashCode() {
return 0;
}
- Does this work?
- What impact does the above hashCode() have on performance?
Go back and change the Set and HashSet of your code to use List and LinkedList, respectively.
What aspect of the contract does this break for .equals(Object other)?
Adding to a list does not consult .equals(Other object), so you should see no difference.
Nothing should be found in the list with the broken .equals(Object other).
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!
Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!
Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.
public interface Valuable {
public int getLiquidValue();
}
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!