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
java code quation using ExceptionProgramme Leader of ITMB wants to check whether the student number and GSM numberentered by the student for the programming contest is valid or not.Write a Java program to read the Student ID and GSM Number of a student. Use a methodcalled ValidityDetails () for checking the validity of details entered.If the Student ID ends with the characters ST and contains more than 6 letters or if the MobileNumber does not contain exactly 8 digits, throw a user defined exceptionInvalidDetailsException.If the details entered are valid, display the message ‘”correct details are entered!!!” otherwisedisplay “Entered invalid details!!!!”
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 HANDLING: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. For example: Input Result 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 fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped…

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