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 27STQ
Explanation of Solution
Given program segment:
//Try block
try
{
//Assign a value to the variable
int n = 7;
//Checking if value is greater than 0
if(n > 0)
//Throwing a predefined exception
throw new Exception();
//Checking if n is lesser than 0
else if (n < 0)
//Throwing user-defined exception
throw new NegativeNumberException();
//Else
else
//Print Hello
System.out.println("Hello!");
}
//Catch the user-defined exception
catch (NegativeNumberException e)
{
//Print the message
System...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
*This is a handout from my class that I'm having trouble with I tried YouTube videos and I'm still lost please explain the answers as simply as you can and thank you.*
A. Look over the following code fragment, give the output:
try {
method();
System.out.println("After the method call");
}
catch (Exception ex) {
System.out.println("Exception in main");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException in main");
}
static void method() throws Exception {
System.out.println(1 / 0);
}
B. Write the code to load an array. Use simple I/O (system.out.print, etc.) to get integer values from the user and load the array (loop?). Your code has the potential to generate 2 exceptions: 1) InputMismatch (research this) and 2) IllegalValueException (assume this exists). If the 1st occurs, change the input to be 99, warn the user and continue. The 2nd exception will occur when the user enters a 0; if this occurs, notify the user that a 0 has been entered, and change the input to…
Explain in detail the following code snippet that is given in the image
Java Questions - Based on the code, which answer out of the choices "1, 2, 3, 4, 5" is correct. Explanation is not needed, just please make sure that the answer you have chosen is the correct option. Thanks .Question is in attached picture.
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
- This is a Java code - Which is output when inputVal is 0?arrow_forwardjavaarrow_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
- Advanced programming applications using javaarrow_forwardUsing JavaFX, create a simple calculator application, like the following: The operators is ( + : " addition process" , -:subtraction process", *:"multiplication process" , /:"division process" , C : "Clear" , H: "History"); Validate that the first number and second is a numeric value if it is not throwing an exception. When clicking the operators (+, -, *, /): the result will display at the bottom, and stored in history. When clicking C, clear the first, second number and the history5 When clicking H, the history will display at the bottom.6 When the user click / and the second number is 0 throw an arithmetic exception.arrow_forwardUsing JavaFX, create a simple calculator application, like the following: The operators is ( + : " addition process" , -:subtraction process", *:"multiplication process" , /:"division process" , C : "Clear" , H: "History"); Validate that the first number and second is a numeric value if it is not throwing an exception. When clicking the operators (+, -, *, /): the result will display at the bottom, and stored in history. When clicking C, clear the first, second number and the history5 When clicking H, the history will display at the bottom.6 When the user click / and the second number is 0 throw an arithmetic exception. help_outlinefullscreenarrow_forward
- void foo () { try { throw new Exception1 (); print (" A "); throw new Exception2 (); print (" B "); } catch ( Exception1 e1 ) { print " handler1 "; } print (" C "); throw new Exception2 (); } void main () { try { try { foo (); print (" D "); } catch ( Exception1 e1 ) { print " handler2 "; } print (" E "); } catch ( Exception2 e2 ) { print " handler3 "; } } Write down the output of the program and justify why it is the output. Instead of the “replacement” semantics of exception handling used in modern languages, a very early design of exception handling introduced in the PL/I language uses a “binding” semantics. In particular, the design dynamically tracks a sequence of “catch” blocks that are currently active; a catch block is active whenever the corresponding try block is active. What will be the output of the following program if the language uses the “binding” semantics? Give the reasoning for your answer.arrow_forwardplease solve this important, thanks.arrow_forwardSuppose 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.arrow_forward
- Write a class TimeOfDay that uses the exception classes defined in the previous exercise. Give it a method setTimeTo(timeString) that changes the time if timeString corresponds to a valid time of day. If not, it should throw an exception of the appropriate type. [Java]arrow_forwardLet's revisit chapter 5 example, but this time, no method can throw any exception out of the method and if an exception was detected then you will have to throw your own custom exception to let the user know what happened. public class Chapter11Demo{ public static void main(java.lang.String[] args) {method1();}private static void method1(){ method2();}private static void method2(){ method3();}private static void method3(){ method4();}private static void method4(){ method5();}private static void method5(){ method6();}private static void method6(){ java.io.File in=new java.io.File("somefile.txt"); java.io.File out=new java.io.File("somefile2.txt"); java.util.Scanner inFile=new java.util.Scanner(in); java.io.FileWriter outFile=new java.io.FileWriter(out); }}arrow_forwardsolve using java:(thanks for helping)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