Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
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 java Write a program that prompts the user to enter the bid value of a bicycle in an auction from two bidders. Read two integers and display the Greatest bid value your program should prompt the user to read the number again if the input format and type is incorrect. Use exception handling to perform the same.
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
python program please help

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (7th 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