Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Secret
- {
- // Instance variables
- private int solution;
- private int counter;
- // Constructor assign random: 10 - 20
- // initializes counter
- public Secret() {
- final int MAX = 20;
- final int MIN = 10;
- Random rand = new Random();
- solution = (int)(Math.random() * (MAX - MIN) + MIN);
- counter = 0;
- }
- // Guessing mutator
- public int guess(int myTry){
- if(myTry < solution){
- myTry = myTry;
- }
- else if(myTry > solution){
- myTry = myTry * -1;
- }
- else if(myTry == solution){
- myTry = myTry * 0;
- }
- counter++;
- return myTry + solution;
- }
- // Returns solution and guess counter
- public String toString(){
- return "The secret number is " + solution + "." +
- "\n" + "You have made " + counter + " number of attempts.";
- }
- public String tester(){
- return "number: " + solution + "\n" + "count: " + counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment