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!!!!”
Write a program that calculates an adult's fat-burning heart rate, which is 70% of 220 minus the person's age. Complete fat_burning_heart_rate() to calculate the fat burning heart rate. The adult's age must be between the ages of 18 and 75 inclusive. If the age entered is not in this range, raise a ValueError exception in get_age() with the message "Invalid age." Handle the exception in __main__ and print the ValueError message along with "Could not calculate heart rate info." Ex: If the input is: 35 the output is: Fat burning heart rate for a 35 year-old: 129.5 bpm If the input is: 17 the output is: Invalid age. Could not calculate heart rate info.
Write a program that calculates an adult's fat-burning heart rate, which is 70% of 220 minus the person's age. Complete fat_burning_heart_rate() to calculate the fat burning heart rate. The adult's age must be between the ages of 18 and 75 inclusive. If the age entered is not in this range, raise a ValueError exception in get_age() with the message "Invalid age." Handle the exception in __main__ and print the ValueError message along with "Could not calculate heart rate info." Ex: If the input is: 35 the output is: Fat burning heart rate for a 35 year-old: 129.5 bpm If the input is: 17 the output is: Invalid age. Could not calculate heart rate info. **This is the output i am having trouble with. on line 20 The print string shows the print(ve, 'could not calculate heart rate info.') And i know that the extra space i see at the begining of that second line of output is due to the comma but I cannot figure out how to make the same line print but with out the space from the comma after…

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