Some aspects of this studio are different than in previous studios:
- We begin with an overview of this studio in lecture format.
- Thereafter, form groups of exactly two people.
- When you have a question or need some help, first ask the other pair sitting nearby or across from you.
- If you need furtherhelp, ask a TA or the professor as needed.
After pasting:https://shell.cec.wustl.edu:8443/cse131/svn/studio8-ZZZZZZ
For the purposes of this studio, assume that all months have 31 days.
For now, assume that only legitimate input values are specified for anything your constructor requires.
For each of the two classes, let's make eclipse generate the equals and hashCode methods. You won't understand all of that just yet, but let's do it and we'll explainthings later:
This is an important step for you. You must decide which of the instance variables (fields) should be used to compare two objects of this kind.These classes were specified such that not all of the fields are relevant for this comparsion. Talk this over, make your choices, and then....
public int hashCode() { return 0; }
However, you can see that the code eclipse generated is much more complicated than that.
For now, imagine that you have before you lots of buckets, each labeled with an integer. Think of hashCode() as returning an integer that represents the only bucket in which this object could be found. Thus, if you want to see if the object exists in all of the buckets, you really need only check one bucket.
Convince yourself that if two objects of the same type (for example, Date) equal each other, then their hashCode() values are the same as computed by the eclipse-generated code.
You might also try to find two objects of the same type such that they differ and their hashCode() values happen to be different.
- If this and obj are physically the same object, then the result should be true.
- No instantiated object equals the null reference. The this reference is always to an actually instantiated object.
- If this and obj are objects of different types, then the answer must be false.
- If none of the above rules applies, then equality can be based on any consistent comparison of any subset of the objects' fields.
Remember to use a.equals(b) to see if a and b equal each other! If you use ==, the comparison is restricted tow whether the two objects are phyiscally the same: the equals(Object obj) method is not run for that comparison.
LinkedList<Date> list = new LinkedList<Date>();
You have not seen the angle-bracket notation. It is used to specify parametric types. It may help to read the above line of code as:You may have to use eclipse suggestions to import the proper classes, which will come from the java.util package.Instantiate a new linked list of Date objects and assign that object to the variable named list.
Note that in the documentation, E refers to the type of element in the list you construct. In this running example, that would be a Date object.
To print it you need only say:System.out.println(list);
What do you see? Does the same date appear three times in the list?Date d1 = new Date(...stuff your constructor needs); Date d2 = new Date(...same info as above, so these will equal each other); list.add(d1); list.add(d2); list.add(d1); System.out.println(list);
Do you you see multiple occurrences of equal Date objects in the set?HashSet<Date> set = new HashSet<Date>(); set.add(d1); set.add(d2); set.add(d1); System.out.println(set);
Based on your observations, what is the main difference between sets and lists?
Implement and test these two methods.At this point, if implementation of these methods is difficult, revisit the way you specify and accept information for these classes. You are free to design them to make your life easier.
When you done with this studio, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches