home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
hw05
Objects containing objects
hw05
Due Mar.05 (Mon) 17:00;
30pts.
Reading: Chapter 3.
This problem is Exercise 3.30 from the textbook,
However, some direction is provided here (as mentioned in 3.31),
to help guide us toward a simple solution.
Take the class
ClockDisplay
from the book,
and…
- Make a project which includes class NumberDisplay
and class ClockDisplay, from Chapter 3.
On Blackboard1,
you can download the code which comes on the book's CD,
as well as a version of that same code modified to look like the code
this class is using in lecture. (You can work with either version;
see below for how the lecture code style tends to differ from the book's code style.)
-
Add your name to the @author
@return and @param as needed.
- Rename getTime to be getTime24.
Change its code as follows:
String getTime24() {
return hours.getDisplayValue() + ":" + minutes.getDisplayValue();
}
|
(Note how the comment for the method is
entirely unchanged, even though we are modifying the implementation.)
- Delete the field displayString,
the method updateDisplay,
and all calls to updateDisplay.
(Make sure your program compiles and works correctly.)
- We will work towards writing getTime12,
a ClockDisplay method which returns
a string representing the time in a 12-hour format.
Write a signature and a stub function,
and (in BlueJ) record your test cases.
Do not write the function yet.
- Write a ClockDisplay
method String amPmString(),
which returns the correct suffix.
(We'll use this string later, when computing the 12-hour display string;
for now, we'll just write this small function to stand on its own.)
Write test cases, before writing the function!
Remember to use getters, as needed.
-
Write a helper method in ClockDisplay,
/** Take a number, and wrap it to the interval [1,12] (inclusive).
* @param n The number to wrap.
* @return a number in 1,12. The number is usually
* the remainder after dividing n by 12, except that
* when n is a multiple of 12, we return 12 instead of 0.
* Examples:
* wrapTo1_12( 0 ) = 12
* wrapTo1_12( 1 ) = 1
* wrapTo1_12( 10 ) = 10
* wrapTo1_12( 12 ) = 12
* wrapTo1_12( 13 ) = 1
* wrapTo1_12( 144 ) = 1 12
* wrapTo1_12( 145 ) = 1
*/
|
-
Finally, expand your stub-function for getTime12,
so that it's a working function which passes your test cases.
The two helper functions you just wrote will make your life easy!
Extra Credit
- (3pts)
Generalize wrapTo1_12(int) to wrapTo1(int, int)
where the second input is the maximum value (instead of being hard-coded
as 12).
- (The homework page will have some extra-credit problems after this
assignment is due.)
See
differences between our code and the book.
1Since the book's code is copyrighted,
we can't publish it on the class website. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs