Lab 3 Assignment:


You will be provide with a lab template that has all the problems 
represented by a single function that you must code to give the correct 
outputs based on inputs.

For instance problem R4.1a will have an entry in the lab template that 
looks like this:

    //Problem R4.1a
    public static String r41A( int n) {
        //START CODE
        return "";
        //STOP CODE
    }

You will be expect to only modify the code between the two START and STOP 
code comments. We give you an easy example to see how to very easily build 
up a String with a correct answer. (For those curious, a better way to do 
it is to use a StringBuilder, but that is more complex than we care to 
explain at this time)



R4.1,

Write code using a while loop that returns a String based on an input int 
named n:

    a) all squares less than n. 
      Example: n=30 -> "1 4 9 16 25"
    b) all positive numbers that are divisible by 10 and less than n
      Example: n=30 -> "10 20"
    c) all powers of two less than n
      Example: n=30 -> "1 2 4 8 16"

R4.2, 

Write code using a a loop that computes and returns the value as in int.

    a) the sum of all even numbers between 2 and 100 inclusive
    b) the sum of all square between 1 and 100 inclusive
    c) the sum of all odd nubmers between a and b inclusive
    d) the sum of all odd digits of n (e.g. n = 32677, sum = 3 + 7 + 7 = 17)

R4.3, 

Provide trace tables for these loops. To do this in code, return a String 
that contains the trace table. This should consist of the values of each 
variable for each iteration on a line, with the order of the variables and 
values like this:

iteration1: i=0, j=1, n=2
iteration2: i=1, j=1, n=2
iteration3: i=2, j=1, n=2
iteration4: i=3, j=1, n=2

Where you replace 0/1/2 with the actual values of the variables for a 
given iteration.

    a) int i = 0; int j = 10; int n = 0;
    while (i < j) { 
        i++; 
        j--; 
        n++; 
    }
    b) int i = 0; int j = 0; int n = 0;
    while (i < 10) { 
        i++; 
        n = n + i; 
        j++; 
    }
    c) int i = 10; int j = 0; int n = 0;
    while (i > 0) { 
        i--; 
        j++; 
        n = n + i - j;
    }
    d) int i = 0; int j = 10; int n = 0;
    while (i != j) { 
        i = i + 2; 
        j = j - 2;
        n++;
    }


Clue: You can add a line break in java by appending "\n" to the String. We 
give a brief example of this in the template code.

        
R4.4 (page 184)

What do these loops print? You simply need to return a String containing 
exactly what this code would output inside the lab template.(You can 
calculate this or just give a direct answer, however, incorrect direct 
answers will not receive partial credit)

a) 
for (int i = 1; i < 10; i++) {
    System.out.print(i + " "); 
}
b) 
for (int i = 1; i < 10; i+=2) {
    System.out.print(i + " "); 
}
c) 
for (int i = 10; i > 1; i--) {
    System.out.print(i + " "); 
}
d) 
for (int i = 0; i < 10; i++) {
    System.out.print(i + " "); 
}
e) 
for (int i = 1; i < 10; i = i * 2) {
    System.out.print(i + " "); 
}
f) 
for (int i = 1; i < 10; i++) {
    if (i % 2 == 0) {
        System.out.print(i + " "); 
    }
}

R4.10 (page 185) 

How many iterations do the following loops carry out? Assume that i is not 
changed in the loop body. Return an int with the exact number of 
iterations for each part. (You can calculate this or just give a direct 
answer, however, incorrect direct answers will not receive partial credit)

a) for (int i =  1;  i <= 10; i++)
b) for (int i =  0;  i <  10; i++)
c) for (int i =  10; i >  10; i--)
d) for (int i = -10; i <= 10; i++)
e) for (int i =  10; i >= 10; i++)
f) for (int i = -10; i <= 10; i=i+2)
g) for (int i = -10; i <= 10; i=i+3)


R4.15 (page 186)

Rewrite the follow for loop into a while loop. Put the new code inside the 
appropriate section of the lab template.

int s =0;
for (int i=1; i <=10; i++)
{
    s = s + 1;
}


R4.16 (page 186)

Rewrite the following do loop into a while loop. You may assume n is an 
int provided as an argument to the function where you are writing this 
code. Put the new code inside the appropriate section of the lab template.

double x = 0;
double s;
do
{
    s = 1.0 / (1 + n * n);
    n++;
    x = x + s;
}
while ( s > 0.01);