Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1) Create a public class called Tester. -Create an empty constructor with no parameters: public Tester(){ // no initialization needed } - Create at least

1) Create a public class called Tester.

-Create an empty constructor with no parameters:

 public Tester(){ // no initialization needed 

}

- Create at least 2 specific public methods that will test your AlarmClock class:

- public boolean testAlarm should have the following structure:

- CreateanAlarmClockobjectwithclocktime15:03 - Set the alarm time to 3:05 pm - Turn the alarm on - Advance the clock time one minute, checking to see if the alarm sounds. If it sounds after the first minute (which is . too early), return false. - Otherwise, advance the clock time another minute. If it sounds after the second minute (which is the correct time), . return true. Otherwise, return false.

- public boolean testNoAlarm should have the following structure:

- CreateanAlarmClockobjectwithclocktime6:59 - Set the alarm time to 8:05 am - Turn the alarm on - Advance the clock time one minute, checking to see if the alarm sounds. If it sounds after the first minute (which is too early), return false.

- Otherwise, advance the clock time another minute. If it sounds after the second minute (which is too early),return false. Otherwise, return true.

- Create a public method (public boolean testDuration) that tests setDuration of your AlarmClock class. The method should return true if the test is passed and false otherwise. In your comments before this method, explain what you are testing.

- Create a public method (public boolean testSnooze) that tests snooze of your AlarmClock class. The method should return true if the test is passed and false otherwise. In your comments before this method, explain what you are testing.

- Create a public method (public int runAllTests()) that runs all 4 tests and returns an int representing the number of tests that were passed.

Here is the context that this question refers to.

/** * This is an alarm clock that can be set to a certain time and turned * on/off. When it is on, it will "sound" and alarm when the alarm * is reached. The default sound is "buzz" but may be customized through * use of mutator. There is also a snooze that resets the alarm for 4 * minutes more and an indicator for duration of alarm sound. Note that *time is ketp as a 24 hour time while the alarm is set using 24 hour time * * * @version (10 February 2020) */ public class AlarmClock { private ClockDisplay clock; // the basic clock private boolean alarmOn; // indicates if alarm is on private ClockDisplay alarm; // alarm (12-hour clock) private String sound; // sound alarm makes private int duration; // indicates how many times sound should be //repeated per alarm (must be 1 or 2) /** * Constructor for objects of class AlarmClock */ public AlarmClock() { // initialise instance variables clock = new ClockDisplay(); alarmOn = false; alarm = new ClockDisplay(); sound = "buzz"; duration = 1; alarm.toggleDisplay(); } public AlarmClock(int hour, int minute) // { clock = new ClockDisplay(hour, minute); alarmOn = false; alarm = new ClockDisplay(); sound = "beep"; alarm.toggleDisplay(); } //PART C /* Turns off the Alarm */public void turnAlarmOff() { alarmOn = false; } /* Turns the Alarm on * */ public void turnAlarmOn() { alarmOn = true; } /*Checks to see if alarm is on-- returns true if on, false otherwise * */ public boolean alarmIsOn() { if (alarmOn){ return true; } else { return false; } } /* Returns a string containing the time on the clock. * */ public String getTime(){ return clock.getTime(); } /* returns a returns a string containing the time on the clock. * The string is in 24-hour format from ClockDisplay's getTime method */ public String getAlarmTime(){ return alarm.getTime(); } /* Returns a String that is alarm sound. * */ public String getSound(){ if (duration == 1){ return sound; } else{ return sound + " " + sound; } } /* if newSound is not an empty string, set sound to newSound. Otherwise * leave sound unchanged */ public void setSound(String newSound){ if (str.length() > 0){ //NEED HELP PART C sound = newSound; } else{ return sound; } } /* if d is 1 or 2, set the duration to d. Otherwise, print an error * message and leave duration unchanged. The sound field should not be * changed by this method */ public void setDuration(int d){ //PART C: LAST BULLET if (duration == 1||2){ duration = d; } else{ system.out.println("Error"); //How do you leave the duration unchanged? } } //PART B /*Sets alarm to the 24 hour clock value of hour:minute with am * determining whether the hour is am or pm */ public void setAlarm(int hour, int minute, boolean am){ if (!(hour >= 1 && hour <= 12 && minute >= 0 && minute <= 59)){ System.out.println("error"); } else { if (hour >= 1 && hour <= 11 && am == true){ alarm.setTime(hour, minute); } if (hour >= 1 && hour <= 11 && am == false){ hour = hour + 12; alarm.setTime(hour, minute); } if (hour = 12 && hour <= 11 && am == true){ hour = hour - 12; alarm.setTime(hour, minute); } if (hour = 12 && hour <= 11 && am == false){ hour = hour; alarm.setTime(hour, minute); } } } /* Sets clock to hour :minute. If they are not valid, prints an error * and doesn't change clock time from its previous value. */ public void setTime(int hour, int minute){ if (0 <= hour && hour <= 23){ hour = hour; } else{ System.out.println("Invalid value for hour"); } if (0 <= minute && minute <= 59){ minute = minute; } else { System.out.println("Invalid value for minute"); } } /* Cause clock to advance one minute. * */ public String timeTick(){ if (alarmOn){ return sound; } else { return " "; } } /* Checks to see if alarm has just sounded * If it has sounded, the alarm time is reset to be 4 minutes later * than currentalarm time */ public void snooze(){ if (alarmOn){ setTime; } } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public int sampleMethod(int y) { // put your code here return clock.getTime(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions