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 (8th 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_forwardJAVA PROGRAM Probloem: 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. First Example:Input:The quick brown fox jumped over the lazy dogs.Output:The quick brown fox jumped over the lazy dogs. Second 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 brown fox jumped over the lazy dogs.The quick brown…arrow_forward
- Question 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_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_forwardNeeded codearrow_forward
- Using Java Define a Rubric class that extends the GradedActivity class shown below. The Rubric class should determine the grade a student receives for a chapter lab. Using the following points: Algorithm: 20 points Variables: 5 points Formulas: 5 points Test Data: 15 points UML: 20 points Code: 35 points Create exception classes for 2 error conditions. You determine which errors for the exceptions. Demonstrate the class in a simple program. GradedActivity public class GradedActivity { private double score; // holds the score public void setScore (double s) { score=s; } //end method for setting the score public double getScore() { return score; }// end method to return the score public char getGrade() { // return a letter grade determined by the scores listed below char letterGrade; if (score >= 90) letterGrade = 'A'; else if (score >=80) letterGrade = 'B'; else if (score >=70)…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_forwardSubject: Object Oriented PrgrammingLanguage: Java ProgramTopic: Exception (SEE ATTACHED PHOTO FOR THE PROBLEM)arrow_forward
- Modify the MilTime class given under Final exam module. The class should implement the following exceptions: BadHour Throw when an invalid hour (< 0 or > 2359) is passed to the class. BadSeconds Throw when an invalid number of seconds (<0 or > 59) is passed to the class. Demonstrate the class in a driver program. Demo in the main function. the file: // MillTIme.cpp//#include "stdafx.h"#include <iostream>using namespace std;class Time{protected: int hour; int min; int sec;public: Time(int h, int m, int s) { hour = h; min = m; sec = s; } int getHour() { return hour; } int getMin() { return min; } int getSec() { return sec; }}; class MilTime : public Time{private: int milHours; int milSeconds;public: MilTime(int h = 0, int s = 0) : Time(0, 0, s) { if (h < 0 || h > 2359) { cout << "Hours must be in the range 0 - 2359.\n"; milHours = h;…arrow_forwardPLZ help with the following IN JAVA If a method throws an exception, and the exception is not caught inside the method, then the method invocation: terminates transfers control to the catch block transfers control to the exception handler none of the abovearrow_forwardJAVAarrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage