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
|
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 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? 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: You write the code for this part!
} 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: /** System.out.println(); while (
cols <= rows ) { rows++; }
·
System.out.println(); ·
System.out.print("*"); ·
System.out.print(" "); Part B: **** Save your code in a file called TrianglesPartB.java and print it. Part C: **** 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: |
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. |