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 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 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 Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
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.
Write a JAVA program called Convertor that has two methods. The first one converts an input...
Write a JAVA program called Convertor that has two methods. The first one converts an input binary into its equivalent decimal number. The second method converts a decimal number into its equivalent binary.​ binary2Decimal, decimal2Binary are the names for those methods.​ binary2Decimal receives a binary number as a string and returns a decimal number as an integer. And vice versa for the decimal2Binary method.​
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures 0–20, and their Fahrenheit equivalents. Display table on screen and it a .txt file. c++
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT