Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public String timeTick() - Cause clock to advance one minute. If alarm is on and alarm time is reached after advance, this method returns string

public String timeTick() - Cause clock to advance one minute. If alarm is on and alarm time is reached after advance, this method returns string representing the alarm sound (hint use getSound so that duration is taken into account). Otherwise, this method returns an empty string.

How would I create this public method in Java?

This is the code where public String timeTick() will go into. I know that there are a lot of bugs and that the class is not functioning, but here is the context.

/** * 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 * * @author (Kate Plas) * @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 to Expert-Tailored 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