Java: Introduction to Problem Solving and Programming
Java: Introduction to Problem Solving and Programming
7th Edition
ISBN: 9780133834604
Author: SAVITCH
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 9, Problem 1P

Explanation of Solution

Creating “Main.java”:

  • Create a class named “Main”.
    • Define the “main ()” method.
      • Declare required variables.
      • Do till the user enters “n” using “while” condition.
        • Get the string from the user.
        • Check if the length of the string is greater than or equal to 20.
          • Create an object “e” for the class. Here the exception is thrown using default constructor.
          • Get and print the message using the method “e.getMessage()”.
        • Else,
          • Print the number of characters.
        • Ask whether the user wants to continue or not and store the response in a variable “response”.
        • Check if the response is equal to “n”.
          • Break the loop.

Creating “MessageTooLongException.java”:

  • Create a class named “MessageTooLongException” that extends “Exception”.
    • Define a default constructor that calls the parent class’s method using “super ()” by passing a message.
    • Define a parameterized constructor that calls the parent class’s method using “super ()” by passing a message that is given as the argument.

Program:

MessageTooLongException.java:

//Define a class

public class MessageTooLongException extends Exception

{

    //Default constructor

    public MessageTooLongException()

    {

        //Call the parent class by passing the message

        super("Message Too Long!");

    }

    //Parameterized constructor

    public MessageTooLongException(String message)

    {

        //Call the parent class by passing the message

        super(message);

    }

}

Main.java:

//import the package

import java.util.Scanner;

//Main class

class Main

{

    //Define main method

    public static void main(String[] args)

    {

        //Create an object for the scanner class

        Scanner sc = new Scanner(System.in);

        //Declare required variables

        String str, response = "y";

        //Do till the user enters 'n'

        while(response...

Blurred answer
Students have asked these similar questions
Write a program that converts a time from 24-hour notation to 12-hour notation. To make the solution easier, a requirement is imposed on the input: It must be in xx:xx format, i.e. it must have two digits, a colon, and then another two digits. Define an exception class called TimeException. If the user enters an illegal time, like 10:65, or even gibberish, like 8&*68, your program should throw and handle a TimeException. Test your program with the file "times.txt" as input and store the result in the file "result.txt"   File times.txt: 00:00 12:00 12:01 11:59 23:59 24:00 10:65 3:23 1145 8&*68   File result.txt: #         24-hour           12-hour -------------------------------------------- 1          00:00               12:00 AM 2          12:00               12:00 PM 3          12:01                12:01 PM 4          11:59                11:59 AM 5          23:59               11:59 PM 6          24:00               Time Exception 7          10:65…
Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError as a variable and output "Zero Division Exception: " followed by the exception message from the variable. Use another except block to catch any ValueError caused by invalid input as a variable and output "Input Exception: " followed by the exception message from the variable. Note: ZeroDivisionError is raised when a division by zero happens. ValueError is raised when a user enters a value of different data type than what is defined in the program. Do not include code to raise any exception in the program. (in Python)
Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError as a variable and output "Zero Division Exception: " followed by the exception message from the variable. Use another except block to catch any ValueError caused by invalid input as a variable and output "Input Exception: " followed by the exception message from the variable. Note: ZeroDivisionError is raised when a division by zero happens. ValueError is raised when a user enters a value of different data type than what is defined in the program. Do not include code to raise any exception in the program. Ex: If the input of the program is: 15 3 the output of the program is: 5 Ex: If the input of the program is: 10 0 the output of the program is: Zero Division Exception: integer division or modulo by zero Ex: If the input of the program is: 15.5 5 the output of…

Chapter 9 Solutions

Java: Introduction to Problem Solving and Programming

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