Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 9.3, Problem 30STQ
Explanation of Solution
Given
//Main Class
class Question30
{
//Main method
public static void main(String[] args)
{
//Create an object for the class
Question30 object = new Question30();
try
{
//Print the statement
System.out.println("Trying");
//Call the method
object.sampleMethod();
//Print the statement
System.out.println("Trying after call.");
}
//Catch the exception
catch(Exception e)
{
//Print the statement
System...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Code completion and explain.
public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (RuntimeException ex) /* Line 10 */ { System.out.print("B"); } catch (Exception ex1) { System.out.print("C"); } finally { System.out.print("D"); } System.out.print("E"); } public static void badMethod() { throw new RuntimeException(); } }.
Analyze the following code:
public class Test{
public static void main(String[] args) {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
}
}
catch (Exception ex) {
System.out.println("NumberFormatException");
int i = 0;
int y = 2/i;
}
catch (RuntimeException ex) {
}
}
System.out.println("RuntimeException");
The program displays NumberFormatException.
The program displays RuntimeException.
The program displays NumberFormatException followed by RuntimeException.
The program has a compile error.
Look at the following program and tell what it will output when run:public class ExceptionTest{public static void main(String[] args){int number;String str;try{str = "xyz";number = Integer.parseInt(str);System.out.println("A");}catch(NumberFormatException e){System.out.println("B");}catch(IllegalArgumentException e){System.out.println("C");}System.out.println("D");}}
Chapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (8th 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 - Prob. 36STQCh. 9.4 - Prob. 37STQCh. 9.4 - Prob. 38STQCh. 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 - Suppose that you are in change of customer service...Ch. 9 - Write an application that implements a trip-time...
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
- Explain in detail the following code snippet that is given in the imagearrow_forwardoutput of the program? public class X { public static void main (String [] args) try { badMethod (); System.out.print ("A"); catch (RuntimeException ex) /* Line 10 */ { System.out.print ("B"); catch (Exception ex1) { System.out.print ("C"); finally { System.out.print("D"); System.out.print ("E"); public static void badMethod (). throw new RuntimeException (); А. BD В. ВCD C. BDEarrow_forward// JumpinJive.java - This program looks up and prints the names and prices of coffee orders. // Input: Interactive. // Output: Name and price of coffee orders or error message if add-in is not found. import java.util.Scanner; public class JumpinJive { public static void main(String args[]) throws Exception { // Declare variables. String addIn; // Add-in ordered by customer. final int NUM_ITEMS = 5; // Named constant // Initialized array of add-ins. String addIns[] = {"Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"}; // Initialized array of add-in prices. double addInPrices[] = {.89, .25, .59, 1.50, 1.75}; boolean foundIt; int x; // Loop control variable. double orderTotal = 2.00; // All orders start with a 2.00 charge // Get user input. Scanner input = new Scanner(System.in); System.out.print("Enter coffee add-in or XXX to quit: "); addIn = input.nextLine();…arrow_forward
- Please Solve it in javaarrow_forwardAdvanced programming applications using javaarrow_forwardExample Code Ch 11-1public static void main(String[] args){try{ExceptionThrowerCode etc = new ExceptionThrowerCode();etc.m1();etc.m2();}catch (ArithmeticException ae) { ... }}public class ExceptionThrowerCode{...public void m1(){...}public void m2(){try{m3();}catch(ArithmeticException ae) {...}catch(NullPointerException npe) {...}}public void m3(){try{...}catch(ArithmeticException ae) {...}}} 8. Refer to Example Code Ch 11-1: If a NullPointerException arises in the try statement inm1a. it is caught in mainb. it is caught in m1c. it is caught in m2d. it is caught in m3e. it is not caught and the program terminatesarrow_forward
- Examine this code and determine what happens when it is run: 1 public class Test { int x; 2 3 public Test(String t){ System.out.println("Test"); } public static void main(String[] args) { Test test = new Test ("boo"); System.out.println(test.x); } 4 5 6 7 8 10 } The program has a compile error because Test does not have a default constructor. The program has a compile error because System.out.println method cannot be invoked from the constructor. ) The program runs successfully and prints out: Test O The program has a compile error because you cannot create an object from the class that defines the object.arrow_forwardpublic olass Exam { 6. public static void main(String [] args) { int all =(1,2,3); for (int i=0; i<5; i++) { What is the output of the following code try{ System.out.print (alil): )catch (IndexoutofBoundsException e) ( System.out.print ("E") O 123 123E 123EE 12EE publia alass Exam ( public static void main (String [1 args) ( try ( What is the output of the following question (2 Points) int data = 25/0; System.out.printih (data)/indows }catch (ArithmeticException e)o activate System.out..print ("D");arrow_forwardCompile and run the following Java programming code and observe the result. public class ExceptionCheck { public static void main(String args[]) { try{ System.out.println ("statement 1"); //System.out.println (5/0); System.out.println ("statement 2"); System.out.println ("statement 3"); } catch (ArithmaticException e) { // catch (FileNotFoundException e) System.out.println(10/2); //System.out.println(10/0); } System.out.println ("statement 4"); } } What can you say about the termination of the program in following cases? (a) If no exception in the program, (b) If statement 2 has the exception, (replace statement 2 with System.out.println (5/0);) (c) If corresponding catch block is not available, // catch (FileNotFoundException e) (d) If the exception occurs at the catch block, (remove comment //System.out.println(10/0);) (e) If the exception occurs at statement 4arrow_forward
- public class TestShape { public static void main(String[] args) { /* Circle c1 = new Circle(2.67); System.out.println("c1: "); System.out.println(c1.toString()); System.out.println(); Circle c2 = new Circle(3, false, "Red"); System.out.println("c2: "); System.out.println(c2.toString()); System.out.println(); Rectangle r1 = new Rectangle(3, 2, true, "Blue"); System.out.println("r1: "); System.out.println(r1.toString()); System.out.println(); Rectangle r2 = new Rectangle(3.2, 4, false, "Red"); System.out.println("r2: "); System.out.println(r2.toString()); */ } }arrow_forward13 What changes need to be made to the main method to avoid the compilation error. public class C { static void ml (int x) throws Exception{ if(x ==0) throw new Exception (); else System.out.println ("Correct"); public static void main (String[] args) { m1 (0); O Sol1: public static void main(String args) ( try (m1(0); } catch (Exception e) {} } O Sol2: public static void main(String[] args) throws Exception{ m1 (0): } Activate Windows So te Settrgs to activate Sol1 & Sol2arrow_forwardEncapsulation Do quick 15 min left.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