Exercises


[Page 507 (continued)]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 10.1

Explain the difference between the following pairs of terms:

  1. Throwing an exception and catching an exception.

  2. Try block and catch block.

  3. Catch block and finally block.

  4. Try block and finally block.

  5. Dynamic scope and static scope.

  6. Dialog box and top-level window.

  7. Checked and unchecked exception.

  8. Method stack and method call.


[Page 508]
Exercise 10.2

Fill in the blanks.

  1. _______ an exception is Java's way of signaling that some kind of abnormal situation has occurred.

  2. The only place that an exception can be thrown in a Java program is within a ________.

  3. The block of statements placed within a catch block is generally known as an ________.

  4. To determine a statement's________scope, you have to trace the program's execution.

  5. To determine a statement's scope,________you can just read its definition.

  6. When a method is called, a representation of the method call is placed on the ________.

  7. The root of Java's exception hierarchy is the________class.

  8. A________exception must be either caught or declared within the method in which it might be thrown.

  9. An________exception can be left up to Java to handle.

Exercise 10.3

Compare and contrast the four different ways of handling exceptions within a program.

Exercise 10.4

Suppose you have a program that asks the user to input a string of no more than five letters. Describe the steps you'd need to take in order to design a StringTooLongException to handle cases where the user types in too many characters.

Exercise 10.5

Exceptions require more computational overhead than normal processing. Explain.

Exercise 10.6

Suppose the following ExerciseExample program is currently executing the if statement in method2():

public class ExerciseExample {   public void method1(int M) {     try {       System.out.println("Entering try block");       method2( M );       System.out.println("Exiting try block");     } catch (Exception e) {       System.out.println("ERROR: " + e.getMessage());     }   } // method1()   public void method2(int M) {     if (M > 100)       throw new ArithmeticException(M + " is too large");   }   public static void main(String argv[]) {     ExerciseExample ex = new ExerciseExample();     ex.method1(500);   } } // ExerciseExample class 



[Page 509]

Draw a picture of the method call stack that represents this situation.

Exercise 10.7

Repeat the preceding exercise for the situation where the program is currently executing the second println() statement in method1().

Exercise 10.8

Draw a hierarchy chart that represents the static scoping relationships among the elements of the ExerciseExample program.

Exercise 10.9

What would be printed by the ExerciseExample program when it is run?

Exercise 10.10

What would be printed by the ExerciseExample program, if the statement in its main() method were changed to ex.method1(5)?

Exercise 10.11

Consider again the ExerciseExample program. If the exception thrown were Exception rather than ArithmeticException, explain why we would get the following error message: java.lang.Exception must be caught, or it must be declared . . . .

Exercise 10.12

Write a try/catch block that throws an Exception if the value of variable X is less than zero. The exception should be an instance of Exception, and when it is caught, the message returned by getMessage() should be "ERROR: Negative value in X coordinate".

Exercise 10.13

Look at the IntFieldTester program (Fig. 10.23) and the IntField class definition (Fig. 10.22). Suppose the user inputs a value that is greater than 100. Show what the method call stack would look like when the IntField.getInt() method is executing the num > bound expression.

Exercise 10.14

As a continuation of the preceding exercise, show what the program's output would be if the user input a value greater than 100.

Exercise 10.15

As a continuation of the preceding exercise, modify the IntOutOfRange-Exception handler so that it prints the message call stack. Then show what it would print.

Exercise 10.16

Define a subclass of RuntimeException named InvalidPasswordException that contains two constructors. The first constructor takes no parameters and an exception thrown with it should return "ERROR: invalid password" when its getMessage() is invoked. The second constructor takes a single String parameter. Exceptions thrown with this constructor should return the constructor's argument when getMessage() is invoked.

Exercise 10.17

Extend the IntField class so that it will constrain the integer JTextField to an int between a lower and upper bound. In other words, it should throw an exception if the user types in a value lower than the lower bound or greater than the upper bound.

Exercise 10.18

Design Issue: One of the preconditions for the InsertionSort() method (Fig. 9.15) is that its array parameter not be null. Of course, this precondition would fail if the array were passed a null array reference. In that case, Java would throw a NullPointerException and terminate the program. Is this an appropriate way to handle that exception?

Exercise 10.19

With respect to the preceding exercise, suppose you decide that it is more appropriate to handle the NullPointerException by presenting an error dialog. Modify the method to accommodate this behavior.

Exercise 10.20

Design Issue: Another possible way to design the sequentialSearch() method (Fig. 9.18) would be to have it throw an exception when its key is not found in the array. Is this a good design? Explain.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net