S-111
  • Home
  • Lectures
  • Problem Sets
  • Sections
  • Syllabus
  • Schedule
  • Staff
  • Resources
  • Canvas
  • Ed Discussion
  • Gradescope

Section 3

Agenda

  • Take Problem Set 2 questions
  • Review exercises
  • Java exercises
  • Agenda
  • Getting started
  • Bug of the Day
  • Review exercises
  • Java exercises

Getting started

  1. Create a folder named section3 for your work on these exercises. You can find instructions for doing so here.

  2. 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.

Bug of the Day

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.

Review exercises

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!");
}
  1. What are the components of a for loop? 1
  2. In what order are each of these components evaluated or executed? 2
  3. Using 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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
  4. 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.

  1. Trace the values of the variables using a table, and determine the output of the program. 5
    int n = 64;
    for (int i = 1; i <= n; i++) {
        System.out.println(n);
        n = n / 2;
    }
    

Java exercises

  1. 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
    
  2. 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
    
  3. 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
    ... ... ... ... ...

  4. 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);
            }
        }
    }
    
  5. 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.

    1. Identify the important relationships between characters within the line and the line number. Assume that the line number begins with 1.
    2. Derive the formula for the number of spaces as a function of the line number.
    3. Derive the formula for asterisks as a function of the line number.

    After you obtain the formulae, use them to write a program which outputs the diamond.

    • The outer loop should control the number of lines, and it should initialize a variable named line.
    • The nested loops should be simple repetition loops, and they should use the formulae you derived.
    • If you find it necessary, you may use more than one outer loop.
  6. 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:

          *
         ***
        *****
       *******
      *********
     ***********
    *************
     ***********
      *********
       *******
        *****
         ***
          *
    
    1. 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).

    2. 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.

    3. Add a class constant called SCALE_FACTOR.
    4. Test the new version for different values of the constant.

  1. 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. ↩

  2. 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. ↩

  3. 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();
    

    ↩

  4. 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. ↩

  5. 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

    ↩

  6. Ascending.java ↩

  7. position 1: a, i; position 2: a, i, b, j; position 3: a, i, b; position 4: a, i, b, d; position 5: a ↩

  8. 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

    ↩

  9. 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

    ↩

  10. Diamond.java ↩

  11. Diamond2.java ↩

Last updated on June 27, 2025.