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

Section 7

  • Bugs of the Day
  • Java exercises
    • Indefinite loops
    • Case study: a number guessing game

Bugs of the Day

This program is supposed to compute the sum of the first 5 squares, but it has multiple issues.

int n = 0;
int i = 0;
while (true); {
    n = i * i;
    i++;

    if (i == 5) {
        break;
    }
}

System.out.println(n);

Identify the bugs and suggest a fix.

Java exercises

Indefinite loops

Recall that we can use indefinite loops (while or do { ... } while loops) when the number of iterations that we need to make is not obvious or impossible to determine before the loop begins. Often this is used in a program that takes input from the keyboard, file or the network, when the data being recieved by the program could be of any length.

  1. Let’s trace through an example of a while loop.

    int a = 4;
    int b = 12;
    
    while (b > a && a > 0) {
        System.out.println(b);
        b = b - a;
        a--;
    }
    

    Using a piece of paper or the chalkboard, create a table that resembles the following to trace the while loop above. 1

    a b b > a a > 0 b > a && a > 0
    4 12 ... ... ...
    ... ... ... ... ...

  2. Convert the following for loop into an equivalent while loop. 2

    for (int i = 10; i < 24; i += 2) {
        System.out.println("i = " + i);
    }
    
  3. Convert the following while loop into an equivalent do { ... } while loop. 3

    int num = 5;
    System.out.println(num);
    num++;
    
    while(num > 5 && num < 20){
        System.out.println(num);
        num++;
    }
    

Case study: a number guessing game

  1. To exercise your skills in procedural decomposition, conditional execution, indefinite loops and the Scanner class, we will construct a number guessing game. Write your class in a file named NumberGame.java. The program should have the following properties:

    • When the game begins, the program should ask the player for their preferred level of difficulty (a number between 1 and 3, where 1 is the easiest difficulty and 3 is the most difficult).

      • If the player selects the easy difficulty (1), the game will select a secret number between 1 and 10, inclusive.
      • If the player selects the normal difficulty (2), the game will select a secret number between 1 and 50, inclusive.
      • If the player selects the hard difficulty (3), the game will select a secret number between 1 and 500, inclusive.
    • Then the program should pick a random number in the range specified by the level of difficulty, and begin allowing the player to make guesses.

      • If a player makes a guess that is greater than the secret number, the game will print Your guess is too high!.
      • If a player makes a guess that is less than the secret number, the game will print Your guess is too low!.
      • If a player guesses the right number, the game is over.
    • The number of guesses that are given to the player is determined by the level of difficulty.

      • If the player selected the easy difficulty, the player should be given four guesses.
      • If the player selected the normal difficulty, the player should be given five guesses.
      • If the player selected the hard difficulty, the player should be given eight guesses.
    • If the player does not guess the secret number before the number of guesses has been exhausted, the program exits immediately with the message Game over! The number was <x>!, where <x> is the secret number.

    • If the player guesses the secret number before the number of guesses has been exhausted, the program should exit after printing You win! Good guessing!.

    • If the player gets the number right on their first guess, the program should print Wow! Are you cheating?, before printing the You win! message.

    Here are some additional hints.

    • In order to generate a random number, the static random() method from the Math class can be used. (Recall that the Math class is always available to you.) Math.random() will return a double between 0.0 and 1.0. You will need to write an expression that converts this floating-point number into an integer in the correct range as dictated by the level of difficulty of the game.

    You should copy this template code into a Java file and start by writing an unstructured solution, with the logic in the main() method. Then, test your program until you are convinced it works, and break each piece into methods and combine repeated blocks in conditionals, as we did yesterday with the buffet exercise. 4


  1. The code produces the following output:

    12
    8
    5
    3
    

    The trace can be found below.

    a b b > a a > 0 b > a && a > 0
    4 12 true true true
    3 8 true true true
    2 5 true true true
    1 3 true true true
    0 2 true false false
     ↩

  2. Here’s an equivalent while loop.

    int i = 10;
    while (i < 24) {
      System.out.println("i = " + i);
       i += 2;
    }
    

    ↩

  3. Here’s an equivalent do { ... } while loop.

    int num = 5;
    
    do {
        System.out.println(num);
        num++;
    } while (num > 5 && num < 20);
    

    ↩

  4. Here’s one possible solution: NumberGame2.java ↩

Last updated on July 5, 2025.