Question

In: Computer Science

Using Java: Create a class that will hold the information about a clock, called Clock. You...

Using Java:

Create a class that will hold the information about a clock, called Clock. You need to keep minutes, hours and seconds in military time (24-hour) format. Include member functions that will setTime ( this will set the hours, minutes and seconds of the clock), getHours (return the hours), getMinutes (return the minutes) and getSeconds (return the seconds). Another method printTime which will print the time out like this 03:13:09 (hours:minutes:seconds). Overloaded constructors that will take in hours, minutes and seconds or the default constructor that will take in nothing and set hours, minutes and seconds all to 0.

Your class should also include the following methods

incrementSeconds() //A method to increment the time by one second. //The time is incremented by one second. // If the before-increment time is // 23:59:59, the time is reset to 00:00:00.

incrementMinutes() //A method to increment the time by one minute. // The time is incremented by one minute. // If the before-increment time is // 23:59:53, the time is reset to 00:00:53.

incrementHours() //A method to increment the time by one hour. // The time is incremented by one hour. // If the before-increment time is // 23:45:53, the time is reset to 00:45:53.

Show possible calls to do the following

Create a clock that is set to 9:0:0

Create another clock that is set to 9:30:20

Add one hour to the first clock

Add 30 minutes to the second clock

Using your clock class, create a child class, ClockChild. Create a constructor that calls the parent’s default constructor and an overloaded constructor that takes in hours, minutes and seconds and sets the member variables accordingly. Create a printTime method, for the child. Now it will take in a parameter called typeOfClock. This will be an integer that will be either 12 or 24. This tell the function how to print the time, either in military time (the parents type, call the parent method) or in normal type (12-hour) 11:30pm or 9:09am.

Show calls to instantiate two objects. One of the parent type, with the time set to 23:30:30 and one of the child type with the same time set. Show a call to printTime, using both objects.

Solutions

Expert Solution

Given below is the code for question. Please do rate the answer if it helped. Thank you.

Clock.java
----
public class Clock{
   private int hours, minutes, seconds;
   public Clock() {
       setTime(0, 0, 0);
   }
  
   public Clock(int h, int m, int s) {
       setTime(h, m, s);
   }
   public void setTime(int h, int m, int s) {
       seconds = s;
       minutes = m;
       hours = h;
   }

   public int getHours() {
       return hours;
   }

   public int getMinutes() {
       return minutes;
   }

   public int getSeconds() {
       return seconds;
   }
  
   public void printTime() {
       String s = "";
       if(hours < 10)
           s += "0";
      
       s += hours + ":";
      
       if(minutes < 10)
           s += "0";
      
       s += minutes + ":";
      
       if(seconds < 10)
           s += "0";
      
       s += seconds;
      
       System.out.println(s);
      
   }
  
  
   public void incrementSeconds() {
       seconds++;
       if(seconds == 60) {
           seconds = 0;
           incrementMinutes();
       }
   }
  
   public void incrementMinutes() {
       minutes++;
       if(minutes == 60) {
           minutes = 0;
           incrementHours();
       }
   }
  
   public void incrementHours() {
       hours++;
       if(hours == 24) {
           hours = 0;
       }
   }
  
}


ChildClock.java
----

public class ChildClock extends Clock {
   public ChildClock() {
       super();
   }

   public ChildClock(int h, int m, int s) {
       super(h, m, s);
   }
  
   public void printTime(int type) {
       if(type == 12) {
           String ampm = "";
           int h = getHours();
           int m = getMinutes(), s = getSeconds();
          
           if(h >= 12) {
               h -= 12;
               ampm = "pm";
           }
           else{
               ampm = "am";
           }
          
           String str = "";
           if(h < 10)
               str += "0";
          
           str += h + ":";
          
           if(m < 10)
               str += "0";
          
           str += m + ":";
          
           if(s < 10)
               str+= "0";
          
           str += s;
           str += " " + ampm;
          
           System.out.println(str);
       }
       else {
           super.printTime();
       }
   }
}


ClockTest.java
-----

public class ClockTest {
   public static void main(String[] args) {
       Clock c1 = new Clock(9, 0, 0);
       Clock c2 = new Clock(9, 30, 20);
      
       System.out.print("c1 = ");
       c1.printTime();
       System.out.print("c2 = ");
       c2.printTime();
      
       System.out.println("Adding 1 hour to first c1 and 30 mins to c2");
       c1.incrementHours();
      
       for(int i = 1; i<= 30; i++)
           c2.incrementMinutes();
      
       System.out.print("c1 = ");
       c1.printTime();
       System.out.print("c2 = ");
       c2.printTime();
      
       Clock c3 = new Clock(23, 30, 30);
       ChildClock c4 = new ChildClock(23, 30, 30);
      
       System.out.print("parent clock c3 = ");
       c3.printTime();
      
       System.out.print("child clock c4 in 12-hour format = ");
       c4.printTime(12);
      
       System.out.print("child clock c4 in 24-hour format = ");
       c4.printTime(24);
      
   }
}


Related Solutions

Create a class BankAccount to hold at least the following data / information about a bank...
Create a class BankAccount to hold at least the following data / information about a bank account: Account balance Total number of deposits Total number of withdrawals Interest rate e.g., annual rate = 0.05 Service charges per month The class should have the following member functions: Constructor To set the required data. It may be parameterized or user’s input. depositAmount A virtual function used to accept an argument for the amount to be deposited. It should add the argument (amount)...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
Using java, create a class called MyString that has one String called word as its attribute...
Using java, create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this in an application called Jumble that prompts the user for a...
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...
The language is java Write a class called Tablet that stores information about a tablet's age,...
The language is java Write a class called Tablet that stores information about a tablet's age, capacity (in GB), and current usage (in GB). You should not need to store any more information Write actuators and mutators for all instance data Write a toString method When you print a tablet, the info should be presented as such: This tablet is X years old with a capacity of Y gb and has Z gb used. There is A gb free on...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT