Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Consider a class Time that represents a time of day. It has attributes for the hour and minute . The hour value ranges from 0

Consider a class Time that represents a time of day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. This Exercise is different from the textbooks version.

Write a default constructor that initializes the time to 0 hours, 0 minutes.

Write a private method isValid(hour, minute) that returns true (a boolean) if the given hour and minute values are both in the appropriate range, false otherwise.

Write a void method setTime(hour, minute) that sets the time only if the given values are valid; if one or both are not valid, the time is unchanged.

Write another version of setTime(hour, minute, isAM) that sets the time if the given values are valid. The given hour should be in the range 0 to 11. The boolean parameter isAm is true if the time is an a.m. time and false if it is a p.m. time.

Hint: The second version of setTime() (an overloaded method) must check to make sure that hour is <= 11, and should convert a p.m. hour (one where isAm is false) to that hour + 12 to convert it to a correct afternoon time; in either case, it should then just call the first version of setTime().

Write a toString() method to return the current time in hours and minutes as a String.

The toString() method should return a String that looks like this, using the current instance variables:

hour:  minute: 

IN JAVA:

public class Time // Week 8 Tuesday Lab, part 3 (extra credit)

{

private int theHour;

private int theMinute;

/**

* Create a new instance of Time

*/

/* fill in the default constructor here */

/* create the private isValid method here */

/* create the first version of setTime here

it should only change the instance variables

if the hour and minute are valid */

/* create the second version of setTime here

if isAM is false it should adjust hour by adding

12 to it; in either case it should then call the

first version of setTime */

@Override

public String toString()

{

/* write the toString method to return the above String */

}

/**

* @param args the command line arguments

*/

public static void main(String[] args)

{

/* DO NOT MODIFY THIS MAIN METHOD */

System.out.println("Create the time 15, 19");

time2 = new Time();

time2.setTime(15, 19);

System.out.println(time2);

time2.setTime(3, 19, false); // should be the same as above

System.out.println(time2);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students explore these related Databases questions

Question

=+4 How did it affect HR?

Answered: 3 weeks ago