Espresso: A Concentrated Introduction to Java


Laboratory: Interfaces

Summary: In today's laboratory, you will explore Java's interfaces by considering different ways to represent simple dates.

Equipment

None.

Contents

Exercise 0: Preparation

For this laboratory, create a package in Eclipse named username.time.

Paper and Pencil Problems

Exercise 1: Choosing Representations

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.

Exercise 2: Exploring Representations

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.

Computer Problems

Exercise 3: A Time Interface

Create a new interface of the following form. (To create an interface in Eclipse, you can select New and then Interface from the File menu. Alternatively, you can create a new class and then modify it to indicate that it is an interface, not a class.)
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();
}

Exercise 4: A Time Implementation

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");

Exercise 5: Another Time Implementation

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.

Exercise 7: Generalizing

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.

Exercise 8: Changing Implementations

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.


Written and revised by Samuel A. Rebelsky, 2005-2006.
Revised further by Marge M. Coahran, 2006.

Samuel A. Rebelsky
rebelsky@grinnell.edu
-->