Computer Science & Software Engineering |
CSCI 135 |
Part 1: while Loops
Write a program named GuessUntil.java that generates a random number between 1 and 10. To do this, recall that the command:
int number = (int) ((Math.random( )* 10) + 1);
will generate a random integer within that range.
The user can enter guesses at the number until they get it right. To do this, we need a way to get user input, not just command line input, into your program. We'll talk about this more later on, but for now, here is "magic" code that does that.
At the top of your program, before the public class <whatever> statement, either above or just below your header comment, put in this line:
import java.util.Scanner;
At the top of your main method, put in this line:
What this does is allow you to define a variable named keyboard that is of type Scanner. This is the Java class for getting user input. Now, whenever you need interactive input from the user, use the line:Scanner keyboard = new Scanner(System.in);
This line says to use the variable "keyboard" to get the next integer that the user types in at the keyboard. When you run your program, make sure that you click inside the console window before entering input. Also, the user should know what they are expected to type at the keyboard, so you should always print out a line (a prompt) that tell the user what is expected. For example:guess = keyboard.nextInt();
Would look to the user like:System.out.print("Enter a number between 1 and 10: "); guess = keyboard.nextInt();
Your program should use some form of a while loop to continue getting user guesses until they finally guess the number that was randomly generated. You can decide whether a while loop or a do...while loop is more appropriate. When the user finally guesses the correct number, print out:Enter a number between 1 and 10:
You got it!
Part 2: for Loops
Rewrite your previous program so that the user only gets three guesses. Name this program GuessThree.java. Use a for loop in this program. Each time the user guesses wrong, you should print out whether the guess was too high or too low. If they get it right you should print out "You got it!"
If the user guesses correctly before their three guesses are used up, you should exit the loop. IMPORTANT! Try to figure out a way NOT to use a break statement to terminate your loop early. You will lose a point if you use a break statement.
Here is an example output when the user guessed on the second try:
Enter a number between 1 and 10: 5
Your guess was too low.
Enter a number between 1 and 10: 7
You got it!
Part 3: Nested Loops
Write a program that asks the user to enter the size of a traingle (an integer from 1 to 50). Name this program Triangle.java. Display the triangle by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. On the next line,write one fewer asterisk and continue by decreasing the number of asterisks by 1 for each successive line until only one asterisk is displayed. (Hint: Use nested for loops; the outside loop controls the number of lines to write, and the inside loop(s) control(s) the number of asterisks to display on a line.)
For example, if the user enters 3, the output would be:
This is similar to the StarTriangle example that we saw in class, but has the added twist of printing asterisks up to and then down from the number the user has entered.* ** *** ** *
Grading Each of the three programs is worth 10 points each, for a total of 30 points. For each program, you will be graded according to the following criteria:
Grade Item | GuessUntil | GuessThree | Triangle | Points Earned |
---|---|---|---|---|
Program Compiles | 2 | 2 | 2 | |
Program Runs | 2 | 2 | 2 | |
Header Comment | 2 | 2 | 2 | |
Program Runs Correctly | 4 | 4 | 4 |
Page last updated: August 15, 2018