Question

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 will be submitting 4 files for part b of this assignment.)

// A Time stores time in ISO 8601 format keeping hours, minutes, and seconds.

public class Time {

private int hours; // in range [0,23]

private int minutes; // in range [0,59]

private int seconds; // in range [0,59]

// constructs a new Time with the given hours, minutes, and seconds

public Time(int hours, int minutes, int seconds) {

setHours(hours);

...

}

// returns the fields' values

public int getHours()...

public int getMinutes()...

public int getSeconds()...

// sets fields values implementing appropriate input validation.

// if input is not valid sets value to zero.

public void setHours(...) ...

public void setMinutes(...) ...

public void setSeconds(...) ...

public void setTime(int hours, int minutes, int seconds) ...

// Advance by 1 second and return this instance Time nextSecond() ...

Time previousSecond() ...

// returns a string such as “Time is 14:25:43”

public String toString(){ ... }

• Write the client class TimeClient that creates an object of class Time and initializes its with time 23:59:59. Print out the time using toString method. Advance the time by 10 seconds using nextSecond method and print the time. Decrease the time by 20 seconds using previous time method and print the time again. Set the time to 25:00:00, and print the time. Set the time to 12:40:86 and print the time. When you execute the TimeClient file you result should look like:

Creating time of 23:59:59

The object time is 23:59:59

Advancing time by 10 seconds

The object time is 00:00:09

Decreasing time by 20 seconds

The object time is 23:59:49

Setting time to 25:00:00

The object time is 00:00:00

Setting time to 12:40:86

The object time is 12:40:00

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

If you have any doubts, please give me comment...

// A Time stores time in ISO 8601 format keeping hours, minutes, and seconds.

public class Time {

private int hours; // in range [0,23]

private int minutes; // in range [0,59]

private int seconds; // in range [0,59]

// constructs a new Time with the given hours, minutes, and seconds

public Time(int hours, int minutes, int seconds) {

setHours(hours);

setMinutes(minutes);

setSeconds(seconds);

}

// returns the fields' values

public int getHours(){

return hours;

}

public int getMinutes(){

return minutes;

}

public int getSeconds(){

return seconds;

}

// sets fields values implementing appropriate input validation.

// if input is not valid sets value to zero.

public void setHours(int h){

if(h>=0 && h<=23)

hours = h;

else

hours = 0;

}

public void setMinutes(int m){

if(m>=0 && m<=59)

minutes = m;

else

minutes = 0;

}

public void setSeconds(int s){

if(s>=0 && s<=59)

seconds = s;

else

seconds = 0;

}

public void setTime(int hours, int minutes, int seconds){

setHours(hours);

setMinutes(minutes);

setSeconds(seconds);

}

// Advance by 1 second and return this instance

Time nextSecond(){

seconds++;

if(seconds>59){

seconds = 0;

minutes++;

if(minutes>59){

minutes = 0;

hours++;

if(hours>23)

hours = 0;

}

}

return this;

}

Time previousSecond(){

seconds--;

if(seconds<0){

seconds = 59;

minutes--;

if(minutes<0){

minutes = 59;

hours--;

if(hours<0)

hours = 23;

}

}

return this;

}

// returns a string such as “Time is 14:25:43”

public String toString(){

return String.format("Time is %02d:%02d:%02d", hours, minutes, seconds);

}

}

public class TimeClient{

public static void main(String[] args) {

System.out.println("Creating time of 23:59:59");

Time t = new Time(23, 59, 59);

System.out.println("The object time is "+t.toString());

System.out.println("Advancing time by 10 seconds");

for(int i=0; i<10; i++)

t.nextSecond();

System.out.println("The object time is "+t.toString());

System.out.println("Decreasing time by 20 seconds");

for(int i=0; i<20; i++)

t.previousSecond();

System.out.println("The object time is "+t.toString());

System.out.println("Setting time to 25:00:00");

t.setTime(25, 0, 0);

System.out.println("The object time is "+t.toString());

System.out.println("Setting time to 12:40:86");

t.setTime(12, 40, 86);

System.out.println("The object time is "+t.toString());

}

}

Add a comment
Know the answer?
Add Answer to:
This is a Java programming assignment and I want help with the Time class. See below...
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
  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • [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...

  • 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...

  • Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from...

    Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from Assignment 1. To get started, you can either make a copy of your Assignment 1 Time.java, or download the solution, Time.java. This is Assignment 1: --------------------------- public class Time { private int h; private int m; public Time() { super(); } public Time(int h, int m){ if(h>=1 && h<=23){ this.h = h; }else{ this.h = 0; } if(m>=0 && m<=59){ this.m = m; }else{...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • 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...

  • 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...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

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