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

You are to create new classes (similar to ExceptionLength below) as follows: and

ID: 3546930 • Letter: Y

Question

You are to create new classes (similar to ExceptionLength below) as follows:


and create instances of the classes above similar to ExceptionLength.java when the appropriate exceptions are thrown in DemoTimeFormatExceptions.java


import java.util.Scanner;

public class DemoTimeFormatExceptions
{
public static void main(String[] args) throws Exception
{
Scanner keyboard = new Scanner(System.in);
String timeEntered;
boolean validLength = true, validHour = true, validMinute = true, validAMPM = true, validColon = true, validSpace = true;
//System.out.println("Enter a time in the format hh:mm am or hh:mm pm");
//timeEntered = keyboard.nextLine();


do
{
    System.out.println("Enter a time in the format hh:mm am or hh:mm pm");
    timeEntered = keyboard.nextLine();
    try
    {
    if(timeEntered.length() == 8)
        validLength = true;
        else
        {
        validLength = false;    
        throw new ExceptionLength("Invalid length, must in in format hh:mm am or hh:mm pm");
        //continue;
        
        }
    }//end try

    catch(    ExceptionLength e)
    {
        System.out.println(e.getMessage());
        
        continue;
        //e.myMethod();
    }//end catch ExceptionLength

    System.out.println("HERE");
}
while(!validLength || !validHour || !validMinute ||!validAMPM || !validColon || !validSpace);

}//end main
}//end class



import java.util.Scanner;

public class ExceptionLength extends Exception
{

//public boolean validLength;

public ExceptionLength()
{
super("Length Exception of unknown specificity.");
//validLength = false;
//getAnotherDate();
}

public ExceptionLength(String messagePassed)
{
super(messagePassed);
//validLength = false;
}

public String getAnotherDate()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a time in the format hh:mm am or hh:mm pm");
return keyboard.nextLine();    
}

}//end class


Explanation / Answer


DemoTimeFormatExceptions.java

import java.util.Scanner;

public class DemoTimeFormatExceptions {
    public static void main(String[] args) throws Exception {
        Scanner keyboard = new Scanner(System.in);
        String timeEntered;
        boolean validLength = true, validHour = true, validMinute = true, validAMPM = true, validColon = true, validSpace = true;
        // System.out.println("Enter a time in the format hh:mm am or hh:mm pm");
        // timeEntered = keyboard.nextLine();

        do {
            System.out
                    .println("Enter a time in the format hh:mm am or hh:mm pm");
            timeEntered = keyboard.nextLine();
            try {
                if (timeEntered.length() == 8)
                    validLength = true;
                else {
                    validLength = false;
                    throw new ExceptionLength(
                            "Invalid length, must in in format hh:mm am or hh:mm pm");
                    // continue;

                }
            }// end try

            catch (ExceptionLength e) {
                System.out.println(e.getMessage());

                continue;
                // e.myMethod();
            }// end catch ExceptionLength
           
           
            //Hour check
            try{
                int hours = Integer.parseInt(timeEntered.substring(0, 2));
                if(hours < 0 || hours > 12)
                    throw new ExceptionHour(
                            "Invalid hours, must in in format hh:mm am or hh:mm pm and hours in the range 0-12 ");
            }
            catch (ExceptionHour e) {
                System.out.println(e.getMessage());

                continue;
           
            }// end catch ExceptionHour
           
            //Minute check
            try{
                int minutes = Integer.parseInt(timeEntered.substring(2, 4));
                if(minutes < 0 || minutes > 59)
                    throw new ExceptionMinute(
                            "Invalid Minutes, must in in format hh:mm am or hh:mm pm and minutes in the range 0-59 ");
            }
            catch (ExceptionMinute e) {
                System.out.println(e.getMessage());

                continue;
           
            }// end catch ExceptionMinute
           
            //colon check
            try{
                char colon = timeEntered.charAt(2);
                if(colon != ':')
                    throw new ExceptionColon(
                            "Invalid Colon, must be in format hh:mm am or hh:mm pm with colon(:) as seperator between hh and mm");
            }
            catch (ExceptionColon e) {
                System.out.println(e.getMessage());

                continue;
           
            }// end catch ExceptionColon
           
            //Space check
            try{
                String space = timeEntered.substring(5,6);
                if(!space.equals(" "))
                    throw new ExceptionSpace(
                            "Invalid time, must in in format hh:mm am or hh:mm pm, one space must be given before am or pm");
            }
            catch (ExceptionSpace e) {
                System.out.println(e.getMessage());

                continue;
           
            }// end catch ExceptionSpace

           
            //AMPM check
            try{
                String AMPM = timeEntered.substring(6, 8);
                if(!AMPM.equalsIgnoreCase("am") && !AMPM.equalsIgnoreCase("pm"))
                    throw new ExceptionAMPM(
                            "Invalid AM or PM, must in in format hh:mm am or hh:mm pm");
            }
            catch (ExceptionAMPM e) {
                System.out.println(e.getMessage());

                continue;
           
            }// end catch ExceptionAMPM
           
           
            System.out.println("HERE");
        } while (!validLength || !validHour || !validMinute || !validAMPM
                || !validColon || !validSpace);

    }// end main
}// end class


ExceptionHour.java


import java.util.Scanner;

public class ExceptionHour extends Exception {

    // public boolean validLength;

    public ExceptionHour() {
        super("Length Exception of unknown specificity.");
        // validLength = false;
        // getAnotherDate();
    }

    public ExceptionHour(String messagePassed) {
        super(messagePassed);
        // validLength = false;
    }

    public String getAnotherDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a time in the format hh:mm am or hh:mm pm");
        return keyboard.nextLine();
    }

}// end class


ExceptionMinute.java


import java.util.Scanner;

public class ExceptionMinute extends Exception {

    // public boolean validLength;

    public ExceptionMinute() {
        super("Length Exception of unknown specificity.");
        // validLength = false;
        // getAnotherDate();
    }

    public ExceptionMinute(String messagePassed) {
        super(messagePassed);
        // validLength = false;
    }

    public String getAnotherDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a time in the format hh:mm am or hh:mm pm");
        return keyboard.nextLine();
    }

}// end class


ExceptionColon.java


import java.util.Scanner;

public class ExceptionColon extends Exception {

    // public boolean validLength;

    public ExceptionColon() {
        super("Length Exception of unknown specificity.");
        // validLength = false;
        // getAnotherDate();
    }

    public ExceptionColon(String messagePassed) {
        super(messagePassed);
        // validLength = false;
    }

    public String getAnotherDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a time in the format hh:mm am or hh:mm pm");
        return keyboard.nextLine();
    }

}// end class


ExceptionAMPM.java
import java.util.Scanner;

public class ExceptionAMPM extends Exception {

    // public boolean validLength;

    public ExceptionAMPM() {
        super("Length Exception of unknown specificity.");
        // validLength = false;
        // getAnotherDate();
    }

    public ExceptionAMPM(String messagePassed) {
        super(messagePassed);
        // validLength = false;
    }

    public String getAnotherDate() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a time in the format hh:mm am or hh:mm pm");
        return keyboard.nextLine();
    }

}// end class


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