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....
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...
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...
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...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT