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, Problem 3P

Explanation of Solution

Given program:

The given code segment is highlighted.

//Import required package

import java.util.*;

//Define the class

public class Main

{

    //Define the main method

    public static void main(String[] args)

    {

        //Declare a variable

        int num = 0;

        //Create an object for Scanner class

        Scanner kbd = new Scanner(System...

Explanation of Solution

Wrapping the code inside try-catch block:

The modified code segment is highlighted.

//Import required package

import java.util.*;

//Define the class

public class Main

{

    //Function

    static void fun()

    {

        //Declare a variable

        int num = 0;

         //Create an object for Scanner class

        Scanner kbd = new Scanner(System.in);

        //Do until true

        while(true)

        {

            //Try block

            try

            {

                  //Get the number from the user

System.out.println ("Please enter a number: ");

                num = kbd.nextInt();

                //Print the number

System.out...

Blurred answer
Students have asked these similar questions
The following snippet of code inputs an integer and outputs it: int num=0; Scanner kbd = new Scanner(System.in); num = kbd.nextInt(); System.out.println(num); Run this program but enter text instead of an integer. The program should crash and tell you what kind of exception was thrown by the nextInt method. Wrap this code inside a try/catch block where you catch the exception that is thrown. Add a loop so the user must enter the number again if text is entered
Q1) The following snippet of code inputs an integer and outputs it:int num=0;Scanner kbd = new Scanner(System.in);num = kbd.nextInt();System.out.println(num);Run this program but enter text instead of an integer. The programshould crash and tell you what kind of exception was thrown by thenextInt method. Wrap this code inside a try/catch block where youcatch the exception that is thrown. Add a loop so the user must enterthe number again if text is entered.Q2) The following class maintains an account balance and returns a special error code.public class Account{private double balance;// returns new balance or -1 if errorpublic double deposit(double amount){if (amount > 0)balance += amount;elsereturn -1; // Code indicating errorreturn balance;}}Rewrite the class so that it throws appropriate exception instead ofreturning -1 as an error code. Write test code that attempts to deposit invalid amounts and catches the exceptions that are thrown
Write a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again. Format your output with setprecision(2) to ensure the proper number of decimals for testing!

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
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
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,