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.
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!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
