Drill4Solutions

Drill4Solutions - public class NumberGuessercfw_ private...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
public class NumberGuesser{ private int min; // minimum possible private int max; // maximum possible private int lastGuess; // my last guess private int numGuesses; // keep track of the total number of guesses public NumberGuesser() // constructor { min = 0; max = 100; lastGuess = 50; numGuesses = 1; } // return the last guess calculated public int getGuess() { return lastGuess; } // return the number of guesses total public int getNumGuesses() { return numGuesses; } /* calcNextGuess * depending on the user input, update the bounds and calculate next answer * input: answer. This is the user response to my last guess */ public void calcNextGuess(int answer) // each parameter must have its type { // if it was correct, then we are finished if (answer == 0) return; // reset our bounds based on too high or too low if (answer == 1) min = lastGuess; else max = lastGuess; numGuesses++; // record that we required one more guess // calculate our next guess lastGuess = (min + max) / 2; } // end of enterGrade
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

Drill4Solutions - public class NumberGuessercfw_ private...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online