THE ORIGINAL ASSIGNMENT: Write a program that prompts the user to enter time in
ID: 3825120 • Letter: T
Question
THE ORIGINAL ASSIGNMENT:
Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your program must contain three exception classes: InvalidHrExcep, InvalidMinExcep, and InvalidSecExcept. If the user enters an invalid value for hours, then the program should throw and catch an InvalidHr object. Similar conditions for Minutes and Seconds.
WHAT I HAVE SO FAR:
import java.io.*;
import java.util.*;
public class ch13_ex1 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int hours;
int minutes;
int seconds = 0;
String str;
hours = getHours();
minutes = getMinutes();
seconds = getSeconds();
System.out.print("Enter AM or PM: ");
str = input.next();
System.out.println();
System.out.print("24 hour clock time: ");
print24HourTime(hours, str);
}// end of main
private static int getSeconds() {
// TODO Auto-generated method stub
return 0;
}
private static int getMinutes() {
// TODO Auto-generated method stub
return 0;
}
public static int getHours()
{
boolean done = false;
int hr = 0;
do
{
try
{
System.out.print("Enter hours: ");
hr = input.nextInt();
System.out.println();
if (hr < 0 || hr > 12)
throw new InvalidHrException();
done = true;
}// end of try loop
catch (InvalidHrException hrObj)
{
System.out.println(hrObj);
}//end of catch
} while (!done); //end of do loop
return hr;
}//end gethours
public static void print24HourTime(int hr, String str)
{
if (str .equals("AM"))
{
if (hr == 12)
System.out.print("");
else
System.out.print(hr);
}//end of if AM
else if (str.equals("PM"))
{
if (hr == 12)
System.out.print(hr);
else
System.out.print(hr + 12);
}//end else if
}//end of print24HourTime
}// end of class
--------------------------------------
InvalidHrException class:
public class InvalidHrException extends Exception {
private String message;
public InvalidHrException() //default construction
{
message = "The value of hours must be between 1 and 12.";
}
public InvalidHrException(String str)
{
message = str;
}
public String getMessage()
{
return message;
}
public String toString()
{
return message;
}
}//end class
Really, I just need help figuring out the classes for the minutes and seconds. I'm not even sure how to really go about doing it, and with what I have experimented so far with, just gives me a bunch of errors that I have no clue how to fix. I have tried really hard to figure it out and I just can't get it. Please help, thank you!
Explanation / Answer
Below is your updated code.I have changed the code to bold which I have modified: -
ch13_ex1.java
import java.util.Scanner;
public class ch13_ex1 {
static Scanner input = new Scanner(System.in);
static int hours;
static int minutes;
static int seconds = 0;
static String str;
public static void main(String[] args) {
// TODO Auto-generated method stub
hours = getHours();
System.out.print("Enter AM or PM: ");
str = input.next();
System.out.println();
minutes = getMinutes();
seconds = getSeconds();
System.out.print("24 hour clock time: ");
print24HourTime(hours, str);
System.out.println(":"+minutes+":"+seconds);
}// end of main
private static int getSeconds() {
boolean done = false;
int sec = 0;
do
{
try
{
System.out.print("Enter Seconds: ");
sec = input.nextInt();
System.out.println();
if (sec < 0 || sec > 60)
throw new InvalidSecExcept();
done = true;
}// end of try loop
catch (InvalidSecExcept hrObj)
{
System.out.println(hrObj);
}//end of catch
} while (!done); //end of do loop
if(sec == 60) {
sec = 0;
if(minutes+1 == 60) {
minutes = 0;
if(hours+1 == 12) {
hours = 0;
if(str.equals("AM")) {
str = "PM";
} else {
str = "AM";
}
} else {
hours = hours+1;
}
} else {
minutes = minutes+1;
}
}
return sec;
}
private static int getMinutes() {
boolean done = false;
int min = 0;
do
{
try
{
System.out.print("Enter Minutes: ");
min = input.nextInt();
System.out.println();
if (min < 0 || min > 60)
throw new InvalidMinExcep();
done = true;
}// end of try loop
catch (InvalidMinExcep hrObj)
{
System.out.println(hrObj);
}//end of catch
} while (!done); //end of do loop
if(min == 60) {
min = 0;
if(hours+1 == 12) {
hours = 0;
if(str.equals("AM")) {
str = "PM";
} else {
str = "AM";
}
} else {
hours = hours+1;
}
} else {
min = min+1;
}
return min;
}
public static int getHours()
{
boolean done = false;
int hr = 0;
do
{
try
{
System.out.print("Enter hours: ");
hr = input.nextInt();
System.out.println();
if (hr < 0 || hr > 12)
throw new InvalidHrException();
done = true;
}// end of try loop
catch (InvalidHrException hrObj)
{
System.out.println(hrObj);
}//end of catch
} while (!done); //end of do loop
return hr;
}//end gethours
public static void print24HourTime(int hr, String str)
{
if (str.equals("AM"))
{
if (hr == 12)
System.out.print("");
else
System.out.print(hr);
}//end of if AM
else if (str.equals("PM"))
{
if (hr == 12)
System.out.print(hr);
else
System.out.print(hr + 12);
}//end else if
}//end of print24HourTime
}// end of class
InvalidHrException.java
public class InvalidHrException extends Exception {
private String message;
public InvalidHrException() //default construction
{
super("The value of hours must be between 1 and 12.");
message = "The value of hours must be between 1 and 12.";
}
public InvalidHrException(String str)
{
super(str);
message = str;
}
public String getMessage()
{
return message;
}
public String toString()
{
return message;
}
}//end class
InvalidMinException.java
public class InvalidMinExcep extends Exception{
private String message;
public InvalidMinExcep() //default construction
{
super("The value of minutes must be between 1 and 60.");
message = "The value of minutes must be between 1 and 60.";
}
public InvalidMinExcep(String str)
{
super(str);
message = str;
}
public String getMessage()
{
return message;
}
public String toString()
{
return message;
}
}
InvalidSecExcept.java
public class InvalidSecExcept extends Exception{
private String message;
public InvalidSecExcept() //default construction
{
super("The value of Seconds must be between 1 and 60.");
message = "The value of Seconds must be between 1 and 60.";
}
public InvalidSecExcept(String str)
{
super(str);
message = str;
}
public String getMessage()
{
return message;
}
public String toString()
{
return message;
}
}
Sample Run:
Enter hours: 12
Enter AM or PM: AM
Enter Minutes: 60
Enter Seconds: 60
24 hour clock time: 13:1:0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.