Share
Explore

The Java Spy Adventure - Assisting Agent Peanut: Assignment 2 (due July 27)

Instructions:
Hand in via Uploading your GitHUB LINK

Java Lab Learning Notebook


How to submit this Assignment:

Get your Code working.
Make a GitHub Repository called S23 Java IN2343-G7
Go to github.com
Make an Account if necessary.
Once we are logged into our Account: make a Repository
Make files in your GitHub Repository named corresponding to your Assignment Activities.
Copy / paste your code from your IDE to that file and commit it.
Get the Repository URL from Address Bar of the browser:
Make a test file - Name it as : PeterSigurdson_C939393_A2.txt
into that text file: Put the url for your repository.
Good morning, Mr. Bichon.
Your mission, should you choose to accept it, involves the recovery of a vital resource stolen from the Central European Republic.
In the wake of an unidentified disaster, rescue dogs were brought in to locate survivors. Special dog food, designed to enhance the dogs' tracking abilities, was pilfered by the notorious international criminal and terrorist, Herr Wolf.
You and The Furr Squad are to retrieve the stolen supplies and bring Herr Wolf to justice.
The fate of the survivors and the entire republic depends on your team.
As always, should any member of your team be caught or killed, the Scretary will disavow any knowledge of your actions.
Good luck, Peanut.
This message will self-destruct in five seconds.

Introduction

In this lab, we'll be helping Agent Peanut, the Bichon Bond, navigate through a labyrinth filled with danger and excitement. As a developer, you'll be providing assistance to Agent Peanut by writing Java code to overcome various obstacles set by Herr Wolf. This lab will improve your skills in Java, problem-solving, and logical thinking.

Environment setup

You will need the latest version of Java installed and an Integrated Development Environment (IDE) such as IntelliJ IDEA or Eclipse.

Lab Exercises

Exercise 1: The Secret Entrance

Agent Peanut has located the secret entrance to Herr Wolf's lair. This entrance is locked by a security code. Your task is to write a Java method to crack the code. The code is a four-digit number with no repeating digits. The only hint given is that the product of these four digits is 24.

public class Main {
public static void main(String[] args) {
findCode();
}
public static void findCode() {
for(int i = 1; i < 10; i++) {
for(int j = 1; j < 10; j++) {
for(int k = 1; k < 10; k++) {
for(int l = 1; l < 10; l++) {
if(i != j && i != k && i != l && j != k && j != l && k != l) {
if(i * j * k * l == 24) {
System.out.println("The code is: " + i + j + k + l);
}
}
}
}
}
}
}
}

Exercise 2: Laser Hallway

Agent Peanut has now reached a hallway protected with lasers. You need to write a Java method that will simulate a strategy to help Agent Peanut navigate through the lasers. The hallway can be represented as a 2D array where 0 represents a safe zone, and 1 represents a laser.
Peanut can only move right or down in the grid.
Your task is to find a path from the top left to the bottom right.
public class Main {
public static void main(String[] args) {
int[][] hallway = {
{0, 0, 1, 0},
{1, 0, 1, 1},
{0, 0, 0, 0},
{1, 1, 1, 0}
};
navigateHallway(hallway);
}

public static void navigateHallway(int[][] hallway) {
// Implement your solution here
// Tip: Use recursion to explore all possible paths
}
}
image.png

Exercise 3: Safe Crack

Agent Peanut has located the safe where the stolen dog food is kept. Your mission is to write a Java method that will help Agent Peanut crack the safe. The safe is opened by a sequence of rotations - three turns right to the first number, two turns left to the second number, and one turn right to the final number. All numbers are in the range of 0-99 and the safe has a peculiar property - the sum of the second and third numbers is the first number.
public class Main {
public static void main(String[] args) {
crackSafe();
}
public static void crackSafe() {
// Implement your solution here
// You need to find three numbers that satisfy the safe's condition
}
}

Solution using nested for-loops to iterate through all possible combinations of numbers until it finds one that satisfies the conditions:

image.png

This code will output the first combination where the sum of the second and third numbers equals the first number. It will stop immediately once it has found a valid combination. If no valid combination is found after checking all possibilities, it will print "No valid combination found".


Lesson 1: Setting up the Scenario

class AgentPeanut {
String name;
String mission;
public AgentPeanut(String name, String mission) {
this.name = name;
this.mission = mission;
}
}

class HerrWolf {
String name;
String fortress;
public HerrWolf(String name, String fortress) {
this.name = name;
this.fortress = fortress;
}
}

public class Main {
public static void main(String[] args) {

image.png

}
}

Lesson 2: Navigating the Labyrinth

class Labyrinth {
String[][] path;

public Labyrinth(String[][] path) {
this.path = path;
}
}

public class Main {
public static void main(String[] args) {
String[][] path = {{"Start", "Path", "Trap"}, {"Trap", "Path", "End"}};
Labyrinth labyrinth = new Labyrinth(path);
// Write code here to navigate through the labyrinth
}
}
image.png

Lesson 3: Bypassing Security Systems

interface SecuritySystem {
boolean validate(String input);
}

class BiometricScanner implements SecuritySystem {
@Override
public boolean validate(String input) {
// Write code here to validate biometric data
}
}

class PasswordLock implements SecuritySystem {
@Override
public boolean validate(String input) {
// Write code here to validate password
}
}

public class Main {
public static void main(String[] args) {
BiometricScanner scanner = new BiometricScanner();
PasswordLock lock = new PasswordLock();
// Write code here to bypass the security systems
}
}
image.png

Lesson 4: Retrieving the Stolen Dog Food

class StolenFood {
String location;
int quantity;

public StolenFood(String location, int quantity) {
this.location = location;
this.quantity = quantity;
}
}

public class Main {
public static void main(String[] args) {
StolenFood food = new StolenFood("Vault", 100);
// Write code here to retrieve the stolen dog food
}
}
image.png

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.