CS305j, Introduction to Computing
Discussion section handout #2

1. What is the output of the following code segments? Predict then answer then
copy and past the code into the interactions panel in Dr. Java to run them and
see how the actual results compare to the predicted results.

Segment A:

for(int i = 1; i <= 5; i++){
    for(int j = 1; j <= 3; j++){
        System.out.print('*');
    }
}
System.out.println("!!");



Segment B: How does the lack of braces affect the code?

for(int i = 1; i <= 5; i++)
    for(int j = 1; j <= 3; j++)
        System.out.print('*');
System.out.println("!!");



Segment C:

for(int i = 0; i < 6; i++){
    for(int j = 0; j <= 3; j++){
        System.out.print( j * i );
    }
    System.out.println("!!!");
}



Segment D: How does the lack of braces affect the code?

for(int i = 0; i < 6; i++)
    for(int j = 0; j <= 3; j++)
        System.out.print( j * i );
        System.out.println("!!!");



Segment E:

for(int i = 1; i <= 5; i++){
    for(int j = 1; j <= 3; j++){
        for(int k = 1; k <= 4; k++){
            System.out.print('#');
        }
    }
}



Segment F:

for(int i = 1; i <= 4; i++){
    System.out.print('i');
    for(int j = 1; j <= i; j++){
        System.out.print(j);
        for(int k = 1; k <= 3; k++){
            System.out.print('x');
        }
        System.out.print('j');
    }
    System.out.println(i);
}


Segment G:

for(int m = 1; m <= 5; m++){
    for(int n = 1; n <= m; n++){
        for(int p = n; p <= 3; p++){
            System.out.print("$");
        }
        System.out.println();
        System.out.print('=');
    }
}



Segment H:

for(int m = 2; m <= 25; m += 5){
    for(int n = 1; n <= m; n = n * 2){
        System.out.println( m + " " + n );
    }
}



2. Why does the following code result in a syntax error? What would you do to
fix the syntax error?

for(int i = 1; i <= 10; i++){
    for(int j = 5; j <= 20; j *= 2){
        System.out.println( i + " " + j );
    }
}
System.out.println( i + " " + j );



3. Write a static Java method to produce the following figure. Use for loops to
produce the figure so its size can easily be changed.

<<<<<<<<<<>>>>>>>>>>
++<<<<<<<<>>>>>>>>++
++++<<<<<<>>>>>>++++
++++++<<<<>>>>++++++
++++++++<<>>++++++++
++++++++++++++++++++


4. What does each of the expressions evaluate to? Predict the answer and then
test your prediction using Dr. Java. When you predict an answer be sure to
indicate the literal type. (For example 7 for int, 7.0 for double, "7" for a
String. Realize Dr. Java will not output the double quotes for Strings, but you
need to be aware of what the results data type is.) Some of the expressions
contain syntax errors. If an expression is not syntactically correct your answer
should be syntax error.

20 / 7 * 2 + 5

5671 % 100 / 10

5 + 2 * 3 - 3

10 / 4.0 * 2

2.0 * 5 / 20

2.5 + 3.5 + 10 / 3

5.6 % 5

8987 % 10 % 100

1 + 2 + "result" + 2 + 3

"result" + 5 * 6

12.0 / 5 / 2

3 + 4 + value + 2 * 3

4 * (3 + 2)

5 * (1.5 + 1.5)

9 / 2.0 + 7 / 3 - 3.0 / 2

200 / 5 / 10 / 7

78 % (5 * 2) % 5

("number" + 3) * 2

"1" + "2" + "3" + "4"



5. Consider the following loop. What would expression would you put in the
println statement to produce the desired sequences? For example to get the
sequence 0, 5, 10, 15, 20, 25, 30 the expression ((i - 1) * 5) yields the
desired result.

for(int i = 1; i <= 7; i++){
    System.out.print( <expression> );
}


Sequence 1:   1, 2, 3, 4, 5, 6, 7

Sequence 2:   -10, -9, -8, -7, -6, -5, -4

Sequence 3:   4, 9, 16, 25, 36, 49, 64

Sequence 4:   100, 80, 60, 40, 20, 0, -20

Sequence 5:   -8 -4 0 4 8 12 16

Sequence 6:   -5 0 5 10 15 20 25

Sequence 7:   23 20 17 14 11 8 5

Sequence 8:   3 10 29 66 127 218 345