Discussion Section 3: Indefinite Loops and
Simple Methods
Due: 10pm Tuesday February 6
Purpose: In this
discussion section, you will
practice how to use indefinite loops using while and also use
simple methods.
Please pick a number from 1 to 10.Your program should terminate after trying all 10 numbers from 1 to 10, and also check if the user input is Y or N. If the user input is neither Y nor N, then your program should ask again for input. It is okay to simply repeat the current guess as shown above, but feel free to use your imagination for error message.
My guess is 1. Is this what you have in mind(Y/N)? N
My guess is 2. Is this what you have in mind(Y/N)? M
My guess is 2. Is this what you have in mind(Y/N)? N
My guess is 3. Is this what you have in mind(Y/N)? Y
Thanks for playing!
Your program should include a method nextGuess. The
signature
is as follows:
public int nextGuess(int currentGuess)
Implement this method to return (currentGuess+1), as long as the currentGuess is less than 10. (We will change this to something more interesting later.) Your main program should start from 1, and call this method to get the next guess.
Here is a template for Guess.java.
import java.util.*Once you are done with implementing Guess.java as described above, try to start from a random number between 1 and 10, using Random class in java. The following code returns a random number between 0 and n.
/**
* @author name 1: discussion section time:
* @CS account user name:
*
* @author name 2: discussion section time:
* @CS account user name:
*
* @version Date
*
*/
public class Guess {
public Guess() {
}
public int nextGuess(int currentGuess) {
// your code here
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
Guess g = new Guess();
System.out.println("Please pick a number from 1 to 10.");
// your code here
}
}
Random r = new Random();Make sure that your program doesn't start from 0, and change the main method so that it will try every number between 1 and 10, even after starting from a random number. A sample scenario is as follows:
int randomNumber = r.nextInt(int n);
Please pick a number from 1 to 10.
My guess is 9. Is this what you have in mind(Y/N)? N
My guess is 10. Is this what you have in mind(Y/N)? N
My guess is 1. Is this what you have in mind(Y/N)? N
My guess is 2. Is this what you have in mind(Y/N)? N
My guess is 3. Is this what you have in mind(Y/N)? Y
Thanks for playing!
Your program should be internally correct (sound logic) and
externally correct (following java style guideline).