Need help with a java program Driver class should loop until “q” is entered to q
ID: 3765080 • Letter: N
Question
Need help with a java program
Driver class should loop until “q” is entered to quit
If enter is not “q”, then create an instance of the Time object passing the entry as an argument
If no error
Print time by calling print method in Time class
If error
Print error message (see sample)
Time.java class
Time objects should store the time in 2 integer instance variables, hour and minute, and include a string variable to hold the error. This should be initialized with null.
Time Constructor:
Receive time as a string
Perform error checking to ensure time was entered in proper format to include a color (:) between the digits
Use indexOf and substring to separate the time string into the appropriate instance variables
Check hour to ensure is between 1 and 23
Check minute to ensure is between 0 and 59
If an error occurs, change the error instance variable to reflect the error (see sample)
print method:
Include a separate method to print the date using printf to output time in digits for the hour and 2 digits for the minutes
Print AM if before noon and PM if after noon. Do NOT print in military time
getError method:
Include a getError method that returns the error instance variable to the print method
Sample Output:
(requires no user input)
Enter time in the form mm:dd ("q" to quit): 5:20
05:20 AM
Enter time in the form mm:dd ("q" to quit): 05:20
05:20 AM Enter time in the form mm:dd ("q" to quit): 05:80
Invalid minute entered: 80
Enter time in the form mm:dd ("q" to quit): 15:20
03:20 PM
Enter time in the form mm:dd ("q" to quit): 25:20
Invalid hour entered: 25
Enter time in the form mm:dd ("q" to quit): 0520
Invalid separator entered: :
Enter time in the form mm:dd ("q" to quit): q
Explanation / Answer
public class time
{
Scanner stdIn = new Scanner(System.in);
boolean valid = false
int hour;
int mint;
String error = null;
String hourStr;
String mintStr;
String timeStr;
public Time(String TimeStr)
{
colon = timeStr.indexOf("::");
System.out.println (colon);
hour = Integer.parseInt( timeStr.substring( 0, 2 ) );
minute = Integer.parseInt( timeStr.substring( 2, 4 ) );
System.out.println(hour);
System.out.println("Test working");
Integer.parseInt(timeStr);
if((hour<=24)&&(hour>0))
{
System.out.println(hour);
}
Integer.parseInt(timeStr);
if((mint<=60)&&(mint>0))
{
System.out.println(mint);
}
******************************************************
do
{
System.out.print("Enter a time in the form mm::dd ('q' to quit): ");
value = stdIn.next();
try
{
if ((!value.equalsIgnoreCase("q")) || (!value.equals("")))
{
timevalue = new Time(value);
timevalue.printTime();
quit = false;
}
else
{
quit = true;
}
}//end try
catch (StringIndexOutOfBoundsException sioobe)
{
//timevalue.getError();
System.out.println("Error, you must enter a valid time, try again"); //e.getError());
stdIn.next(); // Invalid input is still in the buffer so flush it
quit = false;
} // end catch
} while (!quit); //(!value.equalsIgnoreCase("q"));
// } //endloop