Concept explainers
Explanation of Solution
Changing statements inside “if” block:
The output of the
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...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
- Subject: Object Oriented PrgrammingLanguage: Java ProgramTopic: Exception 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. EXAMPLE: Input:The quick brown fox jumped over the lazy dogs. Output:The quick brown fox jumped over the laze dogs. ANOTHER EXAMPLE: Input: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…arrow_forwardIn C++ You have a class called Fraction, which uses the heap. The constructor for the class is declared as: Fraction::Fraction(); Implement an exception handling design in the parameterized constructor to catch zeros in the denominator. Show the new exception class you will need to put in the header (specification) file. Show the definition for your parameterized constructor that throws the exception if the denominator is zero. Finally, show how to use the try-catch statement in the driver program when instatiating a new paramerized Fraction object. You do not need to show entire programs, only the requested lines of source codearrow_forwardQuestion 3 } public class WeekDay { static void daysFromSunday(int day) { String dayName = ""; try { } } if (day > 7) { throw new Exception("Invalid Day! Too high!"); } else if(day < 1){ throw new Exception("Invalid Day! Too low!"); } switch(day){ case 1: dayName="Sun"; break; case 2: dayName="Mon"; break; case 3: dayName="Tue"; break; case 4: dayName="Wed"; break; } ▬▬ } case 5: dayName="Thu"; Dreak; case 5: dayName="Thu"; break; case 6: dayName="Fri"; break; default: dayName="Sat"; System.out.println(dayName); } catch (Exception excpt) { System.out.println("Oops"); System.out.println(excpt.getMessage()); }finally{ System.out.println("Done!"); public static void main(String[] args) { daysFromSunday(10); daysFromSunday(3); daysFromSunday(0); OOops Invalid Day! Too high! Tue Oops Invalid Day! Too low! OOops Invalid Day! Too high! Done! Tue Done! Oops Invalid Day! Too low! Done! O Oops Invalid Day! Too high! Done! Oops Invalid Day! Too high! Done! Oops Invalid Day! Too low! Done! O Oops…arrow_forward
- a) Write a method in java that searches a numeric array for a specified value. The method should return the subscript of the element containing the value if it is found in the array. If the value is not found , the method should throw an exceptionof the Exception class with the error message “Element not found”. b) Write an exception class that can be thrown when a negative number is passed to a method.arrow_forwardQ4: Write a program that performs exception handling. A class should have two exception handling classes. This program throws an exception of type both classes (user defined data types) and they are catched in a catch block. This class also throws an exception of built-in data types (of your choice). Also in your program include a catch block for all data types. Write a simple C++ code as i am a begineer.arrow_forwardPlease help, write the code for the test cases: public class JunitTest_RideRequestTest { // Test parameterized constructor with invalid input @Test public void test_2_0() { try { RideRequest request = new RideRequest(new String()); fail("You should throw an exception If the input is null or empty."); } catch (IllegalArgumentException e) { // exception excepted do nothing } } @Test public void test_2_2() { RideRequest request = null; try { RideRequest request2 = new RideRequest(request); fail("You should throw an exception If the input is null."); } catch (IllegalArgumentException e) { // exception excepted do nothing } } @Test public void test_2_3() { String s = "John , Downtown , 50.0 , Y , extra info"; try { RideRequest request2 = new RideRequest(s);…arrow_forward
- Needed codearrow_forwardPYTHON: A pedometer treats walking 2,000 steps as walking 1 mile. Write a steps_to_miles() function that takes the number of steps as a parameter and returns the miles walked. The steps_to_miles() function throws a ValueError object with the message "Exception: Negative step count entered." when the number of steps is negative. Complete the main() program that reads the number of steps from a user, calls the steps_to_miles() function, and outputs the returned value from the steps_to_miles() function. Use a try-except block to catch any ValueError object thrown by the steps_to_miles() function and output the exception message. Output each floating-point value with two digits after the decimal point, which can be achieved as follows:print('{:.2f}'.format(your_value)) Ex: If the input of the program is: 5345 the output of the program is: 2.67 Ex: If the input of the program is: -3850 the output of the program is: Exception: Negative step count entered.arrow_forwardX1155: Throwing an Exception The method below takes in a String parameter representing a username. It should throw an Exception if the username is less than 3 characters long. The method correctly throws the exception, but something is still missing. Fix the problem. Your Answer: Feedback 1 public String validateUser (String userName) 2|{ 3 45 6 7 8|} 9 if (userName.length() < 3) { throw new Exception("userName is too short"); } return userName; Check my answer! Reset Your feedback will appear here when you check your answer.arrow_forward
- Defined Exception Handling Exception Handling is used to overcome exceptions that take place during programs’ execution in a systematic manner. For instance, dividing a number by ‘0’ is a mathematical error (not defined). As you learned in class, you can use exception handling to overcome such operations. If you write a code without using exception handling, then the output of division by zero will be shown as infinity which cannot be further processed. Consider the following code: float Division(float num, float den) {return (num / den);} int main() { float numerator = 12.5; float denominator = 0; float result; result = Division(numerator, denominator); cout << "The quotient of 12.5/0 is " << result << endl; }arrow_forwardThis is a java program question dont copy from any other source or internet for gods sake write the program and give me the output also write the code in your own imagination dont copy it from other source pleasearrow_forwardin c++ Define a new exception class named “BadNameException” that must inherit from the C++ runtime_error class. It simply manages the specific reason of the error (string). It must be able to describe the reason for the error. Please note that if runtime_error class is not available, you may inherit from the exception class.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning