Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 (Java) Consider a class Time that represents a time of day. It has att

ID: 3678223 • Letter: P

Question

Problem 1 (Java)

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. Use the provided Time.java as a starting point– do not change the unittests (main

()and the private utility methods followingmain()).

a.Write three constructors: default constructor and two other constructors. A defaultconstructor initializes the time to 0 hours, 0 minutes. Another

two constructors are analogous to the setTime() methods described in Parts c and d.

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

c.Write a method setTime(hour, minute) that sets the time if the given values are valid.Valid hour value ranges from 0 to 23. Valid minute value ranges from 0 to 59.

d.Write another method setTime(hour, minute, isAm ) that sets the time if the given values are valid.The given hour should be in the range 1 to 12.
The parameter isAm is true if the time is an a. m. time and false otherwise.

e.Write a method getTime24() that returns a string in 24-hour notation hhmm. For example, if the hour value is 7 and the minute value is 25, return "0725".

If the hour value is 0 and the minute value is 5, return "0005".If the hour value is 15 and the minute value is 30, return"1530".

f. Write a method getTime12() that returns a string in 12-hour notation h:mm xx. For example, ifthe hour value is 7 and the minute value is 25, return "7:25 am".If the hour

value is 0 and the minute value is 5, return "12:05 am".If the hour value is 15 and the minute value is 30, return "3:30 pm"

Notes:
•You may not use any Java library classes (Date, Time, etc) to manipulate the time.
•You must use exactly 2 instance variables (int hours; int minutes) –you may not have an instance variable to indicate am/pm.-> you always store time in -hour format.

Complete the class Time, write a basic set of unit tests in the main()method within the Time class, and write an interactive program (separate classTestTime) to use your Time class. Your program must prompt for 12- or 24-hour input format or to quit. If 12-hour format is chosen, prompt for hours, minutes, and am or pm, then invoke the 3 parameter version of setTime(). If 24-hour format is chosen, prompt for hours and minutes, then invoke the 2 parameter version of setTime(). Display the time in both 12- and 24-hour formats. Repeat. You must use a switch() statement as the dispatcher to handle the first prompt (12-hour, 24-hour, quit).

Example of expected output (partial):

Valid test: new Time()
Expect:          (0, 0) 0000 12:00 am
Actual:          (0, 0) 0000 12:00 am
Valid test: new Time( 0, 0 )
Expect:          (0, 0) 0000 12:00 am
Actual:          (0, 0) 0000 12:00 am
Error test: new Time( -1, 1 )
Expect:          (0, 0) 0000 12:00 am
Actual:          (0, 0) 0000 12:00 am

Comments: Please provide some comment of the code.

Explanation / Answer

public class Time

{

private int hour;

    private int minute;

public Time()

     //default constructor initializing time to 0 hours, 0 minutes

     {

        hour = 0;

        minute = 0;

     }

private boolean isValid(hour,minute)

     //returns true if given hour & minute values are of range

     {

        if(hour >= 0 && hour <= 23) && (minute >= 0 && minute <=59))

        {

            return true;

        }

        else

        {

            return false;

        }

     }

      

     public void setTime(hour,minute){

     //set time if given values are valid

boolean result=isValid(hour,minute);

if(result==true){

System.out.println("Entered hour and minitue : "+hour+" "+miniute);

}else{

System.out.println("you are entered wrong values of hour and minitue");

}

}

     public void setTime(hour,minute,isAM)

     //sets time if given values are valid

     //hour range: 1 to 12

     //parameter isAM true if the time is an a.m. time, otherwise false

     {

boolean result=isValid(hour,minute);

if(result==true){

isAm:true;

System.out.println("Entered hour and minitue : "+hour+" "+miniute+" "+isAm);

}else{

System.out.println("you are entered wrong values of hour and minitue");

}

  

      

     }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote