Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
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
Subject: Object Oriented PrgrammingLanguage: Java ProgramTopic: Exception (SEE ATTACHED PHOTO FOR THE PROBLEM)
JAVA PROGRAM Probloem: Define a new exception, called ExceptionLineTooLong, that prints out the error message "The strings is too long". Write a program that reads phrase and throws an exception of type ExceptionLineTooLong in the case where a string is longer than 80 characters. First Example:Input:The quick brown fox jumped over the lazy dogs.Output:The quick brown fox jumped over the lazy dogs. Second Example:Input: The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown…
Java Exception method withdraw throws an exception if amount is greater than balance. For example: Test Result Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(5500.0); System.out.println("Balance: "+account.getBalance()); Insufficient: Insufficient funds. Balance: 5000.0 Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(500.0); System.out.println("Balance: "+account.getBalance()); Balance: 4500.0

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage