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
Contents
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.
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.
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.
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()
.)
In the representation that uses an integer for the day of the year, how
would you implement the month()
method?
Because problems like the preceding two require similar helper
computations, I've defined a utility class, DateHelper
,
which provides the following static methods:
String DateHelper.nameOfMonth(int month)
- Returns the name of a month given as an integer between 1 and 12.
int DateHelper.daysInMonth(int month)
- Returns the number of days in a month given as an integer between 1 and 12.
int DateHelper.monthNum(String name)
- Returns the number of the month with the given name. Returns 0 if the name is not understood.
int DaysBetween(SimpleDate d1, SimpleDate d2)
- Returns the number of days between the two dates.
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?
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.)
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.
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.
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
.)
a. Add two mutators to the SimpleDate
interface:
advance(int numdays)
, which advances the date by the specified number of days, and
rewind(int numdays)
, which backs up the date by the specified number of days.
b. What effect do you expect this addition to have if you
compile DayOfYear.java
?
c. Confirm your answer experimentally.
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.
Implement and test advance
and retreat
for
MonthDay
.
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
;
;
Check with Bobby