Guest User

class1

a guest
Jun 23rd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. import java.util.*;
  2. public class Secret
  3. {
  4.     // Instance variables
  5.     private int solution;
  6.     private int counter;
  7.    
  8.     // Constructor assign random: 10 - 20
  9.     // initializes counter
  10.     public Secret() {
  11.         final int MAX = 20;
  12.         final int MIN = 10;
  13.         Random rand = new Random();    
  14.         solution = (int)(Math.random() * (MAX - MIN) + MIN);
  15.         counter = 0;
  16.     }
  17.     // Guessing mutator
  18.     public int guess(int myTry){
  19.         if(myTry < solution){
  20.             myTry = myTry;
  21.         }
  22.         else if(myTry > solution){
  23.             myTry = myTry * -1;
  24.         }
  25.         else if(myTry == solution){
  26.             myTry = myTry * 0;
  27.         }
  28.         counter++;
  29.         return myTry + solution;
  30.     }
  31.     // Returns solution and guess counter
  32.     public String toString(){
  33.         return "The secret number is " + solution + "." +
  34.             "\n" + "You have made " + counter + " number of attempts.";
  35.     }
  36.     public String tester(){
  37.         return "number: " + solution + "\n" + "count: " + counter;
  38.     }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment