Commit f6a5d4e3 authored by zenab.ali's avatar zenab.ali

Final Solution: Implemented all required threads and tested Race Condition scenarios.

parents
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="23 (8)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/VaultHackingRace.iml" filepath="$PROJECT_DIR$/VaultHackingRace.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
public class AscendingHackerThread extends HackerThread {
public AscendingHackerThread(Vault vault) {
// Pass the hacker's name to the parent class
super(vault, "Ascending Hacker");
}
@Override
public void run() {
System.out.println(hackerName + " started searching from 0 upwards.");
// Search from 0 up to 9999
for (int guess = 0; guess <= 9999; guess++) {
if (vault.isCorrectPassword(guess)) {
// If successful, terminate the program via the success function in the parent class
success(guess);
}
}
// If the police win first, this part won't be executed due to System.exit(0)
}
}
\ No newline at end of file
public class DescendingHackerThread extends HackerThread {
public DescendingHackerThread(Vault vault) {
super(vault, "Descending Hacker");
}
@Override
public void run() {
System.out.println(hackerName + " started searching from 9999 downwards.");
// Search from 9999 down to 0
for (int guess = 9999; guess >= 0; guess--) {
if (vault.isCorrectPassword(guess)) {
// If successful, terminate the program
success(guess);
}
}
}
}
\ No newline at end of file
public abstract class HackerThread extends Thread {
// The vault the hacker is trying to break into
protected final Vault vault;
// The hacker's name
protected final String hackerName;
public HackerThread(Vault vault, String hackerName) {
this.vault = vault;
this.hackerName = hackerName;
// 3. Set Maximum Priority for hackers (Required)
this.setPriority(Thread.MAX_PRIORITY);
}
// Abstract function to force child classes to implement the guessing logic
@Override
public abstract void run();
// Shared function called when the hacker wins
protected void success(int correctPassword) {
// 4. Shared success behavior
System.out.println("\n-------------------------------------------------");
System.out.println(hackerName + " WON! Cracked the Vault.");
System.out.println("The password was: " + correctPassword);
System.out.println("-------------------------------------------------");
// 5. Terminate the entire program (Required)
System.exit(0);
}
}
\ No newline at end of file
public class Main {
public static void main(String[] args) {
// 1. Create the Vault with a random password
Vault vault = new Vault();
// 2. Create the three threads
AscendingHackerThread ascHacker = new AscendingHackerThread(vault);
DescendingHackerThread descHacker = new DescendingHackerThread(vault);
PoliceThread policeThread = new PoliceThread();
// 3. Start the three threads simultaneously (Required)
System.out.println("Starting the race!");
ascHacker.start();
descHacker.start();
policeThread.start();
// 4. The program will continue running until one of the threads calls System.exit(0)
}
}
\ No newline at end of file
public class PoliceThread extends Thread {
public PoliceThread() {
// 6. Setting a lower priority to give hackers (MAX_PRIORITY) a slight advantage.
this.setPriority(Thread.MIN_PRIORITY);
}
@Override
public void run() {
System.out.println("\nPolice are on their way: 10 seconds remaining.");
// Countdown from 10 to 0 (Required)
for (int countdown = 10; countdown >= 0; countdown--) {
try {
// 7. Print countdown every second
if (countdown > 0) {
System.out.println("Countdown: " + countdown + "...");
// Wait for one second
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// 8. Handle Interruption (if another thread tries to stop the police thread)
System.out.println("Police interrupted! But they keep going.");
}
}
// 9. When the countdown reaches 0 (Time is up)
System.out.println("\n*************************************************");
System.out.println("Game over for you hackers! The Police arrived.");
System.out.println("*************************************************");
// 10. Terminate the entire program (Required)
System.exit(0);
}
}
\ No newline at end of file
import java.util.Random;
public class Vault {
// 1. Random digital password between 0 and 9999
private final int password;
public Vault() {
// Generate a random password
this.password = new Random().nextInt(10000);
System.out.println("Vault is ready with a password between 0 and 9999.");
}
// Function to check the guess
public boolean isCorrectPassword(int guess) {
try {
// Simulate processing time (5 milliseconds) as required
Thread.sleep(5);
} catch (InterruptedException e) {
// Handle interruption if the thread is interrupted during sleep
// e.printStackTrace();
}
// 2. Check the guess against the actual password
return guess == password;
}
// Helper function to get the password (for debugging/verification)
public int getPassword() {
return password;
}
}
\ 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