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

Contents

Exercise 0: Preparation

Because this exercise uses a variety of files, you will use a different strategy for creating the package.

a. In a terminal window, type

/home/rebelsky/bin/espresso dates

You should see messages about files being copied to a temporary directory.

b. Start Eclipse.

c. In Eclipse, build a project named Temp from /home/username/CSC152/Temp.

d. In the Temp project, you should see a package named username.dates. Drag that package to your Code project.

e. Delete the Temp project.

You can now work with the username.dates package within your Code project.

Paper and Pencil Problems

Exercise 1: Choosing Representations

Look at the interface described in SimpleDate.java. As you will note, this interface describes dates that fall within a year that is not a leap year.

Decide on two different ways you could represent SimpleDate values. Focus on the fields you would use, not the way in which you would implement the methods.

Exercise 2: Exploring Representations

Three typical mechanisms for representing such dates are (a) using an integer for the month and an integer for the day of the month; (b) using a string for the month and an integer for the day of the month; (c) using an integer for the day of the year.

What are the advantages and disadvantages of each?

Please discuss your answer to this question with a neighbor or partner.

Exercise 3: Computing the Day of the Year

In the representation that uses an integer for the month and an integer for the day of the month, how would you compute which day of the year the date falls on? (That is, sketch dayOfYear().)

Exercise 4: Computing the Month

In the representation that uses an integer for the day of the year, how would you implement the month() method?

Exercise 5: Exploring Implementations

Because problems like the preceding two require similar helper computations, I've defined a utility class, DateHelper, which provides the following static methods:

Since you will be using these methods, and not implementing or modifying them, you need not know how they work, just that they work. (You could certainly implement them using a sequence of conditionals. I've used arrays, a technique that you may not yet know.)

a. Read through MonthDay.java and DayOfYear.java. Do the implementations of the methods match your algorithms from the problems above?

b. What other aspects of these implementations do you find interesting or puzzling?

Simple Experiments

Exercise 6: Exploring the Tester

a. Read the code for DateTester.java and explain what it does.

b. Compile and execute it and see whether it works as you expected.

c. Explain where in DateTester we take advantage of interfaces. (Hint: It's in one of the calls to a helper method.)

Exercise 7: Generalizing

Each time we declare a date, the declaration reads (approximately)

MonthDay name = new MonthDay(initialization);

a. Change each declaration to have the form

SimpleDate name = new MonthDay(initialization);

b. What effect do you expect this change to have?

c. Confirm your answer experimentally.

d. Remove the implements SimpleDate line from MonthDay.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. Change all the instances of MonthDay in DateTester.java to DayOfYear.

b. What effect do you expect this change to have?

c. Confirm your answer experimentally.

Exercise 9: Combining Implementations

a. in DateTester.java, make the birthday a MonthDay, but leave the other two dates as DayOfYear objects.

b. What effect do you expect this change to have?

c. Confirm your answer experimentally.

d. When you are done with your confirmation, change the MonthDay back to a DayOfYear. (After this change, DateTester.java should no longer refer to MonthDay.)

Advanced Experiments

Exercise 10: Expanding the Interface

a. Add two mutators to the SimpleDate interface:

b. What effect do you expect this addition to have if you compile DayOfYear.java?

c. Confirm your answer experimentally.

Exercise 11: Expanding an Implementation

a. Implement advance and retreat for DayOfYear.

You need not check for errors or throw exceptions. (Someone stupid enough to advance beyond the end of the year or retreat before the start of the year deserves whatever happens.)

b. Test your implementation. For example, you might print two weeks of days, starting at someone's birthday.

Exercise 12: Expanding Another Implementation

Implement and test advance and retreat for MonthDay.

History

Sunday, 25 September 2005 [Samuel A. Rebelsky]

Monday, 26 September 2005 [Samuel A. Rebelsky]

Tuesday, 27 September 2005 [Samuel A. Rebelsky]

Tuesday, 28 February 2006 [Samuel A. Rebelsky]


This page was generated by Siteweaver on Thu Mar 30 15:24:37 2006.
The source to the page was last modified on Tue Feb 28 09:48:56 2006.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Espresso/Labs/interfaces.html.

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky
rebelsky@grinnell.edu