Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 9, Problem 2E
Program Plan Intro
Program Plan:
- • Import required package.
- • Declare the “Main” class.
- ○ Define the “main ()” method.
- ■ Create an object “sc” using “Scanner” class.
- ■ Declare a boolean variable “flag”.
- ■ Do the below steps until the user enters other than “y” using “while” condition.
- • Inside the “try” block,
- ○ Get two integers from the user.
- ○ Print the remainder on the screen.
- • Catch if the operation causes any arithmetic exception.
- ○ Print the message.
- • Catch if the user enters other than integers.
- ○ Print the message.
- • Ask whether the user wants to continue or not and store the response in a variable “response”.
- • Check if “response” is equal to “y”.
- ○ Assign “true” to the Boolean variable.
- • Inside the “try” block,
- ○ Define the “main ()” method.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError as a variable and output "Zero Division Exception: " followed by the exception message from the variable. Use another except block to catch any ValueError caused by invalid input as a variable and output "Input Exception: " followed by the exception message from the variable. Note: ZeroDivisionError is raised when a division by zero happens. ValueError is raised when a user enters a value of different data type than what is defined in the program. Do not include code to raise any exception in the program. (in Python)
Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError as a variable and output "Zero Division Exception: " followed by the exception message from the variable. Use another except block to catch any ValueError caused by invalid input as a variable and output "Input Exception: " followed by the exception message from the variable.
Note: ZeroDivisionError is raised when a division by zero happens. ValueError is raised when a user enters a value of different data type than what is defined in the program. Do not include code to raise any exception in the program.
Ex: If the input of the program is:
15
3
the output of the program is:
5
Ex: If the input of the program is:
10
0
the output of the program is:
Zero Division Exception: integer division or modulo by zero
Ex: If the input of the program is:
15.5
5
the output of…
Take two integers from the user, Write a program
in Java to divide a number by another.
Point to be NOTED:
User may give "string or o“ as divider intentionally
or by mistake. You must handle all types of
exception, But the program should not stop if
there is any kind of error or exception. Your
program should provide as many chances as user
need to complete the calculation. The program will
immediately exit after successfully dividing the
1st number by 2nd number.
Chapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Ch. 9.1 - Prob. 1STQCh. 9.1 - What output would the code in the previous...Ch. 9.1 - Prob. 3STQCh. 9.1 - Prob. 4STQCh. 9.1 - Prob. 5STQCh. 9.1 - Prob. 6STQCh. 9.1 - Prob. 7STQCh. 9.1 - Prob. 8STQCh. 9.1 - In the code given in Self-Test Question 1,...Ch. 9.1 - In the code given in Self-Test Question 1,...
Ch. 9.1 - Prob. 11STQCh. 9.1 - Prob. 12STQCh. 9.1 - Prob. 13STQCh. 9.1 - Prob. 14STQCh. 9.2 - Prob. 15STQCh. 9.2 - Prob. 16STQCh. 9.2 - Prob. 17STQCh. 9.2 - Prob. 18STQCh. 9.2 - Prob. 19STQCh. 9.2 - Prob. 20STQCh. 9.2 - Suppose that, in Self-Test Question 19, we change...Ch. 9.2 - Prob. 22STQCh. 9.2 - Prob. 23STQCh. 9.3 - Prob. 24STQCh. 9.3 - Prob. 25STQCh. 9.3 - Prob. 26STQCh. 9.3 - Prob. 27STQCh. 9.3 - Prob. 28STQCh. 9.3 - Repeat Self-Test Question 27, but change the value...Ch. 9.3 - Prob. 30STQCh. 9.3 - Prob. 31STQCh. 9.3 - Prob. 32STQCh. 9.3 - Consider the following program: a. What output...Ch. 9.3 - Write an accessor method called getPrecision that...Ch. 9.3 - Prob. 35STQCh. 9.4 - Rewrite the class ColorDemo in Listing 9.13 so...Ch. 9.4 - Prob. 37STQCh. 9 - Write a program that allows students to schedule...Ch. 9 - Prob. 2ECh. 9 - Prob. 3ECh. 9 - Prob. 4ECh. 9 - Prob. 5ECh. 9 - Write code that reads a string from the keyboard...Ch. 9 - Create a class Rational that represents a rational...Ch. 9 - Prob. 9ECh. 9 - Suppose that you are going to create an object...Ch. 9 - Revise the class RoomCounter described in the...Ch. 9 - Prob. 12ECh. 9 - Write a class LapTimer that can be used to time...Ch. 9 - Prob. 1PCh. 9 - Prob. 2PCh. 9 - Prob. 3PCh. 9 - Write a program that uses the class calculator in...Ch. 9 - Prob. 3PPCh. 9 - Prob. 7PPCh. 9 - Prob. 9PPCh. 9 - Suppose that you are in change of customer service...Ch. 9 - Write an application that implements a trip-time...
Knowledge Booster
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
- Question 22 Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError and output an exception message. Use another except block to catch any ValueError caused by invalid input and output an exception message. Please do it ONLY in Python Language also inlcude the input and output screenshotarrow_forwardplease fix the issue hihlighted in yellow and please check my code. In python language: Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError and output an exception message. Use another except block to catch any ValueError caused by invalid input and output an exception message. Note: ZeroDivisionError is thrown when a division by zero happens. ValueError is thrown when a user enters a value of different data type than what is defined in the program. Do not include code to throw any exception in the program. Ex: If the input of the program is: 15 3 the output of the program is: 5 Ex: If the input of the program is: 10 0 the output of the program is: Zero Division Exception: integer division or modulo by zero Ex: If the input of the program is: 15.5 5 the output of the program is: Input Exception: invalid literal for…arrow_forwardpython program please helparrow_forward
- I need to know how to do this in python: 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.arrow_forwardWrite an application that throws and catches an ArithmeticException when you attempt to take the square root of a negative value. Prompt the user for an input value and try the Math.sqrt() method on it. The application either displays the square root or catches the thrown Exception and displays an appropriate message NB:ALSO SHOW OUTPUTarrow_forwardThis code is in Python As you know, when you attempt to get a number from the user by usingsomething like:number = int(input(“Enter a number”) ornumber = float(input(“Enter a number”),if the user includes commas or a dollar sign in the number, the conversion will failwith an exception.Write a program that will include and use a function called getNumber( prompt )that will do the following:1) Use the standard input function to display the prompt and get the user’sinput.2) Use the string functions to remove any commas or dollar sign from theinput.3) Check for the presence of a decimal point.4) If a decimal point is present, use the float conversion function to convertthe modified input to float; otherwise, use the int conversion function toconvert to an int5) Return the number to the calling code.arrow_forward
- Part 2 Use exception handeling Write a program (CoconutCherry that This program should accept multiple numbers as command line arguments and print those numbers out again space separated but for multiples of 3 print “coconut” instead of the number, for multiples of 7 print “cherry” instead of the number, and for the multiples of both 3 and 7 print “coconut-cherry” instead of the number. For numbers smaller than 1, print “invalid”arrow_forwardWrite a function that returns the result of the following expression: (x² + y²) / z² It should throw an error message string if division by zero occurs. Write a main () that prompts the user for three integers, then passes those three integers (in order) to the function you wrote for assignment 1. Make sure you have an exception handler. Belt-and-suspenders time! Update main () so that it also checks the values input by the user to ensure that division by zero doesn't occur and the user is prompted repeatedly until they enter legal values.arrow_forwardWhen will be the else part of try-except-else be executed? a. None of these b. when no exception occurs c. always d. when an exception occursarrow_forward
- Q1. Again --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..arrow_forwardWrite a program that prompts the user to enter several integer numbers and store them in a one dimensional array. The size of the array should be read from the keyboard. The program should calculate and display the average of the numbers. If the user enters a value for the array size less than one, throw and handle an appropriate exception (you have to display the message array size cannot be less than one) and prompt the user to enter another array size. This process continue until the user enters the correct array size. In addition, the program should be able to handle exception thrown when there is a division by zero problem, when the index of the array is out of bound, when there is input mismatch. SLOVE IT USING JGRASP PLEASEarrow_forwardC++ You did this way back in Unit 1! But now all of your RomanNumber code is wrapped up in a class, so the "main" program is short and sweet. As before, write a program that accepts entries from the user. If it's an integer, convert to Roman Number and display. If it's a Roman Number, convert to integer and display. If it's neither, thrown an exception and continue to process The action to take here will be to display an error message If the entry is 0 or O (The digit 0 (zero) or the letter O) exit and state how many conversions were done of each type (integer to Roman and Roman to integer) and how many exceptions were thrown. As I said, the main program will be short and sweet. Provide your .cpp code and a screen shot of your program in action. Enter twelve conversions - four integer to Roman, four Roman to integer, and two of each in which the input is invalid so that I can see the exceptions. The thirteenth and final entry will be 0 or O.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT