Exercise 04: Rock Around the Clock
Katarina Elevator in Stockholm, Foto by Arjan Richter
This week’s lab work is intended to get you to implement parts of a class that is given for you. You are also to begin to see the idea of modularization, that is, splitting up code into classes.
Pre-lab
P0. Which of the following expressions returns true? After writing your answers on paper, open the CodePad in BlueJ and try it out.
Expression | Your Answer | Actual Value |
---|---|---|
! (4 < 5) | ||
! false | ||
(2>2) | ||
(4==4) && (1<0) | ||
(34 != 33) && !false | ||
(43 < 42) && (rabbitCount > dogCount) | ||
test = (3 < 4) |
P1. Write an expression using boolean variables a and b that evaluates to true when either a and b are both true or both false.
P2. Write an expression using boolean variables a and b that evaluates to true when only one of a and b is true, and which is false if a and b are both true or both false. This is called the exclusive-or.
P3. Consider the expression (a && b). Write an equivalent expression (one that evaluates to true at exactly the same values for a and b) without using the && operator.
P4. Americans are kind of strange about numbers and units. They write the days backwards, they use pounds and inches instead of kilograms and centimeters, and they have this bizarre 12-hour clock they use with “am” and “pm”.
- What time is “12:00 am” on the German (24-hour) clock?
- What time is “12:00 pm”?
- What time is “03:00 am”?
- What time is “05:30 pm”?
P5. write a method that computes the 12hrs-clock hour value from the 24hrs-clock hour value.
P5. write a method that takes the 24hrs-clock hour value as a parameter and returns the correct value for “am”/“pm” for the 12-hrs clock
What To Hand In
You need to upload 2 Files to Moodle:
- Your Lab Report as PDF. For more Information on the report see the Labs and Exercises page.
- The source code folder containing all BlueJ projects compressed with ZIP and the extension .zip
Lab assignments are due before your next lab at 22:00. They may, of course, be turned in earlier.
Assignment
Source Code for this exercise: https://github.com/htw-imi-info1/chapter03 specifically the LED Display in the BlueJ Project book-projects/clock-display-leds.
Here’s an example how to use the LedDisplay:
LedDisplay ld = new LedDisplay();
ld.start(); // this starts the automatic clock update
ld.toggleTicker(); // toggles between timeTicks every second and faster timeTicks (for testing)
ld.setTickerSpeed(100); // make it real fast - 100 ms
ld.stop(); // to stop the automatic timeTicks for manual testing like so:
ld.setTime(11,59);
ld.timeTick();
ld.setTime(23,59);
ld.timeTick();
Note that - in difference to the original book example - the ClockDisplay stores the time in an int field minutesInDay
, not in the various NumberDisplays!
Part 1: Clock time
Adapt the simple ClockDisplay (Class ClockDisplay) to “display” the time American-style (i.e. 12-hour clock and am / pm) (display meaning store it in the field displayString) You will have to include the am/pm in the time! Make sure your displayString has exactly the format as in this example:
06:15 am
. Note that you only need to modify the method updateDisplay()!!Now, do the same for LedDisplay. It only has two methods, now you need to extend both of them to add the am/pm part.
- note that there is a boolean constant
WITH_AM_PM
in ClockDisplay that needs to be set to true to get a wider LEDDisplay with space for the am/pm part. - you can also use it to be able to switch between both modes, but this is optional.
- NumberDisplay has a method
public void updateDisplay(String twoChars)
that you can use to display the am/pm part - simply pass “am” or “pm”.
- note that there is a boolean constant
Make your clock into an alarm clock by adding an alarm. You should be able to set the alarm time and turn the alarm on and off. When the clock reaches the alarm time, it should ring (writing “Riiiiiiiing!” to the terminal is sufficient).
Part 2: Completing the KaraClock
This builds on the preparation exercises done during the last labs. The code repository is still https://github.com/htw-imi-info1/kara-clock-lab - the same as last week. You find an overview of all kara clock exercises in karaclock-complete
All you have to add is a method that can set the Digit’s value to enable the setTime() method:
6. Initialize DigitDisplayKara (set count of leaves)
In order to be able to set the clock to a given time, each digit needs to be able to initialize with a given number of leaves.
This could be achieved with the same number of ticks, but would take some time and would not be as elegant.
You can use the DecimalNumberWorld to test it. After completed, call DisplayKara’s setValue(1234567890);
to achieve the pattern in part 5.
Now, your clock should work!
7. The first Kara Clock
Now, with your completed DigitalDisplayKara, the first Kara Clock can be built.
Open ClockDisplayWorld1
- it contains three DigitDisplayKaras: one for minutes, one for ten minutes, and one for hours.
Experiment a bit with it!
Showing 24 hours is difficult though - 24 leaves would be too much, using two digits - one for 10 hours, one for single hours - would need to toggle the limit for the single hour DigitDisplayKara between 10 and 4.
If you want a real clock open ClockDisplayWorld12Hours
. It starts a thread and calls tick() every second, you can start and stop it using the
respective methods in ClockDisplayKara
8. The 12 Hour Clock with am/pm
To show all 24 hours, a 12 hours am/pm is a good solution for the Kara-Clock!
The first step of changing it to a proper 12h clock with am/pm display has
already been made in ClockDisplayWorld12Hours
. It uses a special HoursDisplayKara
for the hours, which shows a “12” instead of the “0”. Find the getDisplayText(int count)
to see how this is implemented. You can use this as a guide to create
an AmPmDisplayKara that is also a subclass of DigitDisplayKara and only overrides the public String getDisplayText(int count)
(note that this is only used to set the text below the lower tree line.)
Use ClockDisplayWorldAmPm
(which is a copy of ClockDisplayWorld12Hours
) to implement this.
Find and read its prepareColumns()
method to understand how the clock display is built and what you need
to do to add another digit.
Technically, the ClockDisplayWorld is just a number with different rollOverLimits for each digit. The place value has to be the product of the previous (mostly right) digit and its place value:
am/pm | hours | ten minutes | minutes | |
---|---|---|---|---|
limit | 2 | 12 | 6 | 10 |
placeValue | 12*60 | 6*10 | 10*1 | 1 |
Your task is to add the am/pm display.
It is just another digit with a place value of 12 hours, and should show am/pm instead of 0 and 1. Also, it has a special (quite illogical) position at the end, eg. 12:00 am.
To enable the DisplayKara to find and walk to the next DigitDisplayKara, each digit stores the number of steps (which may be
negative as it is simply passed to multiMove() ) in its field stepsToNextDigit
which can be set with the constructor:
public DigitDisplayKara(int rollOverLimit, long placeValue, int stepsToNextDigit)
Make sure 1. Multi-Move and 2. Move up and Count which you should have done in last week’s lab or in class.
There is a special repo with the scaffold for all KaraClock exercises: https://github.com/htw-imi-info1/kara-clock-lab