public class Time { //hours in conventional time private int hours; //minutes in
ID: 441516 • Letter: P
Question
public class Time { //hours in conventional time private int hours; //minutes in conventional time private int minutes; //true if afternoon time, false if morning time private boolean afternoon; //Constructs a cutomary time (12 hours, am or pm) from a military time ##:## public Time(String militaryTime) { //Check to make sure something was entered if (militaryTime == null) { System.out.println("You must enter a valid military time." ); } //Check to make sure there are 5 characters else if (//ADD A CONDITION TO CHECK THE LENGTH OF THE STRING) { System.out.println(militaryTime + " is not a valid military time." ); } else { //Check to make sure there's a colon in the correct spot if (//ADD A CONDITION TO CHECK THE COLON POSITION) { System.out.println(militaryTime + " is not a valid military time." ); } //Check to make sure all other characters are digits else if (//ADD A CONDITION TO CHECK FOR A DIGIT) { System.out.println(militaryTime + " is not a valid military time." ); } else if (//ADD A CONDITION TO CHECK FOR A DIGIT) { System.out.println(militaryTime + " is not a valid military time." ); } else if (//ADD A CONDITION TO CHECK FOR DIGIT) { System.out.println(militaryTime + " is not a valid military time." ); } else if (//ADD A CONDITION TO CHECK FOR DIGIT) { System.out.println(militaryTime + " is not a valid military time." ); } else { //ADD CODE TO SEPARATE THE STRING INTO 2 SUBSTRINGS ONE FOR THE HOURS AND THE MINUTES, CONVERTING // THEM TO INTEGERS AND STORING THEM INTO THE APPROPRIATE INSTANCE VARIABLES. //validate hours and minutes are valid values if(hours > 23) { System.out.println(militaryTime + " is not a valid military time." ); } else if(minutes > 59) { System.out.println(militaryTime + " is not a valid military time." ); } //convert military time to conventional time for afternoon times else if (hours > 12) { hours = hours - 12; afternoon = true; System.out.println(this.toString()); } //account for midnight else if (hours == 0) { hours = 12; System.out.println(this.toString()); } //account for noon else if (hours == 12) { afternoon = true; System.out.println(this.toString()); } //morning times don
Explanation / Answer
please rate - thanks
import java.util.*;
public class TimeDemo
{
public static void main (String [ ] args)
{
Scanner keyboard = new Scanner(System.in);
char answer = 'Y';
String enteredTime;
String response;
while (Character.toUpperCase(answer)=='Y')
{
System.out.print("Enter a miitary time " + "using the ##:## form ");
enteredTime = keyboard.nextLine();
Time now = new Time (enteredTime);
System.out.println("Do you want to enter another (Y/N)? ");
response = keyboard.nextLine();
answer = response.charAt(0);
}
}
}
------------------------------------------
public class Time
{
//hours in conventional time
private int hours;
//minutes in conventional time
private int minutes;
//true if afternoon time, false if morning time
private boolean afternoon;
//Constructs a cutomary time (12 hours, am or pm) from a military time ##:##
public Time(String militaryTime)
{
//Check to make sure something was entered
if (militaryTime == null)
{
System.out.println("You must enter a valid military time." );
}
//Check to make sure there are 5 characters
else if (militaryTime.length()!=5)
{
System.out.println(militaryTime + " is not a valid military time." );
}
else
{
//Check to make sure there's a colon in the correct spot
if (militaryTime.indexOf(":")!=2)
{
System.out.println(militaryTime + " is not a valid military time." );
}
//Check to make sure all other characters are digits
else if (!Character.isDigit(militaryTime.charAt(0)))
{
System.out.println(militaryTime + " is not a valid military time." );
}
else if (!Character.isDigit(militaryTime.charAt(1)))
{
System.out.println(militaryTime + " is not a valid military time." );
}
else if (!Character.isDigit(militaryTime.charAt(3)))
{
System.out.println(militaryTime + " is not a valid military time." );
}
else if (!Character.isDigit(militaryTime.charAt(4)))
{
System.out.println(militaryTime + " is not a valid military time." );
}
else
{
//ADD CODE TO SEPARATE THE STRING INTO 2 SUBSTRINGS ONE FOR THE HOURS AND THE MINUTES, CONVERTING
// THEM TO INTEGERS AND STORING THEM INTO THE APPROPRIATE INSTANCE VARIABLES.
hours=Integer.parseInt(militaryTime.substring(0,2));
minutes= Integer.parseInt(militaryTime.substring(3,5));
//validate hours and minutes are valid values
if(hours > 23)
{
System.out.println(militaryTime + " is not a valid military time." );
}
else if(minutes > 59)
{
System.out.println(militaryTime + " is not a valid military time." );
}
//convert military time to conventional time for afternoon times
else if (hours > 12)
{
hours = hours - 12;
afternoon = true;
System.out.println(this.toString());
}
//account for midnight
else if (hours == 0)
{
hours = 12;
System.out.println(this.toString());
}
//account for noon
else if (hours == 12)
{
afternoon = true;
System.out.println(this.toString());
}
//morning times don
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.