You need to sign in or sign up before continuing.
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 class Main {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) {
System.out.println("Starting threads from Main..."); Vault vault = new Vault();
Thread ascendingHacker = new AscendingHackerThread(vault);
MyThread t1 = new MyThread(); Thread descendingHacker = new DescendingHackerThread(vault);
MyThread t2 = new MyThread(); Thread police = new PoliceThread(10);
t1.start(); ascendingHacker.start();
t2.start(); descendingHacker.start();
police.start();
t1.join();
t2.join();
System.out.println("All threads finished.");
} }
} }
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