First, copy the lab materials to your account.
cp -r /home/Classes/Cs1/Labs/Lab08 .Everything you need for the lab exercises today is contained in this new directory.
Change directory to Lab08.
Now run the program and enter "abc" into the text field. Notice that this causes your program to throw a NumberFormatException. Look at the stack trace. At what line in the file MyJFrame.java is the exception thrown? Enclose this line of code in a try-catch. When this type of exception is caught, print an error message using System.out.println(...) and then exit the program using System.exit(1).
1
Call us over when you are ready with your modification.
Now comment out the System.exit(1) that should be in your catch block. Compile and run your program again. Try entering "abc" again. What happens that is different?
2 Call us over when you are ready to explain the difference in behavior
in your program.
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?
3 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?
4 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?
5 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.
6 Call us over when you are ready 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.
7 Call us over when you are ready 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.
8 Call us over when you are ready to show us your results and explain
them.
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.
9 Call us over when you are ready to show us your results.
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?
10
Call us over when you are ready to explain the answers
to your questions.
Change the else if statement in the gt5() method so that the condition is (n < 5) instead of (n <= 5). Observe that this is a programming error that someone might indeed make.
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?
11
Call us over when you are ready to explain the answers
to your questions.