In: Computer Science
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter the time as a 4-digit number with no colon.
Define an exception class called InvalidTimeFormatException. If the user enters an invalid time lime 1065 or 2515, the program should throw and handle an InvalidTimeFormatException. NOTE: Assume the user will enter the time as a 4-digit number with no colon.
SAMPLE OUTPUT:
Enter time in 24-hour notation:
1614
That is the same as 4:14pm
Again? (y/n)
y
Enterprise in a 24-hour notation:
0245
That is the same as 2:45 AM
Again?(y/n)
Code:
//importing this package because we are using scanner
class
import java.util.*;
//exception class
class InvalidTimeFormatException extends Exception{
//construcotr
InvalidTimeFormatException(String s){
super(s);
}
}
//class named time, the file should be named as Time.java
class Time {
//main method
public static void main(String[] args){
//creating an instance of scanner
class
Scanner s = new
Scanner(System.in);
//an infinite loop which only
breaks when user enters n
while(true){
//taking
input
System.out.println("Enter time in
24- hour notation: ");
int t=s.nextInt();
//try block
try{
//string str to store output
String str="";
//getting houts and mins from
input
int hr=t/100;
int min=t%100;
//if user enters invalid time
throwing exception
if(hr >24 || min>60){
throw new
InvalidTimeFormatException("Exception occured");
}
//if hr is greater than 12
//subtracting 12 from hr and adding
Pm at the end
if(hr>12 &&
hr<=24){
hr=hr-12;
str=hr+":"+min+"
PM";
}
//else adding AM at the end
else{
str=hr+":"+min+"
AM";
}
//printing output
System.out.println("The time is:
"+str);
//catching exception
}catch(InvalidTimeFormatException i){
System.out.println(i);
}
//taking choice
System.out.println("Again?(y/n):");
char ch= s.next().charAt(0);
//if the choice is n , breaking the loop
if(ch=='N' || ch =='n'){
break;
}
}
}
}
Output:
Code Screenshot:
Code Snippet:
//importing this package because we are using scanner class
import java.util.*;
//exception class
class InvalidTimeFormatException extends Exception{
//construcotr
InvalidTimeFormatException(String s){
super(s);
}
}
//class named time, the file should be named as Time.java
class Time {
//main method
public static void main(String[] args){
//creating an instance of scanner class
Scanner s = new Scanner(System.in);
//an infinite loop which only breaks when user enters n
while(true){
//taking input
System.out.println("Enter time in 24- hour notation: ");
int t=s.nextInt();
//try block
try{
//string str to store output
String str="";
//getting houts and mins from input
int hr=t/100;
int min=t%100;
//if user enters invalid time throwing exception
if(hr >24 || min>60){
throw new InvalidTimeFormatException("Exception occured");
}
//if hr is greater than 12
//subtracting 12 from hr and adding Pm at the end
if(hr>12 && hr<=24){
hr=hr-12;
str=hr+":"+min+" PM";
}
//else adding AM at the end
else{
str=hr+":"+min+" AM";
}
//printing output
System.out.println("The time is: "+str);
//catching exception
}catch(InvalidTimeFormatException i){
System.out.println(i);
}
//taking choice
System.out.println("Again?(y/n):");
char ch= s.next().charAt(0);
//if the choice is n , breaking the loop
if(ch=='N' || ch =='n'){
break;
}
}
}
}
Note:
Save the file name as Time.java