Need help: Write a new class that implements the Weapon interface, which entails
ID: 665304 • Letter: N
Question
Need help:
Write a new class that implements the Weapon interface, which entails two main things: indicating this in the class header ->
public class ______ implements Weapon {
public void attack(Zombie zombie):
}
and implementing the method indicated in the Weapon interface ->
For this part, consider a few things:
The weapon can be whatever you want. It can be serious or silly.
The logic of a weapon can be as complex (for example, Stick and Laser) or as simple (BagOfRocks) as you wish -- though for this assignment, you are advised to go for a simpler option. The point is to get some exposure to the idea of implementing an interface and writing programs that interact with interface types.
However you choose to implement the attack method, you will have one local variable, the formal parameter pointing to a Zombie object. To interact with it, you will call the damage method of the Zombie class, so you will need to at least understand how to call it.
At the very least, your implementation of the attack method will need to do the following steps
Go to the following code in ZombieWars.java
You will need to make two changes to the switch statement: (1) Add a case 3 (along with a break statement at the end of case 2) that will setnewWeapon equal to a new object of the type that you created, and (2) change the switch expression so that % 3 becomes % 4 (so the expression can evaluate to 0, 1, 2, or 3). The modified code will probably look something like the following:
In your own code, "ClassName" will obviously be replaced by the name of the class that you created, while the ... will be replaced with actual parameters (matching the formal parameters in your class constructor) -- or, you will simply have an empty parameter list ().
============================================================
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
public class ZombieWars {
private static final AtomicBoolean keepGoing = new AtomicBoolean(true);
private static Random rand = new Random();
// Stick durability range
private static final int STICK_MIN = 5;
private static final int STICK_MAX = 30;
// Laser power range
private static final int LASER_MIN = 15;
private static final int LASER_MAX = 45;
// Rocks-in-bag range
private static final int ROCKS_MIN = 5;
private static final int ROCKS_MAX = 15;
// Zombie life range
private static final int ZOMBIE_MIN = 5;
private static final int ZOMBIE_MAX = 75;
// Game time
private static final int GAME_TIME = 120000;
// Range for zombie's initial proximity
private static final int PROX_MIN = 25;
private static final int PROX_MAX = 50;
// Range for zombie's approach per iteration
private static final int PROX_SHIFT_MIN = 5;
private static final int PROX_SHIFT_MAX = 15;
// Range for weapons delay in milliseconds
protected static final int WEAPON_DELAY_MIN = 1000;
protected static final int WEAPON_DELAY_MAX = 4000;
@SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException {
Thread game = new Thread(){
Zombie zombie = null;
Queue<Weapon> weapons;
@Override
public void run(){
weapons = getWeapons();
while (keepGoing.get()){
zombie = getNewZombie();
//25, 50
int zombieProximity = randomIntInRange(PROX_MIN, PROX_MAX);
System.out.println("A zombie is coming!");
zombie.approach();
System.out.println();
while (!zombie.isDown() && zombieProximity > 0){
System.out.printf(" Zombie is now %d feet away, with %d life remaining! ", zombieProximity, zombie.getLife());
while(weapons.isEmpty() && keepGoing.get()) ; // await weapons
weapons.remove().attack(zombie);
zombieProximity -= randomIntInRange(PROX_SHIFT_MIN, PROX_SHIFT_MAX);
}
System.out.println(" ");
if (zombie.isDown())
System.out.println("Yay, we did it!");
else
System.out.println("Our zombie friend enjoys "cerveaux provenle" tonight...");
System.out.println(" *********** ");
}
}
};
game.start();
Thread.sleep(GAME_TIME);
keepGoing.set(false);
game.join();
System.out.println("Game Over!");
}
private static Queue<Weapon> getWeapons() {
final Queue<Weapon> weapons = new LinkedList<Weapon>();
weapons.add(getRandomWeapon());
new Thread(){
public void run(){
while(keepGoing.get()){
weapons.add(getRandomWeapon());
try {
Thread.sleep(randomIntInRange(WEAPON_DELAY_MIN, WEAPON_DELAY_MAX));
} catch (InterruptedException e) {}
}
}
}.start();
return weapons;
}
// SOME CHANGES HERE
private static Weapon getRandomWeapon() {
Weapon w = null;
switch (Math.abs(rand.nextInt()) % 3){
case 0:
w = new Stick(randomIntInRange(STICK_MIN, STICK_MAX));
break;
case 1:
w = new Laser (randomIntInRange(LASER_MIN, LASER_MAX));
break;
case 2:
w = new BagOfRocks (randomIntInRange(ROCKS_MIN, ROCKS_MAX));
}
return w;
}
private static Zombie getNewZombie() {
return new Zombie(randomIntInRange(ZOMBIE_MIN, ZOMBIE_MAX));
}
// Helper method for generating random integer values
// within a certain range.
private static int randomIntInRange(int low, int high) {
int multiplier = high - (low - 1);
return (int)(Math.random() * multiplier) + low;
} // END randomIntInRange
}
Explanation / Answer
The required class to be completed:-
public class Axe implements Weapon{
public void attack(Zombie zombie){
zombie.damage();
}
}
The required method getRandomWeapon() to be modified:-
private static Weapon getRandomWeapon() {
Weapon newWeapon = null;
switch (Math.abs(rand.nextInt()) % 4){
case 0:
newWeapon = new Stick(randomIntInRange(STICK_MIN, STICK_MAX));
break;
case 1:
newWeapon = new Laser (randomIntInRange(LASER_MIN, LASER_MAX));
break;
case 2:
newWeapon = new BagOfRocks (randomIntInRange(ROCKS_MIN, ROCKS_MAX));
break;
case 3:
newWeapon = new Axe();
break;
}
return newWeapon;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.