Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 16.1, Problem 3STE
Program Plan Intro
Exception:
An exception is a problem that creates during the execution of a program; it offers a method to transfer control from one part to another part of a program.
An exception handling is created by using the following three keywords such as try, catch and throw.
- The “try” block have the program for the basic
algorithm that says the computer what to do when all goes well. - The “throw” keyword throws an error statement to the “catch” block.
- The “catch” block will catch the exception or handling the exception.
Generally, the compiler executes “try” block. In the “try” block, if the statements cause an exception, it throws an error statement to the “catch” block using the keyword “throw”. The “catch” block then handles the error based upon the type of exception.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Suppose you are asked to test the code below. The code:
public String allocateRoom(int numOfEmps) throws IllegalArgumentException {
if (numOfEmps == 0)
throw new IllegalArgumentException();
if (numOfEmps < 5)
return "Small";
if (numOfEmps < 10)
return "Standard"; return "Large";
}
Throws exception if there are no employees
Returns small up to 5 employees
Returns standard for 5 to 9 employees
Returns large for 10 or more employees
List the minimum amount of test inputs that achieve 100% branch coverage. You need to specify the branch(es) each input covers.
List the minimum amount of test inputs that achieve 100% path coverage. You need to specify the path each input covers.
Explain if executing the test suites above will reveal any defects.
6.
Which of the below code contains correct overloading code? Why would the other won't
work?
Code 1
double myMultiply. (int x){
return 10 x;
Code 2
int awMultielx (int x){
return 10 x;
}
double yMultirly. (double x){ double yMultiely (int x){
return 20 x;
return 2e * x;
7.
What would be the output for the following program and why ( explain in short):
Bublis static void nain(String acsal))(
String.";
String si - "Testing Java Program
String s2 - sukstriui13);
8.
Write the code in Java for the following class from the UML diagram below:
Vehicle
-passengers: int
-maxSpeed: double
-color: String
+Vehiclelint,double.String)
+setColor(): void
+getColor(): String
+findCaracityl): double
The findCapacityl) method return the capacity of the vehicle by the following formula:
Capacity = passengers * 75 L0.20 * maxSpeed)
Part 2 - OddOrEven ClassIn bluej. Write a program that prompts the user to enter an integer. The program should display “The input is odd" to the screen if the input is odd and displays “The input is even" to the screen if the input is even. Hint: Consider using the mod (%) operator.
Chapter 16 Solutions
Problem Solving with C++ (10th Edition)
Ch. 16.1 - Prob. 1STECh. 16.1 - What would be the output produced by the code in...Ch. 16.1 - Prob. 3STECh. 16.1 - What happens when a throw statement is executed?...Ch. 16.1 - In the code given in Self-Test Exercise 1, what is...Ch. 16.1 - Prob. 6STECh. 16.1 - Prob. 7STECh. 16.1 - What is the output produced by the following...Ch. 16.1 - What is the output produced by the program in...Ch. 16.2 - Prob. 10STE
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
- 2- Trace the following code and write the output: class Test1 { Test1(int x) { System.out.println("Test Calls " + x); class Test2 { Testi t1 = new Test1(10); Test2(int i) { t1 = new Test1(i); } public static void main(String[] args) { Test2 t2 = new Test2(5);arrow_forwardJava 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.0arrow_forwardYou are to develop a set ofunit testcasesfor the following code block.Note that the code below 5 may contain some faults. The test cases you write should catch these faults and any others that might arise as the code evolves. Write at-least 5 tests, make sure that your tests cover and test evert aspect of the code.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_forwardthis practice assignment wants me to : Write an application that asks a user to enter an integer. Display a statement that indicates whether the integer is even or odd. it is saying that i am missing a return statement. here is my code: mport java.util.Scanner; class EvenOdd { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number; System.out.println("Enter the integer >>>"); number = input.nextInt(); } public static boolean isEven(int number) { if (number % 2 == 0) System.out.println("The number is even."); else System.out.println("The number is odd."); } }arrow_forwardIn Java please help with the following: Define a class Registration to implement course registration/withdrawal of students. The program should place the code into a try-catch block with multiple catches to check for the validity of various attributes based on the following criteria. a. Student ID must start with a letter and should be followed by four digits. b. The default number of courses must be at least 3. -Print suitable error matches within the catch block. If any of the criteria mentioned above is not fulfilled, the program should loop back and let the user enter new data. -Add methods for the number of courses registered and withdrawn. Create your own exception class which will check inside the method for the courses registered so that after the registration, the maximum number of registered courses should not be more than 6. Also, check inside the method for course withdrawal so that the number of courses after the withdrawal does not go less than 2. -Invoke the…arrow_forward
- JAVA Program ASAP Please modify this program with further modifications which are below so it passes the test cases in Hypergrade. I have provided the failed test cases. 1) For test cases 2 and 3 for the outputted numbers there needs to be commas and there needs to be nothing after that 2)for test case 1 outputted numbers there needs to be nothing after that, 3) for test 4 when file is not found there needs to be nothing after that 4) and for test case 5 and 7 afer a file is not found it needs to display Please re-enter the file name or type QUIT to exit: so you input the text file and displays the numbers. import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.InputMismatchException;import java.util.Scanner;public class FileSorting { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("Please…arrow_forwardComplete the code below according to the instructions below and the example test: 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 Incomplete java code: public class Account{ private String accntNumber; private String accntName; private double balance; public Account(){} public Account(String num, String name, double bal){ accntNumber = num; accntName = name; balance = bal; } public double getBalance(){ return balance;}}//your class herearrow_forwardConsider the following code: public static void test_a(int n) { System.out.println(n + " "); if (n>0) test_a(n-2); }What is printed by the call test_a(6)?arrow_forward
- What is the value stored at x, given the statements? int x; x = 6 / static_cast<int> (4.5 + 6.4);arrow_forwardSimple try-catch Program This lab is a simple program that demonstrates how try-catch works. You will notice the output when you enter incorrect input (for example, enter a string or double instead of an integer). Type up the code, execute and submit the results ONLY. Do at least 2 valid inputs and 1 invalid. NOTE: The program stops executing after it encounters an error! CODE: import java.util.Scanner; public class TryCatchExampleSimple { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = 0; System.out.println("Even number tester.\n"); System.out.println("Enter your name: "); String name input.nextLine(); } } while (true) { try { System.out.println("Enter an integer : "); num= input.nextInt (); } int mod = num%2; if (mod == 0) " System.out.println(name + else " System.out.println(name + num = 0; } catch (Exception e) { break; > System.out.println("ERROR: The number you entered is illegal!"); System.out.println("Exception error: "+e.toString());…arrow_forwardPrint "userNum1 is negative." if userNum1 is less than 0. End with newline. Assign userNum2 with 4 if userNum2 is greater than 9. Otherwise, print "userNum2 is less than or equal to 9.". End with newline. 1 import java.util.Scanner; 2 public class UserNums { public static void main (String [] args) { int userNum1; int userNum2; 4 6. 7 Scanner input userNum1 = 8. = new Scanner(System.in); input.nextInt(); input.nextInt(); 9. 10 userNum2 11 12 V* Your code goes here */ 13 System.out.println("userNum2 is " } 14 + userNum2); 15 16 17 }arrow_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
.2: Function Parameters and Arguments - p5.js Tutorial; Author: The Coding Train;https://www.youtube.com/watch?v=zkc417YapfE;License: Standard Youtube License