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
please do this in java! 3. RetailItem Exceptions Programming Challenge 4 of Chapter 6 required you to write a RetailItem class that holds data pertaining to a retail item. Write an exception class that can be instantiated and thrown when a negative number is given for the price. Write another exception class that can be instantiated and thrown when a negative number is given for the units on hand. Demonstrate the exception classes in a program. /** *Description: This program will displays a string without any user interaction *Class: Fall - COSC 1437.81002 *Assignment1: Hello World *Date: 08/15/2011 *@author  Zoltan Szabo *@version 0.0.0 */    For each method, you will also be required to create docstring as follows: /** * @param String as args * @return Termination code as int, 0 for normal, anything else is error condition * @throws Nothing is implemented */     Flowcharts/UML and Pseudo code   All assignment questions must show design flowchart/UML and/or pseudo code unless…
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
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.)

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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning