Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exceptions and Assertions Download program files (Lab08.zip). Propagating Exceptions: Go into subdirectory ABC and edit the ABC class. Examine the code and predict what will

Exceptions and Assertions

Download program files (Lab08.zip).

Propagating Exceptions:

Go into subdirectory ABC and edit the ABC class. Examine the code and predict what will happen if you compile this program.

Compile the program and examine the compiler error messages. What is the problem with this code?

  1. Call us over when you are ready to answer the question.

Add a throws Exception modifier to the declaration of the method C(). Predict what will happen when you compile your program with this change.

Compile the program and examine the results. Does your code continue to have problems?

  1. Call us over when you are ready to answer the question.

Add a throws Exception modifier to the declaration of the method B(). Predict what will happen when you compile your program with this change.

Compile the program and examine the results. Does your code continue to have problems?

  1. Call us over when you are ready to answer the question.

Predict what will be output when you compile and run your program with the change described above. Run the program and be prepared to explain your results.

In your code for main, add a call to method C() immediately after the calls to methods A() and B() in the try part of the try-catch block. Predict what will be output when you compile and run your program with this change. Run the program and be prepared to explain your results.

Change method B() so that it catches any exception thrown by the call to method C(). When B() catches an exception, it should throw a new exception that it constructs with the text "B is even more exceptional."

Make this change to your code and predict what will happen when you compile and run your program. Compile and run it and examine your results.

  1. Finally, in your ABC class modify the C() method by removing the throw statement, replacing it with the statement System.out.println("in C..."). Predict what will happen when you run your modified program.

Compile and run your modified program and examine your results.

Assertions:

Go into the directory GT5 and edit the class Demo.

Study the code for the method gt5() (which stands for greater than 5). The intended purpose of this method is to return true if it is passed a value greater than 5 and false otherwise. The assert near the end of this method should never be executed (why?).

Normally, an assert will behave like an exception, immediately terminating the procedure. Why is there a final return statement after the assert?

Why does the assert statement have false as the assert condition?

Compile and run this program with a number of different integer input values, and observe the behavior of your program. Enter "quit" to terminate the program. Can you find an integer input value that triggers the assert statement?

  1. Change the else if statement in the gt5() method so that the condition is (n

Compile and run your program. Can you find an integer input value that triggers the assert statement? If so, how does your program behave with this input value?image text in transcribedimage text in transcribed

import java.util.*; class ABC public static void main(String() args) { try { AO; BO; } catch (Exception e) { System.out.println("in main..."); System.out.println(e); } } public static void AO { try { BO; } catch (Exception e) { System.out.println("in A..."); System.out.println(e); } } public static void BO { CO; } public static void CO { throw new Exception("C is exceptional"); } } import javax.swing.*; public class Demo { public static void main(String () args) { int number; String inputString; while (true) { inputString = JOptionPane.showInputDialog(null,"Enter an integer"); if (inputString.equals("quit")) System.exit(0); number = Integer.parseInt(inputString); if (gt5(number)) JOptionPane.showMessageDialog(null, "Greater than 5"); else JOptionPane.showMessageDialog(null, "Not greater than 5"); } public static boolean gt5(int n) { if (n> 5) return true; else if (n 5) return true; else if (n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago