need help Chapter 9 Practice Program 4 Java Project Name IC
need help :(
Chapter 9, Practice Program 4
Java Project Name: IC22_Time
Write a Java console program that allows a user to enter a time, which your code will validate and then indicate whether or not the time is valid. The time will be in the format hour:minute followed by \"am\" or \"pm\".
Create three exception classes (InvalidHourException, InvalidMinuteException and InvalidMeridiemException) that will indicate a specific type of error in the time entered by the user. For example, InvalidHourException could be used to indicate that the value entered for hour was not an integer in the range 1 to 12.
Use a catch block for each different kind of exception.
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
########### Exception Classes ##########
class InvalidHourException extends Exception {
public InvalidHourException(String msg){
super(msg);
}
}
class InvalidMinuteException extends Exception{
public InvalidMinuteException(String msg) {
super(msg);
}
}
class InvalidMeridiemException extends Exception{
public InvalidMeridiemException(String msg) {
super(msg);
}
}
############ Time clas ###########
import java.util.Scanner;
public class Time {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try{
// getting user input
System.out.print(\"Enter time (hours:minutes<space>meridiem ): \");
String time = sc.nextLine();
// getting index of \':\'
int index = time.indexOf(\':\');
if(index == -1){ // not available
throw new Exception(\"Time is worng\");
}
int hour = Integer.parseInt(time.substring(0, index));
if(hour > 12 || hour < 1){
throw new InvalidHourException(\"value entered for hour was not an integer in the range 1 to 12\");
}
time = time.substring(index+1);
// finding index of space
index = time.indexOf(\' \');
if(index == -1){ // not available
throw new Exception(\"Time is worng\");
}
int minutes = Integer.parseInt(time.substring(0, index));
if(minutes > 59 || minutes < 0){
throw new InvalidMinuteException(\"value entered for minute was not an integer in the range 0 to 59\");
}
// validting meridiem
String meridiem = time.substring(index+1);
//System.out.println(meridiem);
if(!(\"am\".equalsIgnoreCase(meridiem.trim()) || \"pm\".equalsIgnoreCase(meridiem.trim()))){
throw new InvalidMeridiemException(\"value entered for meridiem was not an am or pm\");
}
System.out.println(\"Time entered is valid\");
}catch(InvalidHourException e){
System.out.println(e.getMessage());
}catch(InvalidMinuteException e){
System.out.println(e.getMessage());
}catch(InvalidMeridiemException e){
System.out.println(e.getMessage());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
/*
Sample run:
Enter time (hours:minutes<space>meridiem ): 13:32 AM
value entered for hour was not an integer in the range 1 to 12
Enter time (hours:minutes<space>meridiem ): 1:76 AM
value entered for minute was not an integer in the range 0 to 59
Enter time (hours:minutes<space>meridiem ): 1:34 scwdd
value entered for meridiem was not an am or pm
Enter time (hours:minutes<space>meridiem ): 4:56 AM
Time entered is valid
*/



