This lab follows the general format used in our first lab (Lab07). First, unzip the lab materials. Everything you need for the lab exercises today is contained in this new directory.
Open Eclipse and select File -> New Project to create a new Java Project. Use the project name yourLastName08. Interpret the words "Go into subdirectory... and edit class..." as File -> Import -> File System -> Next as was done in the last lab.
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.
(1)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).
Now comment out the System.exit(1) that should be in your
catch block. Compile and run your program again. Try entering
"abc" again.
(2)What happens that is different?
Modify
the constructor to pass the string literal "Click to increment" to the button's setToolTipText method.
Modify the constructor to pass the string literal "Input
decimal or hexadecimal (begins with 0x) value to set the button count." to the inputLine's setToolTipText method.
Compile and run your program.
(3)What happens that is different?
In your NumberFormatException catch block, check the first two characters to see
if a hexadecimal number has been entered. If so, convert the remaining
characters entered to hexadecimal using Integer.parseInt (str, 16), where str is
the substring with characters from position 2 to the end of the input line, and assign
this value to buttonCount. So if 0xf is entered the button will display 15.
(4)What specific exception must be caught so your program continues to run
in the event a null string or nonnumeric string of length 1 is entered?
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?
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?
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?
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."
(5)Make this change to your code and predict what will happen when you compile and run your program. Compile and run it and explain your results.
Finally, in your ABC class modify the C() method
by removing the throw statement,
replacing it with the statement System.out.println("in C...").
(6)Predict what will happen when you run your modified program.
Compile and run your modified program and examine your results.
Email to me your solutions to the six questions. Be sure to label each question
(1..6), put CSC 121 Lab 08 in the subject, and put your name and solutions in the body of the
email.