Question

1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30,...

1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30, or 23:59

○ Time is measured in hours (00 – 23) and minutes (00 – 59)

○ Times are ordered from 00:00 (earliest) to 23:59 (latest)

Complete the first constructor of the class Clock

● It takes two arguments: h and m and creates a new clock object whose initial time is h hours and m minutes

● Test cases: Clock clock1 = new Clock(1, 0); System.out.println(clock1);

Complete the second constructor of the class Clock

● It takes one string argument: s

○ s is composed of two digits, followed by a colon, followed by two digits, so the format is HH:MM such as 02:30

○ it creates a new clock object whose initial time is HH hours and MM minutes

● Test cases: Clock clock2 = new Clock("02:30"); System.out.println(clock2); → 02:30

Complete the method toString of the class Clock

● It returns a string representation of this clock, using the format HH:MM

○ that is, the format is the hours (2 digits), followed by a colon, followed by the minutes (2 digits), for example, 00:00 and 23:59

● Test cases: Clock clock1 = new Clock(1, 0); Clock clock2 = new Clock("02:30"); System.out.println(clock1); → 01:00 System.out.println(clock2); → 02:30

Complete the method isEarlierThan of the class Clock

● It returns true if and only if the time on this clock (the current referenced object) is earlier than the time on that clock (in the argument)

● Test cases: Clock clock1 = new Clock(1, 0); Clock clock2 = new Clock("02:30"); System.out.println(clock1.isEarlierThan(clock2)); → true

Complete the method tick of the class Clock

● It adds 1 minute to the time on this clock

○ for example, one minute after 01:00 is 01:01; one minute after 23:59 is 00:00

● Test cases: Clock clock1 = new Clock(1, 0); System.out.println(clock1); → 01:00 clock1.tick(); System.out.println(clock1); → 01:01

Complete the method tock of the class Clock

● It adds delta minute(s) to the time on this clock, where delta is a positive integer

○ for example, 100 minutes after 02:30 is 04:10

● Note that must not use the method in CW1 6.5 tick().

● Test cases: Clock clock2 = new Clock("02:30"); System.out.println(clock2); → 02:30 clock2.tock(100); System.out.println(clock2); → 04:10

Here below, I attached the clock.java class

public class Clock {
private int hours;
private int minutes;

// CW1 6.1
// Creates a clock whose initial time is h hours and m minutes.
public Clock(int h, int m) {
      
      
      
}

// CW1 6.2
// Creates a clock whose initial time is specified as a string, using the format HH:MM.
public Clock(String s) {
      
      
      
}

// CW1 6.3
// Returns a string representation of this clock, using the format HH:MM.
public String toString() {
  
      
      
      
}

// CW1 6.4
// Is the time on this clock earlier than the time on that one?
public boolean isEarlierThan(Clock that) {
  
      
      
      
}

// CW1 6.5
// Adds 1 minute to the time on this clock.
public void tick() {
  
      
      
      
}

// CW1 6.6
// Adds delta minutes to the time on this clock.
public void tock(int delta) {
  
      
      
      
}

// Test client
public static void main(String[] args) {
Clock clock1 = new Clock(1, 0);
Clock clock2 = new Clock("02:30");
      
System.out.println(clock1);
System.out.println(clock2);
      
System.out.println(clock1.isEarlierThan(clock2));
      
clock1.tick();
clock2.tock(100);
      
System.out.println(clock1);
System.out.println(clock2);
      
}
}

Homework Answers

Answer #1


public class Clock {
private int hours;
private int minutes;

public Clock(int h, int m) {
this.hours=h;
this.minutes=m;   
  
}

public Clock(String s) {
   // split the string by character ':'
String [] splt=s.split(":");
// set hours
this.hours=Integer.parseInt(splt[0]);
// set minutes
this.minutes=Integer.parseInt(splt[1]);
  
}

public String toString() {
// return string
return(String.format("%02d:%02d", this.hours,this.minutes));
  
  
}

public boolean isEarlierThan(Clock that) {
   // get time to minute
int minute1=this.hours*60+this.minutes;
int minute2=that.hours*60+that.minutes;
  
// if small return true
if(minute1<minute2){
   return true;
}
// otherwise false
else{
   return false;
}
  
}

public void tick() {
   // increase minute by 1
this.minutes++;
// if minute is 60 then reset to 0
// and increase hour
if(this.minutes==60){
   this.hours++;
   this.minutes=0;
   if(this.hours==24){
       this.hours=0;
   }
}
  
}

public void tock(int delta) {
   // increase minute by delta
   this.minutes+=delta;
   // if minute is more 60 then reset to valid minutes
   // and increase hour by minute/60
   if(this.minutes>=60){
       this.hours+=(this.minutes/60);
       this.minutes%=60;
       if(this.hours==24){
           this.hours=0;
       }
   }
  
  
}

// Test client
public static void main(String[] args) {
Clock clock1 = new Clock(1, 0);
Clock clock2 = new Clock("02:30");
  
System.out.println(clock1);
System.out.println(clock2);
  
System.out.println(clock1.isEarlierThan(clock2));
  
clock1.tick();
clock2.tock(100);
  
System.out.println(clock1);
System.out.println(clock2);
  
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM. (A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below: void input(int& hours24, int& minutes); //Precondition: input(hours, minutes) is called with //arguments capable of being assigned values. //Postcondition: // user is prompted for time in 24 hour format: // HH:MM, where...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
Your solutions to this assignment MUST be implemented in functional style (using lambdas and streams), whenever...
Your solutions to this assignment MUST be implemented in functional style (using lambdas and streams), whenever possible. Needs help with part 3 only:    1.   Complete the Train constructor (verify that the data is correct)    a.   carrier must be one of the following: "Canadian National Railway", "Canadian Pacific Railway", "Hudson Bay Railway Co", "Quebec North Shore and Labrador Railway", "RailLink Canada", "Tshiuetin Rail Transportation", "Via Rail";    b.   trainNumber must be between 1 and 999;    c.   platform must...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56). If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma. TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT