Commit c18cdb3c authored by rawan's avatar rawan

step6

parent eb08b7f8
package hacker;
public class BinarySearchHacker extends Thread {
private Vault vault;
public BinarySearchHacker(Vault vault) {
this.vault = vault;
this.setName("BinarySearchHacker");
}
public void run() {
int min = 0;
int max = 9999;
while (min <= max) {
// Calculate the middle guess
int y = (min + max) / 2;
// verify
int result = vault.checkGuess(y);
if (result == 0) {
System.out.println(" The clever hacker succeeded " + this.getName() + " The password is: " + y);
System.exit(0);
} else if (result == -1) {
// The guess was too low ,Throw out the bottom half
min = y + 1;
} else { // result == 1
// The guess was too high, throw out the top half
max = y - 1;
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment