Commit 1f966df3 authored by jafar.mahmood's avatar jafar.mahmood

Implement all the answers and test them.

parent 3a02a0ad
No preview for this file type
File added
public class AscendingHackerThread extends Hacker {
public AscendingHackerThread(Vault vault) {
super(vault);
this.setName("AscendingHackerThread");
this.setPriority(Thread.MAX_PRIORITY);
}
@Override
public void hack() {
for (int guess = 0; guess <= 9999; guess++) {
if (vault.isCorrectPassword(guess)) {
System.out.println(this.getName() + " guessed the password " + guess);
System.exit(0);
}
}
}
}
public class DescendingHackerThread extends Hacker {
public DescendingHackerThread(Vault vault) {
super(vault);
this.setName("DescendingHackerThread");
this.setPriority(Thread.MAX_PRIORITY);
}
@Override
public void hack() {
for (int guess = 9999; guess >= 0; guess--) {
if (vault.isCorrectPassword(guess)) {
System.out.println(this.getName() + " guessed the password " + guess);
System.exit(0);
}
}
}
}
public abstract class Hacker extends Thread {
protected final Vault vault;
public Hacker(Vault vault) {
this.vault = vault;
}
public abstract void hack();
@Override
public void run() { hack(); }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("Starting threads from Main...");
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("All threads finished.");
public static void main(String[] args) {
Vault vault = new Vault();
Thread ascendingHacker = new AscendingHackerThread(vault);
Thread descendingHacker = new DescendingHackerThread(vault);
Thread police = new PoliceThread(10);
ascendingHacker.start();
descendingHacker.start();
police.start();
}
}
public class PoliceThread extends Thread {
private final int countdownSeconds;
public PoliceThread(int countdownSeconds) {
this.countdownSeconds = countdownSeconds;
this.setName("PoliceThread");
}
public void run() {
try {
for (int i = countdownSeconds; i > 0; i--) {
System.out.println("Police will arrive in " + i + " seconds");
Thread.sleep(1000);
}
System.out.println("Police have arrived! Game over for hackers.");
System.exit(0);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
\ No newline at end of file
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Thread running: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
}
}
}
\ No newline at end of file
import java.util.concurrent.ThreadLocalRandom;
public class Vault {
private final int secretCode;
public Vault() {
this(0, 9999);
}
public Vault(int min, int max) {
if (min > max) throw new IllegalArgumentException("min > max");
this.secretCode = randBetween(min, max);
System.out.println("Vault password is set to " + this.secretCode);
}
public boolean isCorrectPassword(int guess) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return this.secretCode == guess;
}
private static int randBetween(int min, int max) {
if (min > max) throw new IllegalArgumentException("min > max");
if (max == Integer.MAX_VALUE) {
return (int) ThreadLocalRandom.current().nextLong(min, (long) max + 1L);
}
return ThreadLocalRandom.current().nextInt(min, max + 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