- The work prescribed in this studio will probably exceed the time you have today.
Work as slowly as required to understand what you are doing. You receive credit no matter how far you go with this studio. If there are steps beyond the time you have today, look at those on your own.- In this studio, you carry out the steps you saw in lecture creating an object, reasoning about its equality with other objects, and letting eclipse generate the appropriate code. In lecture you saw a Point object, and the results of that work are in your repositories in the lecture folder. Look for mod8early or mod8late packages. Both are in your repository.
- For this studio, form groups of no more than 2 people. If there is one odd person left in the room, that person can join an already formed group of 2 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.
For the purposes of this studio, assume that all months have 31 days.
- The term immutable means that the instance variables cannot change after the object is constructed. Color is such an object, but Account is not.
To show your intent about immutability, be sure to include the keyword final in your instance variable declarations, as in:
private final int x;If this is unclear, ask around about it.- For now, assume that only legitimate input values are specified for anything your constructor requires.
- Think carefully about what your constructor for Time should retain. Explore a bit how you will deal with the 12- or 24-hour format issue for toString().
- What is the essence of such a Time object?
- What instance variables are required to capture its essence?
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.
public boolean amWorking();
You can be creative in terms of when you decide you are working, but here is a suggestion:
- For Time, assume you are working between 9 AM and 5 PM
- For Date, assume you are working on even-numbered dates
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
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for studio 8