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

So I am having to write a program were the user enters time in 12 hour notation.

ID: 3671122 • Letter: S

Question

So I am having to write a program were the user enters time in 12 hour notation. and the program out puts the time in 24 hour notation. the program must contain three exception classes invalidHr, invalidMin, and invalidSec. in the con exception clases there should be two constructors. the default constructor that supplies the standard error message and a parameterized constructor which takes a string variable to intialize the error message. if the user enters any invalid hours, min, or secs the program should throw and catch a error message accordingly

Explanation / Answer


/**
* @author Srinivas Palli
*
*/
public class invalidHr extends Exception {

   public invalidHr() {
       // TODO Auto-generated constructor stub
       super("Invalid Hour");
   }

   /**
   * @param message
   */
   public invalidHr(String message) {
       // TODO Auto-generated constructor stub
       super(message);
   }

}

/**
* @author Srinivas Palli
*
*/
public class invalidMin extends Exception {

  
   public invalidMin() {
       // TODO Auto-generated constructor stub
       super("Invalid Minites");
   }
  
  
   /**
   * @param message
   */
   public invalidMin(String message) {
       // TODO Auto-generated constructor stub
       super(message);
   }
}

/**
* @author Srinivas Palli
*
*/
public class invalidSec extends Exception {
   public invalidSec() {
       // TODO Auto-generated constructor stub
       super("Invalid Seconds");
   }

   /**
   * @param message
   */
   public invalidSec(String message) {
       // TODO Auto-generated constructor stub
       super(message);
   }

}

import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class DateTimeConversion {

   public static void main(String[] args) {
       Scanner scanner = null;

       try {
           scanner = new Scanner(System.in);
           int hour, minites, seconds;
           System.out.print("Enter hours(1-12):");
           hour = scanner.nextInt();
           if (hour <= 12 && hour >= 1) {

               hour += 12;
           } else {

               throw new invalidHr("Invalid Hours:" + hour);
           }

           System.out.print("Enter Minites(0-59):");
           minites = scanner.nextInt();
           if (minites <= 59 && minites >= 0) {

           } else {

               throw new invalidMin("Invalid Minites:" + minites);
           }

           System.out.print("Enter Seconds(0-59)");
           seconds = scanner.nextInt();
           if (seconds <= 59 && seconds >= 0) {

           } else {

               throw new invalidSec("Invalid Seconds:" + seconds);
           }

           System.out.println("24 Hour fomat is: " + hour + ":" + minites
                   + ":" + seconds);

       } catch (invalidHr e) {
           // TODO: handle exception
           e.printStackTrace();
       } catch (invalidMin e) {
           // TODO: handle exception
           e.printStackTrace();
       } catch (invalidSec e) {
           // TODO: handle exception
           e.printStackTrace();
       }
   }
}

OUTPUT:
Enter hours(1-12):15
invalidHr: Invalid Hours:15
   at DateTimeConversion.main(DateTimeConversion.java:22)

  
Enter hours(1-12):1
Enter Minites(0-59):63
invalidMin: Invalid Minites:63
   at DateTimeConversion.main(DateTimeConversion.java:31)

  
Enter hours(1-12):1
Enter Minites(0-59):54
Enter Seconds(0-59)63
invalidSec: Invalid Seconds:63
   at DateTimeConversion.main(DateTimeConversion.java:40)
  
  
Enter hours(1-12):1
Enter Minites(0-59):54
Enter Seconds(0-59)51
24 Hour fomat is: 13:54:51

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