CSE1020 Lab 05: while loops

This lab has you write some simple while loops. You need to submit your work before Friday midnight.

You may need the Java API for this lab: http://www.cse.yorku.ca/common/java/api/index.html

The last half hour of this lab is a redo of labtest 1. It is optional; if you write both tests, your final mark is computed as the average of the two tests.

Create a lab directory

Use the techniques you learned from the previous labs to create a suitable directory for this lab and start jEdit. You might consider opening a terminal and entering the following commands:

cd Documents/cse1020
mkdir lab05
cd lab05
jedit &

 

while

Remember that the general pattern for a while-loop is:

while (condition)
{
   // body statements
}
// after while

The boolean condition is checked once the first time the loop is encountered, and every time after the body statements. If the condition is true, then the body statements are run; otherwise, control flows to the code after the while block.

Because the condition may be false when the loop is first encountered, a while-loop will run zero or more times.

 

Getting multiple inputs from the user

Write a program named SumLessThanM that reads an integer value n from the command line. Your program needs to compute the smallest integer m such that

1 + 2 + 3 + ... + m > n

An obvious approach is to use a while-loop, but you are free to use other approaches if you wish.

Here are some sample runs:

1 > 0
java SumLessThanM 0
1

28 = 1 + 2 + 3 + 4 + 5 + 6 + 7 > 25
java SumLessThanM 25
7

1035 = 1 + 2 + 3 + ... + 45 > 1000
java SumLessThanM 1000
45

 

Submit your program using the command:

submit 1020 L05 SumLessThanM.java

Getting multiple inputs from the user

Using a Scanner and a while-loop, you can continually get input from the user until the user indicates that they are finished by pressing Ctrl+D.

Start with the program shown below. Modify the program so that it computes the sum of the values input by the user.

import java.io.PrintStream;
import java.util.Scanner;

public class WhileInput
{
   public static void main(String[] args)
   {
      PrintStream out = System.out;
      Scanner in = new Scanner(System.in);

      out.print("Enter a real number: ");
      while (in.hasNext())
      {
         double value = Double.parseDouble(in.next());
         out.print("Enter a real number: ");
      }
   }
}

Here is an example run:

java WhileInput
Enter a real number: 1.5
Enter a real number: 2.5
Enter a real number: 3.5
Enter a real number: 4.5
Enter a real number: 5.5
Enter a real number: user presses Ctrl+D
Sum of values: 17.5

 

Submit your program using the command:

submit 1020 L05 WhileInput.java

The Price is Right

The Clock Game is a famous game on the TV game show The Price is Right. In the game, the contestant must guess the price of a prize. If the guess is lower than the price the host says "higher!" and if the guess is higher than the price the host says "lower!". The contestant continues guessing until she guesses the correct price, or time runs out.

Modify the following program so that it implements the Clock Game (ignoring the time aspect of the original game). Here, the price is always an integer between 1 and 100 dollars (inclusive).

import java.io.PrintStream;
import java.util.Random;
import java.util.Scanner;

public class ClockGame
{
   public static void main(String[] args)
   {
      PrintStream out = System.out;
      Scanner in = new Scanner(System.in);

      Random rng = new Random();
      final int HI = 100;
      final int PRICE = rng.nextInt(HI) + 1;
      int guess = 0; // use this to store the users guess
      out.print("Guess the price: ");
      while ()
      {

      }
   }
}

Here are two example runs:

java ClockGame
Guess the price: 50
Higher!
75
Higher!
88
Higher!
94
You won!

java ClockGame
Guess the price: 25
Higher!
75
Lower!
50
Lower!
30
Higher!
user presses Ctrl+D
You lost!

 

Submit your program using the command:

submit 1020 L05 ClockGame.java

All done!

You can leave, or wait for the re-test of Test 01.