Question

In: Computer Science

In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume...

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)

Solutions

Expert Solution

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


Related Solutions

*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example,...
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers. This is what I have so far. The program runs for correct input values, but I cannot figure out how to accommodate for hours > 23 and minutes > 59, or negative values. My code is below: // Writing a program that converts from 24-hour notation to 12-hour notation.// #include...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation; a function to convert the time from 12-hour notation to 24-hour notation; a function to display the choices; function(s) to get the input;...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation; a function to convert the time from 12-hour notation to 24-hour notation; a function to display the choices; function(s) to get the input;...
Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and...
Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and 24-hour time (military time). We will assume in the context there is no need for seconds, so time12 use only hours (from 1 to 12), minutes, and an "a.m." or "p.m." designation. Our time24 class, which uses hours (from 00 to 23), minutes and seconds. 12-Hours Time 24-Hours Time 12:00 a.m. (midnight) 00:00 12:01 a.m. 00:01 12:00 p.m. 12:00 6:00 p.m. 18:00 11:59 p.m....
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
Write a program that converts a given floating point binary number with a 24-bit normalized mantissa...
Write a program that converts a given floating point binary number with a 24-bit normalized mantissa and an 8-bit exponent to its decimal (i.e. base 10) equivalent. For the mantissa, use the representation that has a hidden bit, and for the exponent use a bias of 127 instead of a sign bit. Of course, you need to take care of negative numbers in the mantissa also. Use your program to answer the following questions: (a) Mantissa: 11110010 11000101 01101010, exponent:...
using C program Assignment Write a computer program that converts a time provided in hours, minutes,...
using C program Assignment Write a computer program that converts a time provided in hours, minutes, and seconds to seconds Functional requirements Input MUST be specified in hours, minutes, and seconds MUST produce the same output as listed below in the sample run MUST correctly compute times Nonfunctional requirements MUST adhere to program template include below MUST compile without warnings and errors MUST follow the code template provided in this assignment MUST NOT change " int main() " function MUST...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT