Espresso: A Concentrated Introduction to Java
Summary: In today's laboratory, you will explore Java's interfaces by considering different ways to represent simple dates.
Equipment
None.Contents
username.time.
You will recall that we recently considered a small implementation of a
Time class, in which we stored three integer values:
hours, minutes, and seconds.
Think of another way that you could represent Time
values. Focus on the fields you would use, not the way in which you
would implement the methods.
Another typical mechanism for representing times is to store the number of seconds (or milliseconds) since the previous midnight.
What are the advantages and disadvantages of each of these approaches? Feel free to discuss your answer to this question with a neighbor or partner.
public interface Time {
/* CONSTANTS ---------------------------------------------------- */
public static final int HOURS_PER_DAY = 24;
public static final int MINUTES_PER_HOUR = 60;
public static final int SECONDS_PER_MINUTE = 60;
/* METHOD HEADERS ----------------------------------------------- */
// returns the number of seconds since the previous midnight
public int secondsSinceMidnight();
// these methods return the hours, minutes, and seconds in the
// standard human-readable time format
public int hours();
public int minutes();
public int seconds();
}
a. Write a class AccumulatedSeconds that implements
Time, by storing a given time as the total number of seconds
since the previous midnight. (Be sure that the header for this class
includes implements Time.)
Provide a constructor that accepts a string of the form "hh:mm:ss".
Note that you will need to implement each of the methods specified in the interface.
Note also that you can (and should) make use of the constants declared in the interface, rather than embedding numeric literals (e.g., 60) throughout your code.
b. Write a main class to test your time implementation. For this exercise, begin by declaring a reference variable and generating a new time object with types that match, as shown below.
AccumulatedSeconds t1 = new AccumulatedSeconds("11:00:00");
a. Write a class HrsMinsSecs that implements
Time. (Be sure that the header for this class also
includes implements Time.)
Provide a constructor that accepts a string of the form "hh:mm:ss".
You will again need to implement each of the methods specified in the interface.
b. Test your class in a manner similar to that in the previous exercise.
a. At this point, you should have a main class that is useful for testing time objects. Modify the initial declaration of the time object that is being tested such that it has the following form
Time t1 = new HrsMinsSecs("11:00:00");
b. What effect do you expect this change to have?
c. Confirm your answer experimentally.
d. Remove the implements Time line from
AccumulatedSeconds.java.
e. What effect do you expect this change to have?
f. Confirm your answer experimentally.
g. Reinsert the implements line.
a. Now change all the instances of AccumulatedSeconds in
your test class to HrsMinsSecs.
b. What effect do you expect this change to have?
c. Confirm your answer experimentally.