Commit ba0be9d4 authored by drnull03's avatar drnull03

Wrote the Code of the homework (without report)

parents
1.Added maven plugin for running java jar files
2. all the java code run in the same package no import statement
3.this code ran on jdk 21.0.8
4.it ran on linux ubuntu 24.04
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pc.lab1</groupId>
<artifactId>VaultHackingRace</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>VaultHackingRace</name>
<url>http://maven.apache.org</url>
<!-- Use Java 17 -->
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- keep junit for tests if you want -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Compiler plugin (optional since properties above are enough, but explicit is OK) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<!-- exec plugin to run com.pc.lab1.App with mvn exec:java -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>com.pc.lab1.App</mainClass>
</configuration>
</plugin>
<!-- jar plugin: adds Main-Class to manifest so you can build a runnable jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<mainClass>com.pc.lab1.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- (Optional) shade plugin if you later add external dependencies and want a fat-jar -->
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.pc.lab1.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
</project>
package com.pc.lab1;
public class App {
public static void main(String[] args) {
System.out.println("Starting Vault Hacking Race...");
Vault vault = new Vault();
AscendingHackerThread asc = new AscendingHackerThread(vault);
DescendingHackerThread desc = new DescendingHackerThread(vault);
PoliceThread police = new PoliceThread();
asc.setPriority(Thread.MAX_PRIORITY);
desc.setPriority(Thread.MAX_PRIORITY);
police.setPriority(Thread.NORM_PRIORITY);
asc.start();
desc.start();
police.start();
}
}
package com.pc.lab1;
public class AscendingHackerThread extends Thread {
private final Vault vault;
public AscendingHackerThread(Vault vault) {
this.vault = vault;
this.setName("AscendingHackerThread");
}
@Override
public void run() {
for (int guess = 0; guess <= 9999; guess++) {
if (vault.isCorrectPassword(guess)) {
System.out.println(getName() + " found the password: " + guess);
System.exit(0);
}
}
}
}
package com.pc.lab1;
public class DescendingHackerThread extends Thread {
private final Vault vault;
public DescendingHackerThread(Vault vault) {
this.vault = vault;
this.setName("DescendingHackerThread");
}
@Override
public void run() {
for (int guess = 9999; guess >= 0; guess--) {
if (vault.isCorrectPassword(guess)) {
System.out.println(getName() + " found the password: " + guess);
System.exit(0);
}
}
}
}
package com.pc.lab1;
public class PoliceThread extends Thread {
public PoliceThread() {
this.setName("PoliceThread");
}
@Override
public void run() {
for (int i = 10; i >= 0; i--) {
System.out.println(getName() + " : " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
System.out.println("Game over for you hackers");
System.exit(0);
}
}
package com.pc.lab1;
import java.util.concurrent.ThreadLocalRandom;
public class Vault {
private final int password;
public Vault() {
this.password = ThreadLocalRandom.current().nextInt(0, 10000); // 0..9999
}
public boolean isCorrectPassword(int guess) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return this.password == guess;
}
}
/*
public class ThreadExample {
public static void main(String[] args) {
Thread t1 = new MyThread("Thread 1");
Thread t2 = new MyThread("Thread 2");
t1.start(); // start thread 1
t2.start(); // start thread 2
try {
t1.join(); // wait for t1 to finish
t2.join(); // wait for t2 to finish
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All threads finished!");
}
}
*/
\ No newline at end of file
package com.pc.lab1;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
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