CIS163AA - Java Prog I - Sect 5811 - MidTerm - by Pat Moss

Due Monday, October 24, 2005

Name: Pat Moss

I.  Definitions:  Please explain the following terms:

 1. post-fix increment -- The ++ is placed after its single operand.
    (p. 656)              Example:

                          int count = 5;
                          int x = count++;

                          The value count is copied to x.
                          Then count is incremented by one.

 2. counter variable   -- A variable whose value specifically determines
                          how many times a loop body is executed.

 3. final data member  -- "final" is a Java reserved word.
                          It can apply to classes, methods, and variables.
                          A final data member is treated as a constant
                          value; it cannot be changed during program
                          execution.

 4. loop               -- In a digital computer, there are two ways to
                          repeat a block of instructions, viz., iteration
                          and recursion. Iteration utilizes a program loop.
                          For the program loop construct, we use a loop control
                          counter. Each time around the loop, we test the
                          loop control counter value against an exit sentinel
                          value. We repeat the instructions in the block until
                          the comparison tests true. Then we exit from the loop.

 5. loop sentinel value - The loop control counter is tested against this value
                          to determine when to exit from the loop.
 
                          The loop control construct is of the form:

                          int counter = min_value; // initialize the counter
                          if (counter <= sentinel-value)
                          {
                           // increment the counter
                           // execute the block of instructions in the loop
                           // return to the if statement
                          } else {
                           // exit from the loop
                          }

                          The test and increment can be placed either at the
                          top or bottom of the loop construct.

 6. continue statement -- There are two statements that allow us to skip the
    (p. 238)              remaining instructions in a loop:

                          a. break = exit permanently from the loop
                          b. continue = skip the body of the loop, and continue
                                        again at the top of the loop
                          Example using continue:
                          for (int i=0;i < 10; i++) {
                           if (i == 5)  // skip the body when i equals 5
                            continue;
                             etc.
                          }

 7. visibility modifier - A Java modifier that defines the scope in which
    (p. 164-5, p. 663)    a constructor can be accessed. The Java visibility
                          modifers are public, protected, private, and
                          default (no modifier used).

 8. private visibility -- A Java reserved word that serves as a visibility
                          modifier for methods and variables.

                          Private methods and variables are not inherited
                          by subclasses, and can only be accessed in the
                          class in which they are declared.

 9. relational operator - One of several operators that determine the
    (p. 658)              ordering relationship between two values.
                          There are four, as follows:

                          <
                          <=
                          >
                          >=

II. Question or short essay

 1. What is the value of creating an outline of your solution before
    you begin coding?

    a. It helps to clarify and define the code.
    b. It helps to avoid "spagetti" code, and rewriting of the algorithms.
    c. It offers proof that the program code conforms to the design specs.
    d. It serves as documentation for an in-house "code walkthrough".
    e. It serves as a form of documentation for later reference.

    Many times, the logic structure for a large program becomes much too
    complex to "keep in one's head". It must be written down, and examined
    for completeness, consistency, efficiency, etc., before expending the
    considerable time and effort and expense to commit it to program code.

 2. How are constructors defined?

    A constructor has the same name as the class. If has no return type
    declaration, and does not return any value.

    A constructor is a special method in a class that is invoked when an
    object is instantiated from the class.

 3. What is the purpose of the init method for an applet? (p. 693)

    It initializes the applet.
    It is called just after the applet is loaded.

 4. What is an infinite loop? How do you avoid it?

    A program loop construct contains three parts:
    initialize, increment (or decrement), and test.

    We test a loop control counter vs. an exit sentinel value.

    The loop continues to execute until the test condition becomes true.

    So, if the counter value never intersects with the sentinel value,
    the test condition never becomes true, and the loop will continue
    to execute indefinitely. We call this condition an "infinite loop".

    For example:

    for (int i=0; i < 10; i=1)
    {
      // body of an infinite loop
    }

    To prevent an infinite loop, we must be careful to control the
    three parts of the loop, so that the test will become true in a
    consistent, predictable manner.

 5. How do you use a break statement in a switch structure? (p. 223-227)

    Following each condition in a switch statement, a break statement
    is required in order to exit from the switch construct.

    For example:

    switch (idChar)
    {
     case 'A':
      aCount++;
      break;
     case 'B':
      bCount++;
      break;
     default:
      undefCount++;
    }

III. Programming Exercises  

  1. Write a cube class. It has three essential characteristics: a height,
     width, and length (these don't have to be the same size). Each face
     of the cube is a rectangle and has an area. The cube also has a volume
     defined as the product of the essential characteristics, as well as a
     surface area defined by the sum of the areas of each face. Include a
     constructor, a method to find the volume, and a method to find the
     surface area.

     Hand in a printed copy of your source code.

     Note: Can use float with one decimal of precision.