Assignment #1
Due: Sat, Jan 27
Objective
To practice with Java syntax, both in writing classes and in writing
executable programs. Also to practice with Java compilation and execution
of programs.
Task:
I have provided an example of two C++ classes, and your task will be to
translate these into two Java classes (with some slight differences
specified below), and also to write a small test program that uses these
classes.
Details:
The following files contain two classes, called
Display and Timer.
- timer.h
- declarations of both classes
- timer.cpp
- definitions of class members, for both classes
This example uses composition and creates Timer as a new
type that represents a digital timer, which looks like a clock displayed
in 24-hour time (like military time).
You are to translate these two classes into Java classes
Display and Timer. Functionality, permissions
(public/private), method names, and other class details should match those
of the given c++ classes except where noted in these guidelines:
- Write the Display class in the file Display.java,
and write the Timer class in the file Timer.java
- In the C++ code, both classes have a show() member function.
To make this more versatile, convert these in your Java classes into
toString functions instead. This means you will return a
string representation of the object from inside the class, rather than
print to the console.
- In the C++ code, there is an overload of operator+ for the Timer
class. Since Java does not allow operator overloading, translate this to a
Timer class method called add(). The add function
should have the same functionality as the operator+ function in the C++
code.
You are also to write a sample test program, described below.
Test Program
I have provided one small file with some hard coded calls, in
the file Sample.java.
This illustrates the syntax of expected calls to public
methods of class Timer.
You are to write your own test program, in a class called
TimerTest (filename TimerTest.java). It should do the
following:
- Create two objects of type Timer
- Prompt and ask the user to enter (from the keyboard) values for hours
and minutes for the first timer. Attempt to set this as the clock time on
the first timer. Whenever the attempt fails, make the user re-enter the
data
- Repeat this user-entry process for the second timer
- Print out the values on both timers
- Add the two timers together, and print the result
- Increment each timer 20 minutes, printing out the results as you
go
A sample of the expected execution of this test program appears below.
Sample run of TimerTest.java
(keyboard input is underlined, for clarity)
Enter hours for first timer: 34
Enter minutes for first timer: 45
Invalid timer values. Try again.
Enter hours for first timer: 12
Enter minutes for first timer: 54
Enter hours for second timer: 9
Enter minutes for second timer: 70
Invalid timer values. Try again.
Enter hours for second timer: 9
Enter minutes for second timer: 48
t1 = 12:54
t2 = 09:48
t1 added to t2 is: 22:42
Incrementing t1 by 20 minutes
12:55
12:56
12:57
12:58
12:59
13:00
13:01
13:02
13:03
13:04
13:05
13:06
13:07
13:08
13:09
13:10
13:11
13:12
13:13
13:14
Incrementing t2 by 20 minutes
09:49
09:50
09:51
09:52
09:53
09:54
09:55
09:56
09:57
09:58
09:59
10:00
10:01
10:02
10:03
10:04
10:05
10:06
10:07
10:08
Testing and Submitting
You can test your code on either program.cs.fsu.edu or
linprog.cs.fsu.edu. The Java2 SDK 1.5.0 is installed on both of
those machines (this is the Java Standard Development Kit). Remember that
we've discussed how to compile java code files with the javac
command, and how to run a main program with the java command.
Example:
javac Fraction.java // this would compile the Fraction class
javac Qwerty.java // this would compile the Qwerty class
java Qwerty // this would run the main program in the
// Qwerty class
You will submit these files:
Display.java
Timer.java
TimerTest.java
To prepare your submission, we will use one more Java tool -- the jar
(java archive) utility. This is similar to the tar (tape archive) utility
common in unix systems. Pack up your files into a jar archive file with
this command:
jar cvf hw1.jar Display.java Timer.java TimerTest.java
E-mail this jar file (hw1.jar) to me (myers@cs.fsu.edu) by the due
date. Your e-mail subject should be HW1-SUBMIT. Include your name
and section in the e-mail body. Please only submit ONCE, unless you make
a mistake and need to correct it (before the due date) -- and minimize
this. Only the last submission will be graded.
Your submission e-mail needs to come either from your CS account
(preferred) or your garnet/mailer account.
Suggestion
The easiest way to mail a file from your CS account is
to send the e-mail directly from your CS address. I'd suggest using the
"pine" e-mailer from there. HOWEVER, it is better to do this when logged
on to shell.cs.fsu.edu (not program or linprog). If you need
help with doing this, see me.