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 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
in C# i need to Write the program BookExceptionDemo for the Peterman Publishing Company. Create a BookException class that is instantiated when a Book’s price exceeds 10 cents per page and whose constructor requires three arguments for title, price, and number of pages. Create an error message that is passed to the Exception class constructor for the Message property when a Book does not meet the price-to-pages ratio. For example, an error message: For Goodnight Moon, ratio is invalid. ...Price is $12.99 for 25 pages. Next, create a Book class that contains fields for title, author, price, and number of pages. Include properties for each field. Throw a BookException if a client program tries to construct a Book object for which the price is more than 10 cents per page. Finally, using the Book class, create an array of five Books. Prompt the user for values for each Book. To handle any exceptions that are thrown because of improper or invalid data entered by the user, set the Book’s…
Write the code in java and understand what the question says and give me the code and don't copy or plagiarize please
main.py Load default template... 1 # Define your method here 2 3 if main ': name 4 # Type your code here.

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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
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
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr