Java Format: Unbound (saleable)
Java Format: Unbound (saleable)
8th Edition
ISBN: 9780134448398
Author: SAVITCH, Walter
Publisher: Prentice Hall
Expert Solution & Answer
Book Icon
Chapter 9, Problem 4E

Explanation of Solution

Exception classes:

The two classes that derives “InvalidFormattingException” are “InvalidHourException” and “InvalidMinuteException”.

InvalidHourException.java:

//Create a class that extends Exception

public class InvalidHourException extends InvalidFormattingException

{

    //Define a parameterized constructor

    public InvalidHourException(String reason)

    {

//Call the parent class's method by passing a message

        super(reason);

    }

}

Explanation:

The above class “InvalidHourException” extends “InvalidFormattingException” and this exception occurs when the hour entered by the user in invalid.

InvalidMinuteException.java:

//Create a class that extends Exception

public class InvalidMinuteException extends InvalidFormattingException

{

    //Define a parameterized constructor

    public InvalidMinuteException(String reason)

    {

//Call the parent class's method by passing a message

        super(reason);

    }

}

Explanation:

The above class “InvalidMinuteException” extends “InvalidFormattingException” and this exception occurs when the minute entered by the user in invalid.

Complete program:

Main.java:

//Import required packages

import java.util.*;

import java.util.Scanner;

//Define the main class

class Main

{

    //Declare required variables

    private int hour, minute;

    private boolean isAM;

    //Constructor

    public TimeOfDay()

    {

        //Instantiate the values

        hour = 0;

        minute = 0;

        isAM = false;

    }

    //Function definition to set the time

public void setTimeTo(String aTime) throws InvalidFormattingException, InvalidHourException, InvalidMinuteException

    {

        //Declare required variables

        int hourFound;

        int minuteFound;

        String indicatorFound;

        //Create an object for the scanner class

        Scanner reader = new Scanner(aTime);

        //Split using the delimiter

        reader.useDelimiter(":");

        //Try block

        try

        {

            //Assign the hour

            hourFound = reader.nextInt();

        }

        //Catch the exception

        catch (Exception e)

        {

            //Throw the exception with a message

throw new InvalidFormattingException("Hour not an integer");

        }

        //Check the condition

        if(hourFound<1 || hourFound>12)

            //Throw the exception with a message

throw new InvalidHourException("Hour not in the range of 1 to 12");

        //Get the remaining string

        String restOfString = reader.next();

        reader = new Scanner(restOfString);

        //Remove the last two characters

        if(restOfString.length()<3)

            //Throw the exception

throw new InvalidFormattingException("Bad format");

        //Get the substring

String minuteString = restOfString.substring(0, restOfString.length()-2);

        //Get the substring

String amString = restOfString.substring(restOfString.length()-2);

        //Try block

        try

        {

            //Convert the minute to integer

minuteFound = Integer.parseInt(minuteString);

        }

        //Catch the exception

        catch (Exception e)

        {

            //Throw the exception

throw new InvalidFormattingException("Minute not an integer");

        }

        //Check the condition

        if(minuteFound<0 || minuteFound>59)

            //Throw the exception with a message

throw new InvalidMinuteException("Minute not in the range of 0 to 59");

        //Check condition

if(!amString.equals("am") && !amString.equals("pm"))

            //Throw the exception with a message

throw new InvalidFormattingException("Did not have am/pm");

        //Assign the value

        hour = hourFound;

        //Assign the value

        minute = minuteFound;

        //Check if the string is am

        if(amString...

Blurred answer
Students have asked these similar questions
Derive exception classes from the class you wrote in the previous exercise. Each new class should indicate a specific kind of error. For example, InvalidHourException could be used to indicate that the value entered for an hour was not an integer in the range of 1 to 12. (Also the previous answer was not preferd from the teacher cause its not simple and use def which we arent taken it)
Write a class TimeOfDay that uses the exception classes defined in the previous exercise. Give it a method setTimeTo(timeString) that changes the time if timeString corresponds to a valid time of day. If not, it should throw an exception of the appropriate type. [Java]
What is the output of the following code? a. Error: ArithmeticException b. No output c. Error: ArrayIndexOutOfBoundsException d. Warning: Some Other exception e. Error:ArithmeticException and Warning:Some Other exception

Chapter 9 Solutions

Java Format: Unbound (saleable)

Knowledge Booster
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning