Studio 8: Dates and Appointments
-
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.
-
If you need further help, ask a TA or the professor as needed.
Warmup
- First, form a group:
- All but one member of your group should have this web page open so you can follow along and see the instructions as you work
- Plan to have one computer at which your team does its work. Initially, one of you will be in charge of typing at that computer.
- Throughout the studio, you should trade who is in charge of the keyboard. Before doing so, commit your work to make sure your work is saved
Procedure
From the story described below, design and implement the described classes. Follow the instructions carefully. Don't rush ahead until you have successfully completed the specified work.
-
Read the following:
- A
Datehas-a month, day, and year. It also has-a field that indicates whether the date is a holiday or not.
For the purposes of this studio, assume that all months have 31 days.
- A
Timehas-an hour (0 to 23) and a minute (0 to 59). It also has-a field that indicates whether the (toString()of the) time should be in 12- or 24-hour format.
- A
-
At this point, create an immutable class for
DateandTimein yourstudio8package of thestudiossource folder, based on the above story.- The term immutable means that the instance variables cannot change after the object is constructed.
Coloris such an object, butAccountis not.
To show your intent about immutability, be sure to include the keyword
finalin 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
Timeshould retain. Explore a bit how you will deal with the 12- or 24-hour format issue fortoString().-
What is the essence of such a
Timeobject? -
What instance variables are required to capture its essence?
-
- The term immutable means that the instance variables cannot change after the object is constructed.
-
Now implement
toString()for each of these classes, returning a String that is appropriately descriptive. -
Create a few instances and print them out in the
mainmethod of the each class. -
Show a TA your work so far.
-
We next equip our two classes with the ability to tell whether they do, or do not equal a given
Object.For each of the two classes, let's make eclipse generate the
equalsandhashCodemethods. You won't understand all of that just yet, but let's do it and we'll explainthings later:-
While editing each class, choose
Source..Generate hashCode() and equals().. -
You are then presented with a menu of instance variables.
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….
-
Click
OK. -
Take a look at the code that is generated. Again, it probably will not make much sense, so please consider the following as you look at the code:
hashCode()For whichever fields you have chosen for equality considerations, it must always be the case that if two objects do equal each other, then they have the same
hashCode()value. This implication goes only in the direction stated, so one possible legal implementation is simply: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 theirhashCode()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.equals(Object obj)With regard to the code automatically generated for
equals(Object obj), the contract in Java for equals includes the following; read over the code and convince yourselves that the code enacts the proper contract:- If
thisandobjare physically the same object, then the result should betrue. - No instantiated object equals the
nullreference. Thethisreference is always to an actually instantiated object. - If
thisandobjare objects of different types, then the answer must befalse. - If none of the above rules applies, then equality can be based on any consistent comparison of any subset of the objects' fields.
-
-
Instantiate some
DateandTimeobjects (several of each) and ensure that they compare properly to each other.Remember to use
a.equals(b)to see ifaandbequal each other! If you use==, the comparison is restricted to whether the two objects are phyiscally the same: theequals(Object obj)method is not run for that comparison. -
Let's now make some lists and sets of the objects we have created so far. We'll focus on
Datebut if you have time, try this withTimeas well (pardon the pun).-
In the
mainmethod of each class, you should already be creating and comparing instances of your objects. -
After you have instantiated some 5 objects, create a
Listof such objects by using the following code:
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:
Instantiate a new linked list of Date objects and assign that object to the variable named
list.You may have to use eclipse suggestions to import the proper classes, which will come from the
java.utilpackage. -
-
What can we do with a LinkedList object? Click on the link in the sentence before this one and check out the API.
Note that in the documentation,
Erefers to the type of element in the list you construct. In this running example, that would be aDateobject. -
Add some of your
Dateobjects to thelistlist and print it out whenyou are done.To print it you need only say:
System.out.println(list);
-
Show your work to a TA.
-
Let's see what happens when we add two
Dateobjects to the list thatequaleach other: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);
What do you see? Does the same date appear three times in the list?
-
Let's do the same thing but this time with a
HashSet. After the code you have written so far, add:HashSet<Date> set = new HashSet<Date>(); set.add(d1); set.add(d2); set.add(d1); System.out.println(set);
Do you you see multiple occurrences of equal Date objects in the set?
Based on your observations, what is the main difference between sets and lists?
-
Show your work to a TA.
-
The story continues: An
Appointmenthas-aDateand aTime. -
Design and implement an
Appointmentobject in thestudio8package. -
Just as you did with
DateandTime, use eclipse to generate thehashCode()andequals(Object obj). You should base these on equality of the containedDateandTimereferences. -
Read over the code eclipse generates. Note how it delegates equality to the contained objects, in which you have previously defined how you want equality treated for objects of those types.
-
Create some instances of
Appointmentobjects using theDateandTimeobjects you have already made. -
In your opinion, what other has-as should an
Appointmenthave? -
Show your work to a TA.
-
Design a
Calendarobject in terms of a collection ofAppointments.- Should you use a list or a set?
- What methods should your
Calendarobject offer?
-
Try to implement and test the methods of your
Calendarobject. -
Show your work to a TA.
-
Let's add code so that a
Timeobject offers the methodboolean isEarlierThan(Time other)that returns whetherthisTimeis earlier than theotherTime, assuming the two occur on the same day. -
Likewise, a
Dateoffers the methodboolean isEarlierThan(Date other)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.
-
Show your work to a TA.
-
Suppose we would like to offer a method common to both
DateandTimethat indicates whether you are working on the specified date or at the specified time. Create an interface calledWorkingwith the single method:public boolean amWorking();
-
Make both
DateandTimeimplement that interface.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
- For
-
Create a list of
Workingobjects and add up the number of those objects that indicate that you are working. -
Show your work to a TA.