Homework 4 Understanding Class Definitions
|
Modified:
|
Overview - The purpose of the homework is to review Chapter 3 concepts
and additional practice skills needed throughout the course.
Problem description - Chapter 3 implemented a ClockDisplay class which
will be extended to include alarm clock features.
An alarm clock holds the alarm hour and minute, which it compares on every
tick with the current hour and minute. If the alarm is on and the alarm hour and
minute equal the clock hour and minute, the alarm is sounded. The alarm hour and
minute can be set, and the alarm can be turned on and off.
Assignment
From the View menu, Show Terminal, then Record method
calls. Clear the window before starting.
- ClockDisplay
- Exercise a
- Add to ClockDisplay class a printTime method to print
displayString to the terminal window, use the getTime
method.
- Create a ClockDisplay instance and calling printTime.
- Exercise b - Add fields.
- Add two fields for the alarm hour and minute, alarmHour and
alarmMinute, of the NumberDisplay class.
- Add a field alarm of type boolean.
- Modify both ClockDisplay constructors to:
- initialize alarm to false,
- initialize alarmHour and alarmMinute fields to
NumberDisplay limits of 24 and 60 respectively (i.e. as is already done
for hours and minutes in ClockDisplay).
- Create an instance for both constructors and Inspect to verify.
- Exercise c - Debugger
- Set a breakpoint in the public ClockDisplay(int hour, int minute)
constructor at the statement:
- hours = new NumberDisplay(24);
- Create an instance using the constructor.
- Click Step until each NumberDisplay constructor has been
called.
- In Debugger window, double-click on alarmHour to open
an Inspector window.
- Copy the Debugger and Inspector window to a Word
document.
- Click Continue to complete constructor execution.
- Exercise d
- Add a mutator method setAlarm that sets alarmHour and
alarmMinute fields. Neither field should be changed if either the
alarm hour parameter is negative or above 23, or alarm minute parameter is negative or above
59.
- Hint: Call the NumberDisplay setValue() on each of the two
fields.
- Add a method printAlarm method to print alarmHour and
alarmMinute fields to the terminal window in the form of:
hour : minute
Hint: Use getValue() method.
- Create a ClockDisplay instance.
- Set a breakpoint at the first statement of setAlarm.
- For each of the following 3 tests:
- Execute the setAlarm method.
- Click Step one time to execute the if-statement
of the method to determine the next statement executed.
- Copy Debugger window to the Word document.
- Set the alarm to 13:27 and print the alarm setting.
- Set the alarm to -13:27 and print the alarm setting.
- Set the alarm to 24:27 and print the alarm setting.
- Exercise e
- Add mutator methods alarmOn that sets alarm field true
and alarmOff that sets alarm field false.
- Create an instance and Inspect to verify when calling each
method.
- Exercise f
- Add method printBeep that:
- prints "Beep Beep Beep" to terminal window
- calls printAlarm method
- Create an ClockDisplay instance and call printBeep method.
- Exercise g
- Add a method checkAlarm that:
- if
- alarm is true and
- the value (use getValue method) of alarmHour equals
hours and
- the value (use getValue method) of alarmMinute
equals minutes
- then call the printBeep method
- else do nothing
- In method timeTick() add a call to method checkAlarm()
after the call to updateDisplay(). This will check the alarm for
each clock tick.
- Create an instance
- call setTime method to set clock time to 13:27
- call setAlarm method to set alarm time to 13:29
- call alarmOn method to set alarm to true
- call timeTick method 3 times
- You should see "Beep Beep" in the Terminal window.
- Exercise h.
- Add sound using SoundClass
- Create a new class named SoundClass. Copy and paste the
following as the new class definition:
import java.applet.*;
import java.io.*;
import java.net.*;
public class SoundClass
{
public void play(String fileName)
{
AudioClip testClip;
URL testUrl;
try{
testUrl=new
URL("file:" + new File(".").getCanonicalPath() + "/" +
fileName);
testClip=Applet.newAudioClip(testUrl);
testClip.play();
} catch(Exception e){
System.out.println(e.toString()); }
}
} |
- Download and save a WAV file into the same
directory as your Java file.
- To test, create a SoundClass object and call the play()
method using the WAV file name as the parameter, (e.g. "HW4.wav").
- You may want to record or use a different WAV file.
- Add a method soundAlarm to ClockDisplay that:
- creates a SoundClass object by:
SoundClass sound = new SoundClass();
- plays a WAV sound file using the SoundClass and
play() method described above.
- To test, create a ClockDisplay instance and call the
soundAlarm() method.
- Modify the checkAlarm method to call both the printBeep() and
soundAlarm() methods at the same time.
- Create a ClockDisplay instance
- call setTime method to set clock time to 13:27
- call setAlarm method to set alarm time to 13:29
- call alarmOn method to set alarm to true
- call timeTick method 3 times
- "Beep Beep" should print and the annoying alarm sound should
play.
- Copy the complete ClockDisplay source code and Terminal
Window to Word and save as HW4 document.
Turn in
C201 OnCourse Dropbox - Place the Word document in C201 OnCourse
dropbox as HW4.