CS99

Fundamental Programming Concepts
Summer 2001

Lab 6

Reading for this lab: 3.2 Savitch.  This lab is considerably longer than any lab you have seen so far.

 

Overview

The goals of this lab are to

  • Separate the characters in a String

  • Use a simple while loop to find the largest number in an input list of fixed size and for an input list of unknown size

  • Write a simple guessing game program, learn to translate pseudo-code

  • Use nested while loops to generate a series of triangular patterns on the screen

Part I: Separating characters in a String

Part A:

Write a program that inputs a five-digit number, separates the numbers into its individual digits and prints the digits separated from one another by three spaces each.  So if the user types in 42339, the program will print

4   2   3   3   9

To do this, read the input as a String (using readString() instead of readInt() with TokenReader) and use the String function charAt().  s.charAt( n ) returns the character at position n in String s; the first character has position 0.  Thus the following bit of code prints B on the screen:

String name = "Bob";

System.out.println( name.charAt( 0 ) );

Part B:

Modify the previous program so that it works for an input digit of any length.  You will have to use a while loop.  To get the bounds on the loop, use the String function length() which returns the number of characters in a String.  For example, if s = "Morpheus", s.length() returns 8.  Save the program in a file called Lab6Part1B.java and print it.
 

Part II: Finding the largest value

Part A:

Write a Java program that inputs a series of 10 positive numbers and determines and prints the largest of the numbers.  Your program should only use three int variables: counter, number, largest.  Save your program in a file called Lab6Part2A.java and print it.

Sample output for your program might be:

Enter in 10 numbers: 91 24 13 2 1001 16 12 99 0 48

The largest number entered was 1001.

Part B:

Modify the program in Part A so that it will read in any number of digits (not just 10).  The program should quit when the user enters in -1 or any negative number.  At that time, it should print the largest number it has seen so far.  Make sure your program prints an appropriate message when the user doesn't enter in any numbers at all (i.e., if the very first number the user enters is -1, that means there is no data and the program should say so).  Save your program in a file called Lab6Part2B.java and print it.
 

Part III: Hi-Lo guessing game, translating pseudo-code

In this part, you will write a program that chooses a random number between 1 and 100 (inclusive) and asks the user repeatedly to guess what that number is. For each guess, the program should tell the user if he is correct, too high, or too low.  The program continues to accept guesses until the user either guesses correctly or the user decides to quit by entering in 0.  If the user guesses right, the program should tell the user how many guesses it took and should ask the user if he wants to play again.

A sample output for your program might be:

Welcome to the Hi-Lo Guessing Game!

Enter a guess: 6
That guess was too low. 
Enter another guess: 50
That guess was too high.
Enter another guess: 37.
Correct!  That took 3 guesses. 
If you would like to quit enter 0, otherwise guess the new number: 0
Bye!  Thanks for playing.

Picking the random number

To get a random integer from 1 to 100 we can use the Math.random() function.   Math.random() returns a random double value from 0 to 1 (excluding 1, but including 0).  To get a random integer from 0 to MAX, we multiply Math.random() by (MAX+1) and cast the result to an int (can you see why we must use MAX+1 and not MAX?).  So the following snippet of code will assign a random integer from 0 to 5 to variable guess:

int guess = ( int ) ( 6*Math.random() );

Can you see how to modify this statement to get a number from 1 to 100?

 The algorithm

Here is the algorithm for the Hi-Lo guessing game.  It's written in pseudo-code, which is a combination of Java and English.  Your job is to convert this pseudo-code into a working Java program:

 /* Hi-Lo Guessing Game Algorithm */
public static void main( String[] args ) {
   <pick random number>
   <ask user to guess a number>
   <set count to its initial value>
   while ( <number guessed is not 0 > ) {
      <if guess is too high, print appropriate message and update count>
      <else if guess is too low, print appropriate message and update count>
      <else if guess is correct then: > {

              You write the code for this part!

   }
   <ask user to guess number>
   }
< print goodbye message >
} // end main

Save your finished code in a file called HiLo.java and print it.
 

Part IV: Printing stars, nested while-loops

Part A:

Write a new Text file called Triangles.java and type the following code exactly into it:

/**
*    CS99 Lab 6: Triangles
*    Author: your name here

*    NetID: your NetID here
*    Date: today's date
*/
class Triangles {
     public static void main( String[] args ) {
          TokenReader in = new TokenReader( System.in );

         
System.out.print("Enter a  number: " );
          int n = in.readInt();

         
int rows = 1;

         
while ( rows <= n ) {

System.out.println();            
int cols = 1;

              while ( cols <= rows ) {
                   System.out.print( "*");
                   cols++;
              }

               rows++;

          }
     }
}

Run the program several times, entering in different numbers.  Do you see the triangle pattern it produces?  For instance, if you enter in 4, you should see the following output

Enter in a number: 4

*
**
***
****


This is your first example of a nested-while loop: a while loop inside of another while loop.  In this program, the outer loop is responsible for making sure the correct number of rows are printed on the screen, while the inner loop is responsible for making sure each row prints the correct number of stars.  Study how the the inner loop works: in the subsequent parts you will be asked to modify it.  You are only allowed to use the following print statements in the parts below 

·         System.out.println();

·         System.out.print("*");

·         System.out.print(" ");

Part B:
Modify the program in part A so that instead of printing the triangle pattern it does now, it prints this one (for an input size of 4):

****
***
**
*

Save your code in a file called TrianglesPartB.java and print it.

Part C:
Modify the program in part A again so that it prints this pattern (for an input size of 4): 

****
 ***
  **
   *

Hint: This pattern may require you to print an appropriate number of spaces before the asterisks.

Save your code in a file called TrianglesPartC.java and print it.

Part D:

Finally, modify the program in part A so that it prints this last pattern for an input size of 4:

   *
  **
 ***
****

Save your code in a file called TrianglesPartD.java and print it.

Part V: Submit Your Lab

Hand in printouts for

·         Lab6Part1B.java

·         Lab6Part2A.java, Lab6Part2B.java

·         HiLo.java

·         TrianglesPartB.java, TrianglesPartC.java, TrianglesPartD.java

As usual, don't forget to put your name, net ID, and date at the top of each file.

This lab is due Tuesday, 17 July 2001 at the beginning of the lab session.  Hand it in to Siddharth.