CIS163AA - Java Prog I - Sect 5811 - Worksheet #8 - by Pat Moss

Due Monday, December 5, 2005

Name: Pat Moss

I.  Definitions:  Please explain the following terms:

 1. polymorphism -- An object-oriented technique by which a reference
    (p. 656)        that is used to invoke a method can result in
                    different methods being invoked at different times.
                    All Java method invocations are potentially
                    polymorphic in that they invoke the method of the
                    object type, not the reference type.

 2. binding      -- The process of associating an identifier with the
    (p. 641)        construct that it represents. For example, the 
                    process of binding a method name to the specific
                    definition that it invokes.

 3. dynamic binding -- The process of associating an identifier with
    (p. 646)           its definition during run time.

 4. sorting      -- The process of putting a list of values into a
    (p. 659)        well-defined order. See also insertion sort and
                    selection sort.

 5. searching    -- The process of determining the existence or location
    (p. 659)        of a target value within a list of values. See also
                    binary search and linear search.

 6. exception    -- (1) A situation that arises during program execution
    (p. 647)            that is erroneous and out of the ordinary.
                    (2) An object that can be thrown and processed by
                        special catch blocks. See also error.

 7. try-catch statement -- 
    a. try       -- A Java reserved word that is used to define the
       (p. 662)     context in which certain exceptions will be handled
                    if they are thrown.
    b. catch     -- A Java reserved word that is used to specify an
       (p. 642)     exception handler, defined after a try block.

 8. stream       -- A source of input or a destination for output.
    (p. 660)

II. Question or short essay

 1. Why does a binary search require that data be sorted?
    A binary search follows this algorithm:
    a. Calculate half of the distance from the current lowest
       index to the current highest index.
    b. Examine the data value at that index location.
    c. If the search argument is less than b., then set the
       new current highest index to that index location.
    d. If the search argument is greater than b., then set the
       new current lowest index to that index location.
    e. continue a. thru d. until either the value is found, or
       until the low and high indexes meet.

    This algorithm at steps c. and d. depends on comparing the
    search argument to the current index value, and then deciding
    whether to search in the first half or last half of the 
    remaining table items.

    Thus, in order to decide which half of the table contains the
    search argument value, the table values must be in sorted order.

 2. What is the general strategy of the selection sort?
    (p. 659) A sorting algorithm in which each value, one at a time,
             is placed in its final, sorted position.
    (p. 498-504) 
    a. Scan the entire list to find the smallest value. 
    b. Exchange that value with the value in the first position in the list.
    c. Scan the remainder of the list for the second smallest value.
    d. Then exchange it with the value in the second position in the list.
    e. Repeat this algorithm until all values are in their sorted postion.

 3. How do you set up a listener to respond to a JButton object?
    (p. 188, 822)
    a. declare private JButton push;
    b. declare private class ButtonListener implements ActionListener
    c. declare public void actionPerformed (actionEvent event)
       to perform actions when the JButton events occur.

 4. List 3 examples of situations that can cause exceptions.
    a. Divide by zero exception.
    b. Array index out-of-bounds exception.
    c. Overflowing, or underflowing, an arithmetic accumulator
       (e.g., overflowing/underflowing an integer data type, or
        some other numeric primitive).

 5. How are errors handled differently from exceptions?
    a. errors (p. 542, 646)
       An object that can be thrown and processed by special catch
       blocks, though usually errors should not be caught. See also
       compile-time error, logical error, run-time error, syntax error,
       and exception.
       Error class hierarchy:
       LinkageError, ThreadDeath, VirtualMachineError, and AWTError
       can call Error.
    b. exceptions (p. 542, 
       An object that can be thrown and processed by special catch
       blocks.
       Exception class hierarchy:
       RunTimeExceptions (ArithmeticException, IndexOutOfBoundsException,
                          NullPointerException),
       Illegal AccessException, NoSuchMethodException, ClassNotFoundException
       can call Exception.

 6. How is the "finally" clause used? (p. 537-538)
    The try-catch statement can have an optional finally clause.
    The finally clause defines a section of code that is executed no 
    matter how the try block is exited. Most often, a finally clause
    is used to manage resources or to guarantee that particular parts
    of an algorithm are executed.

III. Programming Exercises  
 Modify the rebound program from chapter 8, page 471, such that when
 the mouse button is clicked on the panel the animation stops, and
 when it is clicked again the animation resumes.