Question: Written in Java: 1. You are given the following class. class Time { private int hours, minutes, seconds; } a. Create a default constructor which

Written in Java:

1. You are given the following class.

class Time {

private int hours, minutes, seconds;

}

a. Create a default constructor which sets the following:

hours = 0;

minutes = 0;

seconds = 0;

b. Create an overloaded constructor that allows you to initialize all of the instance members.

Test that hours are from 0 to 23, otherwise set the hours to 0.

Test that minutes are from 0 to 59, otherwise set minutes to 0.

Test that seconds are from 0 to 59, otherwise set seconds to 0.

c. Create a method named secpast12 which returns an integer value of hours*3600 + minutes*60 + seconds.

d. Create a toString method that prints hours:minutes:seconds.

e. Add the get and set methods for all variables with data checking.

2. Create a subclass of Time named TimeTwo. The class TimeTwo has additional instance variable boolean ampm. Create a set and get method for ampm.

3. Change the access permissions on the variables in the Time class so that only Time and TimeTwo can access them.

4. TimeTwo should have two constructors. Create a default constructor which will call the default superclass constructor to initialize hours, minutes, and seconds to 0 and set ampm to be false. Create an overloaded constructor which will call the second superclass constructor to initialize hours, minutes, and seconds and initialize ampm.

5. Create a method isPM which will set ampm to true if hours >= 12 and false if hours < 12.

6. Override the toString method. Return a string which calls the superclass toString method and has ampm.

7. Write a driver to create the objects named TimeDriver.

Time t = new Time(1, 5, 3);

TimeTwo time2 = new TimeTwo(12, 13, 58, true);

Call the get method for all instance variables for time2.

Call the secpast12 method for time2.

Call the toString method for t and time2.

Print out the code and the output.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!