IMPORTANT. Make sure you attend the lab session to which you registered, show the result(s) of each exercise to your TA, and submit the .java files that you wrote and AnswersLab7.txt through OWL by 11:55pm on the same day as your lab session to get your marks for the lab.
Download the Exception Examples 2, 10, and 11 from the Sample Code section of the course's webpage.
ExceptionExample2
in Eclipse. The program has a compilation error in the statement where
method check
is invoked. Why is the error produced? Is this an example of a checked exception or an
unchecked exception? Write your answer in AnswersLab7.
try-catch
statement; put only the statement
if (check(students[0])) System.out.println("First student's data is correct");
inside the
try
statement. In the catch
statement print the string stored inside the exception that
was caught (use method getMessage()
to do this).
ExceptionExample2
. Why does the program crash? Write your answer in AnswersLab7. This is an
example of an unchecked exception; the compiler did not check that the program could throw this exception, so it
did not show a compilation error.
try {
statement up so that the try
block includes the statement
if (students[0].equals(""))students[0] = "John Doe";
(so inside the try
block there will be
the two if
statements). Since now two different types of exception can be thrown inside the try
statement, then add a second catch
statement to your program; place the new catch
statement before the catch (Exception e)
one. In this new catch
statement write code to catch
an exception of the type NullPointerException
and print the message "Error: student object is null"
when the exception is caught. Run the program. Which exception is thrown: and exception of type
NullPointerException
or an exception of type Exception
? Write your answer in AnswersLab7.
catch
so catch (Exception
appears before
catch (NullPointerException
. Why does the compiler give a compilation error? Write your answer in
AnswersLab7. If you are not sure about the answer, read this hint.
catch
statement for NullPointerException
. Run the
program. Why is only the word "null" printed? Write your answer in AnswersLab7. If you are unsure about the
answer read this hint.
ExceptionExample10
and enter an invalid input like "a". What exception is thrown? Is this a
checked exception or an unchecked exception? Write your answer in AnswersLab7.
ExceptionExample11
so that the program asks the user for another
input for as long as the input is not a number. When the user enters a number the program must print the
message "Success" and then terminate. Show your program to your TA.
If you struggle, read this hint.
Download BuildLinkedList.java and LinearNode.java
from the section "Linked Data Structures"
of the Sample
Code page.
BuildLinkedList
creates a linked list with 10 nodes, each storing an integer value
from 1 to 10. Run the program; it must print the 10 numbers stored in the nodes of the list.
printList
has "hard-coded" in it the length of the list, so it will work correctly only
on lists that have exactly 10 nodes. Modify this method so that it works for lists of any length. You cannot change
the signature of the method (i.e. cannot add more parameters). You cannot add instance variables to the class either.
Run your program and verify that it prints all the values in the list. Then change the for
loop in the
main
method to create a list with 20 nodes. Run the program and verify that the information in all the
nodes is printed. Show your program to your TA.