Question

Implement a class TimeOfDay that stores a time between 00:00:00 and 23:59:59. Supply a constructor TimeOfDay(int...

Implement a class TimeOfDay that stores a time between 00:00:00 and 23:59:59. Supply a
constructor TimeOfDay(int hours, int minutes, int seconds) and accessor methods to get the current
hours, minutes, and seconds. Supply methods

TimeOfDay addSeconds(int seconds)
int secondsFrom(TimeOfDay other)

The first method returns a TimeOfDay object that is the given number of seconds away from the
current object. The second method computes the number of seconds between two TimeOfDay
objects. Use three integers for the hours, minutes, and seconds as the internal representation.

java language
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:


//TimeOfDay class
public class TimeOfDay
{
   //private data members
   private int hours;
   private int minutes;
   private int seconds;
  
   //constructor
   TimeOfDay(int hours, int minutes, int seconds)
   {
       if(hours<0 || hours>=24)
           this.hours = 0;
       else
           this.hours = hours;
          
       if(minutes<0 || minutes>=60)
           this.minutes = 0;
       else
           this.minutes = minutes;
          
       if(seconds<0 || seconds>=60)
           this.seconds = 0;
       else
           this.seconds = seconds;
   }
  
   //method to return hours
   int getHours()
   {
       return this.hours;
   }
  
   //method to return minutes
   int getMinutes()
   {
       return this.minutes;
   }
  
   //method to return seconds
   int getSeconds()
   {
       return this.seconds;
   }
  
   /* method returns a TimeOfDay object that is the given number of seconds
   away from the current object */
   TimeOfDay addSeconds(int seconds)
   {
       int h = seconds / 3600;
      
       seconds = seconds % 3600;
      
       int m = seconds / 60;
      
       int s = seconds % 60;
          
       TimeOfDay td = new TimeOfDay(h, m, s);
      
       if(td.seconds + this.seconds >=60) td.minutes++;
      
       td.seconds = (td.seconds + this.seconds) % 60;
      
       if(td.minutes + this.minutes >=60) td.hours++;
      
       td.minutes = (td.minutes + this.minutes) % 60;
      
       td.hours = (td.hours + this.hours) % 24;
  
       return td;
      
   }
  
   //method computes the number of seconds between two TimeOfDay
   int secondsFrom(TimeOfDay other)
   {
       int tsecs;
       int carry = 0, carry2 = 0;
      
       if(this.seconds > other.seconds){
carry = 1;
tsecs = 60 - this.seconds;
}
else
   tsecs = other.seconds - this.seconds;
  
if(this.minutes > other.minutes + carry){
carry2 = 1;
tsecs = tsecs + (60 - this.minutes) * 60;
}
else
   tsecs = + tsecs + (other.minutes - this.minutes) * 60;
  
tsecs = tsecs + (other.hours - this.hours - carry2) * 3600;
  
return tsecs;
   }

}


//driver class
class Driver
{
   public static void main (String[] args)
   {
       TimeOfDay t1 = new TimeOfDay(12, 34, 55);
       TimeOfDay t2 = new TimeOfDay(8, 12, 15);
      
       int nsecs = t2.secondsFrom(t1);
      
       System.out.println("Time difference in seconds: " + nsecs);
      
       TimeOfDay t3 = t2.addSeconds(nsecs);
      
       System.out.println("After adding " + nsecs + " seconds new time is " +
           t3.getHours() + ":" + t3.getMinutes() + ":" + t3.getSeconds());
          
   }
}

Output:

Time difference in seconds: 15760
After adding 15760 seconds new time is 12:34:55

Add a comment
Know the answer?
Add Answer to:
Implement a class TimeOfDay that stores a time between 00:00:00 and 23:59:59. Supply a constructor TimeOfDay(int...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • In this practical task, you need to implement a class called MyTime, which models a time...

    In this practical task, you need to implement a class called MyTime, which models a time instance. The class must contain three private instance variables: hour, with the domain of values between 0 to 23. minute, with the domain of values between 0 to 59. second, with the domain of values between 0 to 59. For the three variables you are required to perform input validation. The class must provide the following public methods to a user: MyTime() Constructor. Initializes...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...

    Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object. I need the main method or test class public class Die { private final int MAX = 6;...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...

    Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, new java.util.Date().toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutesmethods. (2) Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT