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
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 9.1, Problem 5STQ

Explanation of Solution

Changing statements inside “if” block:

The output of the program will not get affected if the statements inside the “if” block is replaced by the given statements, because, the modified part is same like the original program.

Changed program of Listing 9.2:

The modified statement is highlighted below.

//Import required package

import java.util.Scanner;

//Define a class

public class Main

{

    //Define the main method

    public static void main(String[] args)

    {

        Scanner keyboard = new Scanner(System.in);

        //Try block

        try

        {

            //Get the number of donuts

System.out.println("Enter number of donuts:");

            int donutCount = keyboard.nextInt();

            //Get the number of glasses of milk

System.out.println("Enter number of glasses of milk:");

            int milkCount = keyboard.nextInt();

            //Check if milkCount is less than 1

            if(milkCount < 1)

            {

                //Create an exception

Exception e = new Exception("Exception: No milk!");

                //Throw the exception

                throw e;

            }

            //Calculate donuts per glass

double donutsPerGlass = donutCount / (double)milkCount;

            //Print the donuts count

System...

Blurred answer
Students have asked these similar questions
use C++ programing language  Write a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)
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…
write a code in java

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (7th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning