In: Computer Science
I need to make changes to code following the steps below. The code that needs to be modified is below the steps. Thank you.
1. Refactor Base Weapon class:
a. Remove the Weapon abstract class and create a new Interface class named WeaponInterface.
b. Add a public method fireWeapon() that returns void and takes no arguments.
c. Add a public method fireWeapon() that returns void and takes a power argument as an integer type.
d. Add a public method activate() that returns void and takes an argument as an boolean type.
2. Refactor the specialization Weapon Classes:
a. Refactor the Bomb class so that it implements the WeaponInterface class.
b. Refactor the Gun class so that that it implements the WeaponInterface class.
3. Refactor the Game Class:
a. Remove all existing game logic in main().
b. Create a private helper method fireWeapon() that takes a WeaponInterface as an argument as a weapon. For the passed in weapon activate the weapon then fire the weapon.
c. Create an array of type WeaponInterface that can hold 2 weapons.
d. Initialize the first array element with a Bomb.
e. Initialize the second array element with a Gun.
f. Loop over the weapons array and call displayArea() for each weapon.
------
// Weapon.java
public abstract class Weapon {
public void fireWeapon(int power)
{
System.out.println("Class Name
:"+this.getClass().getSimpleName());
System.out.println("Mathod Name
:"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Power
:"+power);
}
public abstract void activate(boolean enable);
}
______________________
// Bomb.java
public class Bomb extends Weapon {
@Override
public void fireWeapon(int power)
{
System.out.println("Class Name
:"+this.getClass().getSimpleName());
System.out.println("Mathod Name
:"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Power
:"+power);
}
public void fireWeapon()
{
System.out.println("Class Name
:"+this.getClass().getSimpleName());
System.out.println("Mathod Name
:"+new Throwable().getStackTrace()[0].getMethodName());
super.fireWeapon(67);
}
@Override
public void activate(boolean enable) {
System.out.println("Class Name
:"+this.getClass().getSimpleName());
System.out.println("Mathod Name
:"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Enable
:"+enable);
}
}
_____________________________
// Gun.java
public class Gun extends Weapon {
@Override
public void fireWeapon(int power)
{
System.out.println("Class Name
:"+this.getClass().getSimpleName());
System.out.println("Mathod Name
:"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Power
:"+power);
}
public void fireWeapon()
{
System.out.println("Class Name
:"+this.getClass().getSimpleName());
System.out.println("Mathod Name
:"+new Throwable().getStackTrace()[0].getMethodName());
super.fireWeapon(98);
}
@Override
public void activate(boolean enable) {
System.out.println("Class Name
:"+this.getClass().getSimpleName());
System.out.println("Mathod Name
:"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Enable
:"+enable);
}
}
_____________________________
// Game.java
public class Game {
public static void main(String args[]) {
Bomb b = new Bomb();
Gun g = new Gun();
b.fireWeapon(45);
g.fireWeapon(60);
}
}
//Java Code
public interface WeaponInterface {
/**
* public method fireWeapon() that
* returns void and takes no arguments.
*/
public void fireWeapon();
/**
* public method fireWeapon() that returns void and
* takes a power argument as an integer type.
*/
public void fireWeapon(int power);
/**
* public method activate() that returns void and
* takes an argument as an boolean type.
*/
public void activate(boolean enable);
}
//=========================================
public class Gun implements WeaponInterface {
@Override
public void fireWeapon(int power)
{
System.out.println("Class Name :"+this.getClass().getSimpleName());
System.out.println("Method Name :"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Power :"+power);
}
@Override
public void fireWeapon()
{
System.out.println("Class Name :"+this.getClass().getSimpleName());
System.out.println("Method Name :"+new Throwable().getStackTrace()[0].getMethodName());
fireWeapon(98);
}
@Override
public void activate(boolean enable) {
System.out.println("Class Name :"+this.getClass().getSimpleName());
System.out.println("Method Name :"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Enable :"+enable);
}
}
//=========================================
public class Bomb implements WeaponInterface {
@Override
public void fireWeapon(int power)
{
System.out.println("Class Name :"+this.getClass().getSimpleName());
System.out.println("Method Name :"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Power :"+power);
}
@Override
public void fireWeapon()
{
System.out.println("Class Name :"+this.getClass().getSimpleName());
System.out.println("Method Name :"+new Throwable().getStackTrace()[0].getMethodName());
fireWeapon(67);
}
@Override
public void activate(boolean enable) {
System.out.println("Class Name :"+this.getClass().getSimpleName());
System.out.println("Method Name :"+new Throwable().getStackTrace()[0].getMethodName());
System.out.println("Enable :"+enable);
}
}
//===============================================
public class Game {
public static void main(String args[]) {
/**
* Create an array of type WeaponInterface that can hold 2 weapons.
*/
WeaponInterface[] weapons = new WeaponInterface[2];
weapons[0]= new Bomb();
weapons[1] = new Gun();
for (int i = 0; i <weapons.length ; i++) {
displayArea(weapons[i]);
}
}
/**
* a private helper method fireWeapon() that takes
* a WeaponInterface as an argument as a weapon.
*/
private static void displayArea(WeaponInterface weapon)
{
weapon.activate(true);
weapon.fireWeapon();
}
}
//Output

//If you need any help regarding this solution .......... please leave a comment .......... thanks