Copyright RIT 2003
$Id: writeup.xml,v 1.4 2007/08/27 21:25:53 vcss231 Exp vcss231 $
The purpose of this lab is to learn how to write a Java class from a specification, and see how the finished class can be used in two different applications.
In addition, this lab will give you some practice with unit testing your code.
Java class.
Java class.
Java class can be
used in a variety of applications.
Java
expressions and operators.
Review your course notes and read chapters 5 and 7 in Liang.
Read about class stubs by clicking here (Auxiliary/stubs.html)
Complete the exercises found by clicking here (http://www.cs.rit.edu/~vcss231/Exercises/_Expressions_3) .
Every computer needs to have some notion of time. Some programs need the current date and time, while others may record the time required to perform some task. For either application, the basic process of storing the time and the methods required to use and manipulate that time are almost the same.
In this lab you will write a Java class named
TimeCounter. The TimeCounter
class provides a way to store, change, and retrieve the
time. One of the things that we need to decide about
this class is how it will store the time. One way
to do this would be to store the number of hours, minutes,
and seconds after midnight. This technique works fine
unless you need to convert the time into different
formats. Most computers display the time based on
the time zone in which the computer is located.
A computer in Rochester uses Eastern Standard Time(EST),
whereas a computer in San Francisco uses Pacific Standard
Time (PST). Conversions of time into different
representations is much easier if the time is stored as
the number of seconds past some well known
date and time. Most Unix systems store time as the
number of seconds that have elapsed since January 1, 1970
(Windows stores time as the number of seconds
since January 1, 1980).
The TimeCounter class that you will write
will store the time in tenths of seconds. In other
words, your TimeCounter will have only
one instance variable that stores time in
tenths of a second. This will give the class the
ability to easily convert time into different formats,
and it allow us to record time to the nearest 10th of
a second. The class will also provide methods to set, change, and
obtain the time in hours, minutes, and seconds.
The javadoc page that gives the
specifications for each of the methods in the
TimeCounter class can be found here:
TimeCounter.
Download and unpack the lab6.jar
(/~vcss231/pub/lab06/lab6.jar)
file that contains the code you will need
to complete this lab. Now write a compilable class
stub for the TimeCounter class (if you
have not read the
information on class stubs
(Auxiliary/stubs.html)
you might not know what a stub is). Refer to the
Javadoc pages for the TimeCounter
class to determine what the headings for each of the
methods should look like. Remember for any method
that is a function you must include a return
statement to get your class to compile.
As always, any code that you write must adhere to the
RIT Java coding standard
(http://www.cs.rit.edu/~f2y-grd/java-coding-standard.html)
.
Once your stub compiles check to see if
you got it right by compiling and
running the program
TestStub.
TestStub.java is
included in the
jar file that
you downloaded. Take a few minutes to
look at the contents of the file.
Note that the
program does not do anything useful,
but it does use every method in the
TimeCounter class. The
following command will compile the
TestStub program with your
TimeCounter class stub:
|
Any errors that the compiler reports are the result of your
TimeCounter class stub. Find the
errors and correct them. Once you have corrected
all of the errors, submit your class stub by typing the
following command:
|
Now that you have the class stub for the
TimeCounter class, it is time to start
filling in the code for each of the methods.
To help you verify that your methods are correct,
we have provided you with a simple Java
application called TimeConverter. To run our TimeConverter application,
type the following command:
|
As you can see the TimeConverter allows
you to type in a number representing time in 10ths of a
second and displays the corresponding time in the
form HH:MM:SS.T (where HH=hours, MM=minutes,
SS==seconds, and T==10ths of a second).
For example, if you type in the number 600 in the
input box, the time 00:01:00.0 will appear since there are
60 seconds in a minute and 10 10ths of a second in a
second (i.e. 60 times 10 is 600).
The TimeConverter also allows you to
increment and reset the time it is currently displaying.
Using the TimeConverter, develop
expressions that will take the time in 10ths of
a second and return the number of hours, minutes after
the hour, seconds after the minutes, and 10ths of
a second after the seconds for that time.
Verify that your expressions are correct using
a calculator and the TimeConverter
(if you don't have a calculator there is
one available on your computer). Be sure
to include boundary conditions
as tests for the expressions that you have written.
In case you have not figured it out already, the
TimeConverter uses the TimeCounter
class to do all of the work. When you type in a
time in 10ths of a second, the TimeConverter
invokes the setTime() method in the
TimeCounter to store the time you
type in. To display the time, the
TimeConverter invokes the
getTSeconds(),
getSeconds(), getMinutes(),
and getHours() methods provided by the
TimeCounter class to get the values it needs to
display.
The inc and
reset buttons
simply invoke the tick()
and reset() methods.
Now is the time to start filling in the code in the
stub file you wrote in activity 1. Start by
determining what constants will be
used by the class and declaring the necessary
class variables to hold those constants.
Make sure that you have only one instance
variable declared. Then fill in the constructors,
the getTime()/setTime() methods,
the reset() method,
and the tick() method. In the jar file you downloaded you will
find the source code for the
TimeConverter application that
you can use to verify that the methods in your
TimeCounter class are implemented
correctly. To compile your
TimeCounter class with
the TimeConverter code type
the following command:
|
If the compiler reports any errors check the code
you just wrote. Note that chances are almost
100% that the errors reported by the compiler are from
the work you did in this activity, since you have already
verified that the declaration syntax for
the TimeCounter class was correct. Developing a class step by step makes it easier
to find and correct errors since you have a real good
idea where they occur. Once the
TimeConverter program compiles,
run it by typing the following command:
|
You should see the TimeConverter window
appear on your screen. Type in a few numbers
into the TimeConverter and try pressing
the increment and reset buttons. Nothing should
happen, which is okay, since you have not written the
getTSeconds(), getSeconds(),
getMinutes(), and getHours()
methods yet.
Using the expressions you developed earlier, complete the
rest of the methods in the TimeCounter class
one method at a time. After you have finished a
method, compile the program and try it out. Enter numbers in the TimeConverter
to verify that your expressions are working correctly.
After you have written all of the methods in
TimeCounter and you are convinced that
they work correctly, submit your program using
the following command:
|
This last activity does not involve any programming,
but will show you how the TimeCounter
class you developed can be used in a
useful application.
The program StopWatch.java, which was
included in the jar file, is a
program that implements a standard stop watch. The program uses the TimeCounter
class to keep track of the time. To compile a
version of the stop watch program that uses
your TimeCounter class, type
the following command:
|
After the program compiles type the following command to start the program:
|
The resulting stop watch that appears
on your screen is made possible by you
and your TimeCounter
class.
Write a class called Measurement that
keeps track of distances.
A javadoc page that describes
this class can be found by clicking
Measurement. Like your
TimeCounter class, the
Measurement class will define only one
instance variable that will record the distance in inches.
The other methods in the class will have to convert this
distance to the unit requested by the user.
To test your code, you
are required to write a main method within the
Measurement class that contains the code to
unit test your class. Now you could
implement this test program as a separate class, but often when
unit testing a single class, it makes sense to place the testing
code in a main method within the class itself. The reason for
this is that it keeps the class and code to unit test the class as a
single unit.
(
Note that the existence of other classes with main methods
causes no confusion for java. When you
execute a program you supply java with the name of a class
that contains a main method. It is this
main method
that java will begin to execute.
)
When writing your unit test code you should make sure that you test each method multiple times with data that will make sure that it is working for all cases. Some examples of cases that you should be thinking about are:
int, but for
this lab you shouldn't worry about these cases.
)
To document your testing, you will also be required to create a textfile
called test_cases that contains
documentation about how you tested the class. This documentation should
contain a list of the
Measurement
objects that you
create, including the arguments to the constructors that you used to
create them. The object creation section
should look something like the table below, which is an example from the
TimeCounter class from activity 2.
| Object Name | Constructor( Args ) | Explanation |
|---|---|---|
| timer1 | TimeCounter(99999) | object used to test the get methods of the class (99999T = 02:46:39:9) |
TimeCounter class from activity 2.
| Object | Method(Args) | Expected output | Explanation |
|---|---|---|---|
| timer1 | getSeconds() | 39 | testing to see that the calculation of 1/10th sec to seconds works |
Method(Args) section will often contain multiple method
calls that are needed to perform a particular test.
You will be graded on the unit test program that you write for this class, so take your time, and make sure your tests are complete.
You might want to know that 1 inch is equivalent to 2.54 centimeters.
After you are sure that
everything is correct, submit your Measurement class
using the following command:
|
Grade Breakdown: