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
Expert Solution & Answer
Chapter 9.3, Problem 24STQ
Explanation of Solution
“throws” clause:
- • This clause is used to declare the exception. This provides information to the programmer that the
program might produce an exception so it is better to give the exception handling code which maintains the normal flow of the program. - • It is usually given in the method header.
Syntax:
Method_Name throws ExceptionName
{
//statements
}
Adding “throws” clause to the given method:
//throws clause is added to the Method header
public void ...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
please solve this important, thanks.
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
Java Foundations :
Please provide code and explain. Thank you.
Write the code segments in main( ) and in methodA( ) for the following scenario:
main( ) passes methodA an int variable called a.
methodA generates an exception, but does not deal with it, if a is not between 1 and 100.
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
Similar questions
- // MichiganCities.java - This program prints a message for invalid cities in Michigan. // Input: Interactive. // Output: Error message or nothing. import java.util.Scanner; public class MichiganCities { public static void main(String args[]) throws Exception { // Declare variables. String inCity; // name of city to look up in array. // Initialized array of cities in Michigan. String citiesInMichigan[] = {"Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"}; boolean foundIt = false; // Flag variable. int x; // Loop control variable. // Get user input. Scanner input = new Scanner(System.in); System.out.println("Enter the name of the city: "); inCity = input.nextLine(); // Write your loop here. for (x=0;x<= citiesInMichigan.length;x++){ if(citiesInMichigan[x]==inCity){ foundIt = true;…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_forwardJAVA EXCEPTION HANDLING: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. For example: Input Result 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 fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped…arrow_forward
- Please 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_forwardplz help with the following: IN JAVA Why does the following method have a compile errorarrow_forwardWrite a Month class that holds information about the month. Write exception classes for the following error conditions:• A number less than 1 or greater than 12 is given for the month number.• An invalid string is given for the name of the month.Modify the Month class so that it throws the appropriate exception when either of these errors occurs. Demonstrate the classes in a program.arrow_forward
- using System; namespace ErrorHandlingApplication{class DivNumbers{int result; DivNumbers(){result = 0;}public void division(int num1, int num2){try{result = num1 / num2;}catch (DivideByZeroException e){Console.WriteLine("Exception caught: {0}", e);}finally{Console.WriteLine("Result: {0}", result);}}static void Main(string[] args){DivNumbers d = new DivNumbers();d.division(25, 0);Console.ReadKey();}}} Modify this c# program on which enables users to enter value desired.arrow_forwardI need the answer within 15 minutesarrow_forwardSubject: 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_forward
- The following class maintains an account balance and returns aspecial 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 depositinvalid amounts and catches the exceptions that are thrownarrow_forwardWrite codesarrow_forwardJAVAarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education