Java Format: Unbound (saleable)
Java Format: Unbound (saleable)
8th Edition
ISBN: 9780134448398
Author: SAVITCH, Walter
Publisher: Prentice Hall
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
PLZ help with the follwoing IN JAVA Suppose that statement2 causes an exception in the following try-catch block: Will statement3 be executed? If the exception is not caught, will statement4 be executed? If the exception is caught in the catch block, will statement4 be executed?
How can the standard error message be shown when an exception is thrown?
What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.

Chapter 9 Solutions

Java Format: Unbound (saleable)

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
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
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,