CS100A Fall 1998 Assignment 4 Due at beginning of lecture on Thursday, 08 October

Do this assignment alone. Of course, you may discuss the assignment with others, but don't copy someone's work, either electronically or by hand, and don't hand in work that is not yours.

The goal of this assignment. Give you practice with loops. It does not require use of the computer --the work you hand in can be done with pencil and paper.

Use the computer to check your work. Though you don't have to, we suggest checking your work on the computer. If an exercise calls for writing a loop, check it out in Codewarrior. Start a Codewarrior project using stationery CUCSGraphicsApplication. If the loop you are writing draws in the drawing window, then replace the body of method paint with your code. If the loop just manipulates some variables and writes using System.out.println, then place your code at the beginning of method main. Also, use the debugger if you think it will help. Single-step through your code, watching the values of variables as they change.

String variables. A variable x (say) of class String contains (a reference to) a sequence of characters. The number of characters in the String is x.length() --it may be 0. Method x.charAt is used to refer to a character of x:

    the first character is x.charAt(0),
    the second character is x.charAt(1),
    ...,
    the last character is x.charAt(x.length()-1).

Often, when discussing parts of a String variable x, we use the notation x[i..j] to denote the subsequence of characters beginning with character x.charAt(i) and ending with x.charAt(j). The notation x[i..j] is NOT Java notation. If i=j+1, then x[i..j] denotes the empy sequence of characters, the sequence containing 0 characters. For example, if x contains "abcdefg", then x[1..3] is "bcd".

The assignment.
1. Truthifying a relation. A relationis a true-false statement. Examples are: b<a,  b<=a, and "All the factors of n that are less than k have been printed". The invariant of a loop is a relation.

Below are some relations. To the right of each is an assignment followed by ?. Replace ? by a statement so that the assignment followed by the statement truthifies the relation.

    (a) s is the sum of the first i positive integers.                                      i= 1; ?
    (b) s is the sum of the first i positive integers.                                     i= 0;  ?
    (c) s is the square of i.                                                                                i= 1; ?
    (d) s is the number of underline characters "_" in  x[0..i].                    i= -1; ?
    (e) s is the number of underline characters "_" in x[i..x.length( )-1]      i= x.length( ); ?
 
2. Maintaining a relation. Each of the five parts of question 1 contains a relation. Suppose the relation is true. Find a statement S such that execution of the following maintains the relation (i.e. keeps it true).

    (a) i= i+1; S          (b) i= i+1; S           (c) i= i+1; S         (d) i= i+1; S          (e) i= i-1; S
 
3. Determining when a desired result has been achieved.  For each of the relations R given in part (a)-(e) of exercise 1, write down a condition (another relation) C such that R and C together imply that the following result is true:

    (a) s is the sum of the first 100 positive integers.
    (b) s is the sum of the first 200 positive integers.
    (c) s is the square of 25.
    (d) s is the number of underline characters in x  (i.e. in x[0..x.length-1]).
    (e) s is the number of underline characters in x  (i.e. in x[0..x.length-1]).
 
4. Write a loop (with initialization) that draws 10 rectangles, all of width 50 and height 50. Their upper-left corners should be at positions (5,5), (8,8), (11,11), ..., (32, 32). The invariant of your loop should be:

     i rectangles have been drawn in Graphics g, and the next one to draw goes at position (x, x).

5. Write a loop (with initialization) that draws 8 circles. The center of all the circles should be (75,100). The radius of the circles should be 5,10,15,20,25,30,35,40. The invariant of your loop should be:

    The next circle to draw in Graphhics g has radius r, and those with smaller radii have been drawn.

6. Write a loop (with initialization) that draws 10 circles, each of radius 5, with centers (5,20), (15,20), (25,20), ..., (105,20). You will need only one variable i (say). The loop invariant should be

    The first i circles have been drawn, and the next one to draw has center (5+10*i, 20) .

7. Not to be graded. Hand this one in if you do it, so we see how many people do it. It will NOT be graded. But it's neat! Write a loop (with initialization) that draws a square-looking spiral of 54 lines. Here are the rules for drawing the lines.

Each iteration of your loop will draw one line. Your loop should use the following invariant:

        k lines have been drawn. The next line to draw begins at position (x,y).

Thus, you need three variables, k,x,y. In the body of the loop, declare extra two variables x1, y1 (say) that are local to the body, to contain the end point of the line to draw. At the end of the body, after the statement to draw the line, you can simply copy x1 to x and y1 to y.

8. Print the alphabet. A variable of type char may contain any character. For example, char c= 'A'; assigns the character 'A' to c. Further, the assignment c++; can be used to set c to the next character in the sequence (e.g. 'B' follows 'A'). Read page 16 of Holmes for more information on type char. Write a loop (with initialization) that prints out the uppercase characters A-Z, all on one line (use System.out.print instead of System.out.println).  The loop invariant should be:

    All the upper-case characters that precede c have been printed, and c is the next one to print.

9. Print the alphabet. Write a loop with initialization that prints the alphabet in the form aAbBcC... on a single line. The loop invariant should be:

    cl contains a lower-case character or '{', and cu contains the corresponding upper-case character or '['.
    The part of the desired output that precedes cl has been printed.