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
use C++ programing language  Write a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)
Create a flowchart and modify the code.   INSTRUCTION: Create a new class called CalculatorWithMod. This class should be a sub class of the base class Calculator. This class should also have an additional method for calculating the modulo. The modulo method should only be seen at the sub class, and not the base class! Include exception handling for instances when dividing by 0 or calculating the modulo with 0. You would need to use throw, try, and catch. The modulo (or "modulus" or "mod") is the remainder after dividing one number by another.Example: 20 mod 3 equals 2Because 20/3 = 6 with a remainder of 2   CODE TO COPY: #include <iostream> using namespace std; class Calculator{public:Calculator(){printf("Welcome to my Calculator\n"); } int addition(int a, int b);int subtraction(int a, int b);int multiplication(int a, int b);float division(int a, int b);}; int Calculator::addition(int a, int b){return (a+b);} int Calculator::subtraction(int a, int b){return (a-b);} int…
Subject: Object Oriented PrgrammingLanguage: Java ProgramTopic: Exception 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.   EXAMPLE: Input:The quick brown fox jumped over the lazy dogs. Output:The quick brown fox jumped over the laze dogs.     ANOTHER 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…

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
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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,