Create a folder named section3
for your work on these
exercises. You can find instructions for doing so
here.
In VS Code, select the File->Open Folder or File->Open menu
option, and use the resulting dialog box to find and open your
section1
folder. The name of the folder should appear in the
Explorer pane on the left-hand side of the VS Code window.
Important: In general, you should not use File->Open File to open a specific file in VS Code. Rather, you should create a folder for the files that belong to a given assignment or section, and you should use File->Open Folder or File->Open to open the folder.
Consider the following Java code fragment, which is supposed to produce
a sum of the integers between 1 and n
, where n
is defined to be 10:
int n = 10; int sum = 0; for (int i = 1; sum <= n; i++) { sum = sum + i; }
This code produces a sum
value of 15, which is incorrect (it should be 55).
Identify the logic error in this code and suggest a fix.
Recall from lecture the definition and behavior of the definite (i.e., for
)
loop, then answer the following questions. A sample for
loop is shown
below for reference.
for (int i = 0; i < 6; i++) { System.out.println("Hip hip!"); }
for
loop? 1Using the simple loop template from lecture, write a
System.out.print(...)
statement and for
loop containing a
System.out.print(...)
statement that represents how excited
you are to be in this class: 3
S-111!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Describe how, with for
loops, the scoping rules for variables in the
initialization is different. 4
Using a piece of paper and a pencil or pen, construct a table containing space for the variables in the following code fragment.
int n = 64; for (int i = 1; i <= n; i++) { System.out.println(n); n = n / 2; }
Write a complete program in a file named Ascending.java
that produces
the following sequence of numbers using a for
loop. 6
5 11 17 23 29 35
For each position indicated by the line comments, list the variables
that are in scope. Recall that for
loops are an exception of the normal
scoping rules. 7
int a = 7; for (int i = 1; i <= 6; i++) { // position #1 int b; for (int j = i; j <= 3; j++) { // position #2 int c = j * 3; } // position #3 int d; if (a == 3) { d = 10; int e = 0; } else { // position #4 d = a; } } // position #5
Consider the following code segment:
int a = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < i; j++) { a += i + j; } }
Determine the value of the variable a
after this code has been executed.
Use a piece of paper and a pencil or pen to trace each step of the
procedure using a table like the one below. 8
i | i < 3 | j | j < i | a |
---|---|---|---|---|
0 | true | 0 | false | 0 |
... | ... | ... | ... | ... |
Trace the values of the variables using a table, and determine the output of the program. 9
for (int i = 0; i < 6; i++) { for (int j = i; j < 3; j++) { for (int k = j; k < 2; k++) { System.out.println(i + " " + j + " " + k); } } }
Write a complete program in Diamond.java
that prints the following
pattern using nested for
loops. 10
* *** ***** ******* ********* ******* ***** *** *
Note that the pattern is composed of zero or more spaces followed by one or more asterisks. It is also useful to consider that the diamond is comprised of two triangles: an up-facing triangle and a down-facing triangle.
After you obtain the formulae, use them to write a program which outputs the diamond.
line
.Modify your Diamond.java
file so that it uses a class constant to
define the size of the diamond. 11
Diamond with a scale factor of 2:
* *** ***** ******* ********* *********** ************* *********** ********* ******* ***** *** *
Derive a second set of formulae for one triangle component of the diamond, for a different height of the triangle (e.g., a height of 7 instead of 5).
Compare the formulae for the original version with the formulae for the new version. Determine how the numbers in the formulae depend on the height of the triangle.
SCALE_FACTOR
.A for
loop consists of the initialization, the continuation test,
and the update in the header. The body of the for
loop contains
the code that may be repeated. ↩
The initialization is always done first. Then, the test, body and
update are done continuously, until the test evaluates to a false
Boolean value. Then the flow of control is released to the line after
the for
loop. ↩
The following code fragment represents how excited I am to be your teaching assistant for this class:
System.out.print("S-111"); for (int i = 0; i < 300; i++) { System.out.print("!"); } System.out.println();
Though a variable (e.g., int i
) declared in the header of the loop
is not contained within the curly braces, it will go out of scope once
the test fails and we leave the body of the loop. ↩
The tracing table can be found below:
n |
i |
i <= n |
output |
---|---|---|---|
64 | 1 | true | 64 |
32 | 2 | true | 32 |
16 | 3 | true | 16 |
8 | 4 | true | 8 |
4 | 5 | false |
position 1: a
, i
; position 2: a
, i
, b
, j
; position 3: a
,
i
, b
; position 4: a
, i
, b
, d
; position 5: a
↩
The tracing table can be found below:
i |
i < 3 |
j |
j < i |
a |
---|---|---|---|---|
0 | true | 0 | false | 0 |
1 | true | 0 | true | 1 |
1 | false | 1 | ||
2 | true | 0 | true | 3 |
1 | true | 6 | ||
2 | false | 6 | ||
3 | false |
The tracing table can be found below:
i |
i < 6 |
j |
j < 3 |
k |
k < 2 |
output |
---|---|---|---|---|---|---|
0 | true | 0 | true | 0 | true | 0 0 0 |
1 | true | 0 0 1 |
||||
2 | false | |||||
1 | true | 1 | true | 0 1 1 |
||
2 | false | |||||
2 | true | 2 | false | |||
3 | false | |||||
1 | true | 1 | true | 1 | true | 1 1 1 |
2 | false | |||||
2 | true | 2 | false | |||
3 | false | |||||
2 | true | 2 | true | 2 | false | |
3 | false | |||||
3 | true | 3 | false | |||
4 | true | 4 | false | |||
5 | true | 5 | false | |||
6 | false |
Last updated on June 27, 2025.