Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me write this java program? Write a program that converts from

ID: 3596353 • Letter: C

Question

Can someone help me write this java program?

Write a program that converts from the 24-hour time format to the 12-hour time format. The following is a sample dialogue (user input is underlined):

     Enter time in 24-hour notation:

     13:07

     That is the same as

     1:07 PM

     Again? (y/n)

     Y

     Enter time in 24-hour notation:

     10:65

     That is not a valid time

     Try again:

     10:35

     That is the same as

     10:35 AM

     Again? (y/n)

     N

     End of program

Define an exception class called TimeFormatException so that if the user enters an illegal time, like 10:65, your program will throw, catch, and handle a TimeFormatException exception. Your program should also be able to handle an appropriate exception for gibberish entries, like &&*68, and so on.

You should create a project, named Project4 and the project should contain all the relevant classes for this application.

Your program should be well documented and in particular, begin with a series of comments specifying:

a brief description of what the program does.

Explanation / Answer

Please find my implementation.

import java.util.Scanner;

public class TimeFormat {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       while(true){

           try{

               System.out.println(" Enter time in 24-hour notation:");

               String time = sc.next();

               Scanner scanner = new Scanner(time);

               scanner.useDelimiter(":");

               int hour = Integer.parseInt(scanner.next());

               if(hour < 0 || hour > 23){

                   scanner.close();

                   throw new TimeFormatException(" That is not a valid time");

               }

               int minute = Integer.parseInt(scanner.next());

               if(minute < 0 || minute > 59) {

                   scanner.close();

                   throw new TimeFormatException(" That is not a valid time");

               }

               int hr = hour%12;

               String strHr = Integer.toString(hr);

               String strMin = Integer.toString(minute);

               if(strHr.length() == 1)

                   strHr = "0"+strHr;

               if(strMin.length() == 1)

                   strMin = "0"+strMin;

               System.out.println("That is same as");

               System.out.println(strHr+":"+strMin);

               System.out.println(" Again? (y/n)");

               String c = sc.next();

               scanner.close();

               if("n".equalsIgnoreCase(c))

                   break;

           }catch (TimeFormatException e) {

               System.out.println(e.getMessage());

               System.out.println("Try Again");

           }

       }

       sc.close();

   }

}

class TimeFormatException extends Exception {

   private String message;

   public TimeFormatException(String message) {

       this.message = message;

   }

   public String getMessage() {

       return message;

   }

}

/*

Sample run:

Enter time in 24-hour notation:

13:07

That is same as

01:07

Again? (y/n)

Y

Enter time in 24-hour notation:

10:65

That is not a valid time

Try Again

Enter time in 24-hour notation:

10:35

That is same as

10:35

Again? (y/n)

n

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote