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

Due Monday, October 31, 2005

Name: Pat Moss

I.  Definitions:  Please explain the following terms:

 1. conditional statement -- A programming construct that allows a set
    (p. 659)                 of statements to be executed if a particular
                             condition is true.

                             Example:
                             if (x < 5) {
                               y = 10;
                             } else {
                               z = 20;
                             }

 2. conditional operator  -- A Java ternary operator that evaluates one
    (p. 644, p. 217)         of two expressions based on a condition.

                             Example:
                             total = (total < MAX) ? subtot+1 : subtot+2;

 3. iterator              -- An iterator is an object that has methods
    (p. 238-242)             that allow you to process a collection of 
                             items at one time.

                             Example:
                             while (fileScan.hasNext()) {
                              url = fileScan.nextLine();
                                etc.
                             }

 4. boolean expression    -- An expression that evaluates to a true or
    (p. 641)                 false result, primarily used as conditions
                             in selection and repetition commands.

 5. compareTo method      -- The compareTo method can be used to determine
    (p. 222)                 the relative order of strings.

                             Example:
                             int result = name1.compareTo(name2);
                             if (result < 0)
                              { // name1 < name2 }
                             else
                              { if (result > 0)
                                 { // name1 > name2 }
                                else
                                 { // names are equal }
                              }

 6. radio button          -- A GUI component that allows the user to choose
    (p. 657)                 one of a set of options with a mouse click.

 7. block statement       -- A group of programming statements and
    (p. 641)                 declarative statements demited by braces { }.

                             Example:
                             if (a < b) {
                               // true block
                             } else {
                               // false block
                             }

 8. dialog box            -- A graphical window that pops up to allow
    (p. 645)                 brief, specific user interaction.

II. Question or short essay

 1. logical operators vs relational operators
    a. logical operators (p. 653)
       These operators return a boolean result:

         !   NOT
         &&  AND
         ||  OR

       Example:
       boolean tf = (a < 5) && (b == 10) || (c != 'x');

    b. relational operators 
       One of four operators that determine the ordering relationship
       between two values:

         <   Less than
         <=  Less than or equal to
         >   Greater than
         >=  Greater than or equal to

       Example:
       if (a < b) {
        // a is less than b
       } else {
        // a is not less than b
       }

 2. for statement (p. 648, p. 245-251)
    The for statement is executed zero or more times, and is usually used
    when a precise number of iterations is known.

    Example:
    for (int count=1; count<=5; count++)
    { 
     // body of loop
    }

    Explanation:
    a. Initialize the loop counter to 1: count = 1;
    b. If the boolean expression is true: if count<=5;
    c.   Then execute the instructions in the body of the loop
    d.   And increment the loop counter: count++;
    e.   And return to step b.
    f. Else exit from the loop. 
    
 3. flow of control (p. 202)
    Program flow in a digital computer is based on the von Neumann model,
    which consists of a program (P) counter, an instruction (I) register,
    and an accumulator (A) register.

    Explanation of the von Neumann model:
    a. Set the P counter to an initial value (the location in memory
       of the first instruction of your program).
    b. Fetch the instruction -- that is contained in the memory location
       pointed to by the value in the P register -- to the I register.
    c. Execute the instruction contained in the I register.
    d. If the instruction is a branch instruction,
       d1. If the test is true, copy the branch location into the P register
           and return to step b.
       d2. If the test is false, continue with step e.
    e. Increment the P register value by one.
    f. Return to step b.

    So, we see that the CPU processes each instruction sequentially until
    it encounters a branch instruction. Then the CPU either branches to
    another location in the program, or continues on sequentially, depending
    on the true/false result of the branch instruction.

    This branching concept is a.k.a. "selection".

 4. nested loop
    When a loop is contained inside another loop structure, this is called
    a "nested loop".

    Example:
    for (int i=1; i<=5; i++) {
     for (int j=1; j<=10; j++) {
      // execute nested instructions
      // for example: access row/column elements
      // of a two-dimensional array a[i][j]
     }
    }

 5. compare strings for equality (p. 222)
    The String class contains a method called "equals" that returns
    a boolean value that is true if the two string being compared 
    contain exactly the same characters, and is false otherwise.

    Example:
    if (name1.equals(name2)) {
     // the two strings are equal
    } else {
     // the two strings are not equal
    }

 6. check boxes vs radio buttons
    a. check box
       (1) appears as a small square
       (2) when selected, contains a check icon
       (3) user can select 0, 1, or more choices
           among check boxes
       (4) uses an ItemListener (see p. 267)
    b. radio button
       (1) appears as a smail circle
       (2) when selected, contains a black dot
       (3) user can select only one of a related
           group of radio buttons (mutually exclusive)
       (4) uses an ActionListener (see p. 271)

 7. do loop vs while loop  (p. 242+, p. 227+)
    a. do loop
       The test condition is evaluated at the bottom of the loop.
       So, a do loop will always be executed at least once.
    b. while loop 
       The test condition is evaluated at the top of the loop.
       So, the loop will only be executed if the condition is true.

       Examples:
       do
       {
         // execute body of loop
         // increment counter
       } 
       while (counter < countermax)
       {
         // true
         // return to top of loop
       } else {
         // false
         exit the loop
       }

       while (counter < countermax)
       {
        evaluate condition
        { 
         // true
         // execute body of loop
         // increment counter
         // return to evaluate condition
        } else {
          // false
          exit the loop
        }
       }

 8. indentation
    This is a "programming style" discipline.
    It improves human readability of the source code.
    It has no effect on Java compilation.

III. Programming Exercises  
     Design and implement an application that plays the HI-LO guessing 
     game with numbers. The program should pick a random integer between
     1 and 100 (inclusive), then repeatedly prompt the user to guess
     the number. On each guess, report to the user that they are correct,
     or whether the guess was high or low. Continue accepting guesses
     until the user guesses correctly or chooses to quit. Use a sentinel
     value to determine if the user wants to quit. Count the number of
     guesses and report that value when the user guesses correctly.