Question

In: Computer Science

Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List...

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

Solutions

Expert Solution

/******************************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 :)


Related Solutions

Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
Create a C++ class with a static member item so that whenever a new object is...
Create a C++ class with a static member item so that whenever a new object is created, the total number of objects of the class can be reported.
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT