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

Write a java program that prompts the user to enter the length in feet and inche

ID: 3656413 • Letter: W

Question

Write a java program that prompts the user to enter the length in feet and inches and outputs the time in 24-hour notation. Your program must contain three exception classes: InvalidHrExcep, InvalidMinExcep, and InvalidSecExcep. If the user enters an invalid value for hours, then the program should throw and catch an InvalidHr object. Similar conventions for the values of minutes and seconds.

Explanation / Answer

package com.cramster.nov16; import java.util.Scanner; public class Time { public static void main(String[] args) { while(true) { try { Scanner console = new Scanner(System.in); System.out.print("Enter time in 12 hour notation as follows " + "HH:MM:SS AM/PM : "); String time = console.nextLine(); String[] tokens = time.split(":"); if(tokens.length != 3) throw new Exception("Input is not in correct format"); String[] tokens1 = tokens[2].split(" "); if(tokens1.length != 2) throw new Exception("Input is not in correct format"); int hours = Integer.parseInt(tokens[0]); int minutes = Integer.parseInt(tokens[1]); int seconds = Integer.parseInt(tokens1[0]); if(hours > 12 || hours < 0) throw new InvalidHrException("Hours are not between 0 and 12"); if(minutes > 59 || minutes < 0) throw new InvalidMinException("Minutes are not between 0 and 60"); if(seconds > 59 || seconds < 0) throw new InvalidSecException("Seconds are not between 0 and 60"); if(tokens1[1].equalsIgnoreCase("AM")) System.out.printf("Tme in 24 hour Notation is %2d:%2d:%2d ",hours == 12 ? 0 : hours,minutes,seconds); else if(tokens1[1].equalsIgnoreCase("PM")) System.out.printf("Tme in 24 hour Notation is %2d:%2d:%2d ",hours == 12 ? 12 : hours + 12,minutes,seconds); else throw new Exception("Input is not in correct format. It should be AM or PM"); break; } //Since InvalidHr, InvalidMin, InvalidSec extend class Exception they all are caught by superclass Exception variable(e) catch(Exception e) { System.out.println(e.getMessage()); System.out.println("Try Again"); } } } } class InvalidHrException extends Exception { public InvalidHrException(String message) { super(message); } } class InvalidMinException extends Exception { public InvalidMinException(String message) { super(message); } } class InvalidSecException extends Exception { public InvalidSecException(String message) { super(message); } }
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