CSE 131 Module 8: Abstract Data Types


Before beginning any work, do a Team...Pull in your repository.


If necessary, review studio procedures before starting.



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.
  1. Read the following:
  2. At this point, create an immutable class for Date and Time in your studio8 package of the studios source folder, based on the above story.
    • 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?
  3. Now implement toString() for each of these classes, returning a String that is appropriately descriptive.
  4. Create a few instances and print them out in the main method of the each class.
  5. Show a TA your work so far.
  6. 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 equals and hashCode methods. You won't understand all of that just yet, but let's do it and we'll explainthings later:

    1. While editing each class, choose SourceGenerate hashCode() and equals()
    2. 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....

    3. Click OK.
    4. 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:
      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 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.

      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 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.
  7. Instantiate some Date and Time objects (several of each) and ensure that they compare properly to each other.
    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.

  8. Let's now make some lists and sets of the objects we have created so far. We'll focus on Date but if you have time, try this with Time as well (pardon the pun).
    1. In the main method of each class, you should already be creating and comparing instances of your objects.
    2. After you have instantiated some 5 objects, create a List of 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.util package.
  9. 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, E refers to the type of element in the list you construct. In this running example, that would be a Date object.
  10. Add some of your Date objects to the list list and print it out whenyou are done.
    To print it you need only say:
  11. Show your work to a TA.
  12. Let's see what happens when we add two Date objects to the list that equal each other:
    Date d1 = new Date(...stuff your constructor needs);
    Date d2 = new Date(...same info as above, so these will equal each other);
    What do you see? Does the same date appear three times in the list?
  13. 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>();
    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?
  14. Show your work to a TA.
  15. The story continues: An Appointment has-a Date and a Time.
  16. Design and implement an Appointment object in the studio8 package.
  17. Just as you did with Date and Time, use eclipse to generate the hashCode() and equals(Object obj). You should base these on equality of the contained Date and Time references.
  18. 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.
  19. Create some instances of Appointment objects using the Date and Time objects you have already made.
  20. In your opinion, what other has-as should an Appointment have?
  21. Show your work to a TA.
  22. Design a Calendar object in terms of a collection of Appointments.
  23. Try to implement and test the methods of your Calendar object.
  24. Show your work to a TA.
  25. Let's add code so that a Time object offers the method boolean isEarlierThan(Time other) that returns whether this Time is earlier than the other Time, assuming the two occur on the same day.
  26. Likewise, a Date offers the method boolean 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.

  27. Show your work to a TA.
  28. Suppose we would like to offer a method common to both Date and Time that indicates whether you are working on the specified date or at the specified time. Create an interface called Working with the single method:
      public boolean amWorking();
  29. Make both Date and Time implement 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
  30. Create a list of Working objects and add up the number of those objects that indicate that you are working.
  31. Show your work to a TA.

Submitting your work (read carefully)

Last modified 01:52:19 CDT 21 October 2018
When you are done with this studio, you must be cleared by the TA to receive credit.

This demo box is for studio 8
Last name WUSTL Key Propagate?
(NOT your numeric ID) Do not propagate
lower case only
e.g. Smith j.smith
1 Copy from 1 to all others
2 Copy from 2 to all others
3 Copy from 3 to all others
4 Copy from 4 to all others

Acknowledgements and assertion of integrity

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:

TAs double check!
  • This demo box is for studio 8
  • The student has committed and pushed the work, and verified that it appears at bitbucket.
TA: Password: