In: Computer Science
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List alarmTimes Create an accessor and mutator Create a method addAlarmTime which adds an alarm time to your list Create a method deleteAlarmTIme which removes an alarm time from your list Create a method – displayAlarmTimes which prints all alarm times in the list - Create a ‘Clock’ class - include at least 2 members - one member of Clock class needs to be private ClockAlarm clockAlarm - one member is currentTime - create accessors, mutators - create a method setAlarmTime from the Clock class calls methods on the member clockAlarm - create a method removeAlarmTime from the Clock class calls methods on the member clockAlarm - create a method showAlarmTimes from the Clock class calls methods on the member clockAlarm - create two constructors of your Clock class – one which takes two parameters: alarmTime, currentTime, and sets the clock members with both Another constructor which takes only the currentTime & sets that member. - Create a TestHarness class containing a main which gets an instance of a Clock class. The key is having the methods of the Clock class modifying the members of the ClockAlarm class through the clockAlarm member of the Clock class. Set a few times and alarm times thru your Clock class methods and print out the results
/******************************ClockAlarm.java*************************/
import java.util.ArrayList;
import java.util.Iterator;
public class ClockAlarm {
private ArrayList<String> alarmTimes;
public ClockAlarm() {
alarmTimes = new
ArrayList<>();
}
public ArrayList<String> getAlarmTimes()
{
return alarmTimes;
}
public void setAlarmTimes(ArrayList<String>
alarmTimes) {
this.alarmTimes = alarmTimes;
}
public void addAlarmTime(String alarmTime) {
alarmTimes.add(alarmTime);
}
public void removeAlarmTime(String alarmTime) {
Iterator<String> itr =
alarmTimes.iterator();
int flag = 0;
while (itr.hasNext()) {
if
(itr.next().equalsIgnoreCase(alarmTime)) {
flag = 1;
itr.remove();
}
}
if (flag == 0) {
System.out.println("No alarm found!");
}
}
public void displayAlarmTimes() {
for (String string : alarmTimes) {
System.out.println(string);
}
}
}
/************************************Clock.java*************************/
public class Clock {
private ClockAlarm clockAlarm;
private String currentTime;
public Clock(ClockAlarm clockAlarm, String
currentTime) {
super();
this.clockAlarm = clockAlarm;
this.currentTime =
currentTime;
}
public Clock(String currentTime) {
super();
this.currentTime =
currentTime;
clockAlarm = new
ClockAlarm();
}
public ClockAlarm getClockAlarm() {
return clockAlarm;
}
public void setClockAlarm(ClockAlarm clockAlarm)
{
this.clockAlarm = clockAlarm;
}
public String getCurrentTime() {
return currentTime;
}
public void setCurrentTime(String currentTime)
{
this.currentTime =
currentTime;
}
public void setAlarmTime(String alarmTime) {
clockAlarm.addAlarmTime(alarmTime);
}
public void removeAlarmTime(String alarmTime) {
clockAlarm.removeAlarmTime(alarmTime);
}
public void showAlarmTimes() {
clockAlarm.displayAlarmTimes();
}
}
/******************************TestHarness.java*****************************/
public class TestHarness {
public static void main(String[] args) {
Clock clock = new
Clock("5:50PM");
clock.setAlarmTime("4:30AM");
clock.setAlarmTime("2:00AM");
clock.setAlarmTime("6:30PM");
clock.showAlarmTimes();
System.out.println("After removing
alarm");
clock.removeAlarmTime("6:30PM");
clock.showAlarmTimes();
}
}
/*******************output********************/
4:30AM
2:00AM
6:30PM
After removing alarm
4:30AM
2:00AM
Please let me know if you have any doubt or modify the answer, Thanks :)