-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuesser.java
More file actions
101 lines (90 loc) · 2.92 KB
/
Guesser.java
File metadata and controls
101 lines (90 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.Console;
import java.util.Scanner;
/**
* Assignment 1 - Guessing Game
*
* Here's the unfinished source code for the Guesser class. It is your task to
* complete the missing parts.
*/
public class Guesser {
private int low;
private int high;
/*
* Task 1. Write code here for a constructor which takes two int arguments and
* saves them in the instance variables low and high.
*
* If you don't remember the syntax for how to write constructors, revisit the
* chapter "Classes" and review the section about constructors.
*
*/
// Write the constructor below this line.
public Guesser(int low, int high) {
this.low = low;
this.high = high;
}
/*
* Task 2. Complete the start() method, so that in the method body, you call
* first the rules() method, next the doGuesses() method.
*/
public void start() {
rules();
doGuesses();
// call the rules method here
// call the doGuesses() method here
}
private void rules() {
System.out.println("Think of a number between " + low + " and " + high);
System.out.println("I'm going to ask a few questions in order " + "to guess the number.");
System.out.println("Please answer T for true, and F for false.\n");
}
/*
* Task 3. Complete the code for the getReply() method. In the current version
* below, it returns null each call, which is not what this method is supposed
* to do.
*
* Instead, change the method so that it reads a reply from the player, and if
* it is "T" or "F", we have a valid reply. Return the String that you read from
* the player.
*/
private String getReply() {
Scanner scan = new Scanner (System.in);
String reply = scan.nextLine();
// Write code here which reads a String from the console.
// As long as it is not a valid reply (one of "T" and "F")
// write an error message, and read a new reply.
// When you have gotten a valid reply, return it.
while (!reply.equals("T") && !reply.equals("F")) {
System.out.println("Error!");
System.out.println("Please Answer T or F");
reply = scan.nextLine();
}
return reply;
}
private void doGuesses() {
int i = 0; // number of guesses
int middle = 0;
while (low < high) {
// Set next guess to the middle between
// current low and current high
middle = low + (high - low) / 2;
System.out.println("Is the number less than or equal to " + middle + "?");
String reply = getReply();
if ("T".equals(reply)) {
// The number is less than or equal to middle
// so we move down high to middle
high = middle;
} else {
// The number is greater than middle,
// so we move up low to middle + 1
low = middle + 1;
}
i++; // One more guess!
}
// When low has met high, we don't enter the loop
// and we have found the number
answer(low, i);
}
private void answer(int guess, int numberOfGuesses) {
System.out.println("You were thinking about " + guess + " (took me " + numberOfGuesses + " guesses)");
}
}