Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are going to create a clock in this project and use a class while doing it.

ID: 3816665 • Letter: Y

Question

You are going to create a clock in this project and use a class while doing it. So the class will be the clock called Clock.java and then you will have another JAVA file that runs it called ClockApp.java. Below are the requirements for the clock and the clockapp that runs it. Requirements: 1. Your Clock.java must be the class. 2. Your ClockApp.java is the main program. 3. Your clock will be a 24 hour clock 4. You will create the following public member functions for the clock class. a. clock() //Default constructor with parameters //Post: time is set to 00:00:00 //hr = 0; min = 0; sec = 0 b. clock(int hours, int minutes, int seconds) //Constructor with parameters //Post: The time is set according to //the parameters //hr = hours; min = minutes; sec = seconds c. void setTime(int hours, int minutes, int seconds) //Function to set the time //Post: time is set according to the //parameters: hr = hours; min = minutes; sec = seconds d. void printTime() //Function to print the time //Time is printed in the form hh:mm:ss e. void incrementSeconds() //Function to increment the time by 1 second //Post: The time is incremented by 1 second //If the before-increment time is 23:59:59, the time //is reset to 00:00:00 f. void incrementMinutes() //Function to increment the time by 1 minute //Post: The time is incremented by 1 minute //If the before-increment time is 23:59:53, the time //is reset to 00:00:53 g. void incrementHours() //Function to increment the time by 1 hour. //Post: The time is incremented by 1 hour. //If the before-increment time is 23:45:53, time //is reset to 00:45:53 h. bool equalTime(const clockType otherClock) //Function to compare the two times //Function returns true if this time is equal to //otherClock; otherwise it returns false 5. The ClockApp will then run the clock. You need to test all of the functionality you have built in. 6. While testing the functionality create a loop and increment the seconds for a while.

Explanation / Answer

//Clock.java

//Class definition
class Clock
{
   //Instance variable
   int hours;
   int minutes;
   int seconds;
   //Default constructor
   Clock()
   {
       hours = minutes = seconds = 00;
   }//End of constructor
  
   //Parameterized constructor
   Clock(int hours, int minutes, int seconds)
   {
       this.hours = hours;
       this.minutes = minutes;
       this.seconds = seconds;
   }//End of constructor
  
   //Method to set Time
   void setTime(int hours, int minutes, int seconds)
   {
       this.hours = hours;
       this.minutes = minutes;
       this.seconds = seconds;
   }//End of setTime method method
  
   //Function to print the time
   void printTime()
   {
       String sec = "";
       String min = "";
       String hour = "";

       if(seconds <= 9)
           sec += "0" + String.valueOf(seconds);
       else
           sec += String.valueOf(seconds);
       if(minutes <= 9)
           min += "0" + String.valueOf(minutes);
       else
           min += String.valueOf(minutes);
       if(hours <= 9)
           hour += "0" + String.valueOf(hours);
       else
           hour += String.valueOf(hours);
       //Time is printed in the form hh:mm:ss
       System.out.println(hour + ":" + min + ":" + sec);
   }//End of printTime method
  
   //Function to increment the time by 1 second
   void incrementSeconds()
   {
       seconds++;
       if(seconds > 59)
       {
           seconds = 00;
           incrementMinutes();
       }//End of if
   }//End of incrementSeconds method
  
   //Function to increment the time by 1 minute
   void incrementMinutes()
   {
       minutes++;
       if(minutes > 59)
       {
           minutes = 00;
           incrementHours();
       }//End of if
   }//End of incrementMinutes method
  
   //Function to increment the time by 1 hour.
   void incrementHours()
   {
       hours++;
       if(hours > 24)
       {
           hours = 00;
       }//End of if
   }//End of incrementHours method

   //Function to compare the two times
   boolean equalTime(final Clock c1, final Clock c2)
   {
       //Function returns true if this time is equal to
       if((c1.seconds == c2.seconds) && (c1.minutes == c2.minutes) && (c1.hours == c2.hours))
           return true;
       //Otherwise it returns false
       else
           return false;
   }//End of method equalTime
}//End of class

//ClockApp.java

//Driver class
public class ClockApp
{
   //Main method
   public static void main(String ss[])
   {
       //Creates 3 Clock class objects
       Clock c1 = new Clock();
       Clock c2 = new Clock();
       Clock c3 = new Clock();
       //Sets the time with the help of setTime method call
       c1.setTime(24, 58, 56);
       c2.setTime(24, 58, 56);
       c3.setTime(24, 58, 56);
       //Calls the incrementSeconds() method for 69 times to check the time increment
       for(int x = 0; x < 69; x++)
       {
           c1.incrementSeconds();
           c1.printTime();
       }//End of for loop
       //Calls equalTime() method to check c1 and c2 object time is equal to not
       //If true displays equal
       if(c1.equalTime(c1, c2))
       {
           System.out.println("First Clock Time: ");
           c1.printTime();
           System.out.println("Second Clock Time: ");
           c2.printTime();
           System.out.println("Both times are equal");
       }//End of if
       //Otherwise not equal
       else
       {
           System.out.println("First Clock Time: ");
           c1.printTime();
           System.out.println("Second Clock Time: ");
           c2.printTime();
           System.out.println("Both times are not equal");
       }//End of else
      
       //Calls equalTime() method to check c2 and c3 object time is equal to not
       //If true displays equal
       if(c1.equalTime(c2, c3))
       {
           System.out.println("First Clock Time: ");
           c2.printTime();
           System.out.println("Second Clock Time: ");
           c3.printTime();
           System.out.println("Both times are equal");
       }//End of if
       //Otherwise not equal
       else
       {
           System.out.println("First Clock Time: ");
           c2.printTime();
           System.out.println("Second Clock Time: ");
           c3.printTime();
           System.out.println("Both times are not equal");
       }//End of else
   }//End of main method  
}//End of class

Output:

package myProject;
//Driver class
public class ClockApp
{
   //Main method
   public static void main(String ss[])
   {
       //Creates 3 Clock class objects
       Clock c1 = new Clock();
       Clock c2 = new Clock();
       Clock c3 = new Clock();
       //Sets the time with the help of setTime method call
       c1.setTime(24, 58, 56);
       c2.setTime(24, 58, 56);
       c3.setTime(24, 58, 56);
       //Calls the incrementSeconds() method for 69 times to check the time increment
       for(int x = 0; x < 69; x++)
       {
           c1.incrementSeconds();
           c1.printTime();
       }//End of for loop
       //Calls equalTime() method to check c1 and c2 object time is equal to not
       //If true displays equal
       if(c1.equalTime(c1, c2))
       {
           System.out.println("First Clock Time: ");
           c1.printTime();
           System.out.println("Second Clock Time: ");
           c2.printTime();
           System.out.println("Both times are equal");
       }//End of if
       //Otherwise not equal
       else
       {
           System.out.println("First Clock Time: ");
           c1.printTime();
           System.out.println("Second Clock Time: ");
           c2.printTime();
           System.out.println("Both times are not equal");
       }//End of else
      
       //Calls equalTime() method to check c2 and c3 object time is equal to not
       //If true displays equal
       if(c1.equalTime(c2, c3))
       {
           System.out.println("First Clock Time: ");
           c2.printTime();
           System.out.println("Second Clock Time: ");
           c3.printTime();
           System.out.println("Both times are equal");
       }//End of if
       //Otherwise not equal
       else
       {
           System.out.println("First Clock Time: ");
           c2.printTime();
           System.out.println("Second Clock Time: ");
           c3.printTime();
           System.out.println("Both times are not equal");
       }//End of else
   }//End of main method  
}//End of class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote