Question

In: Computer Science

I need to make changes to code following the steps below. The code that needs to...

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);
   }

}

Solutions

Expert Solution

//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


Related Solutions

Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work ?
This problem needs to be solved with source code. I need a C++ program that will...
This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared. 1.2. We received the following ciphertext which was encoded with a shift cipher: xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds. 1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is...
I need assistance with using Verilog to code the following: The decoder needs attention. Its function...
I need assistance with using Verilog to code the following: The decoder needs attention. Its function is to take the 10‐bit input, and give three 4‐bit signals. The first 4‐bit signal should be the number of hundreds, the second 4‐bit signal should be the number of tens, and the final 4‐bit signal should be the number of units. In Verilog, these can be calculated simply using the numerical operators, specifically: Divide (/)  Modulus(%) Given: input[9:0] number reg[3:0] numb_bcd0, numb_bcd1, numb_bcd2;   ...
I need an original matlab code and gui for a simple double pendulum. This needs to...
I need an original matlab code and gui for a simple double pendulum. This needs to be original and not too complicated. Please provide basic instructions. Thank you!
Please make the following changes to the following code: #This program takes in the first and...
Please make the following changes to the following code: #This program takes in the first and last name and creates a userID printing first letter of first name and first 7 characters of last name #by Abi Santo #9/20/20 def main(): print("This Program takes your first and last name and makes a User ID") f_name = str(input("Please enter your first name ")).lower() l_name = str(input("Enter your last name: ")).lower() userID = str(f_name[:1]+str(l_name[:7])) print(userID) main() Call the function createUserName(). This function...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
Leave code as is ALL I NEED IS TO MAKE THE LIST INTO ASCENDING ORDER FROM...
Leave code as is ALL I NEED IS TO MAKE THE LIST INTO ASCENDING ORDER FROM A TO Z OF ARTISTS NAMES YOU CAN USE THIS AS REFERENCE SPOTIFY LIST TOP 50 AS REFERENCE FOR CSV FILE https://spotifycharts.com/viral/ import java.io.*; public class Spotify { public static void main(String[]args) { // Read csv Spotify file located on my desktop // Download csv file, save on desktop, and add file location in csvFile = " " String csvFile = "CSVFILE LOCATION OF...
Proc,Forks,Exec Examine the code given and do changes as mentioned in the steps below: #include "HALmod.h"...
Proc,Forks,Exec Examine the code given and do changes as mentioned in the steps below: #include "HALmod.h" int GetCommand (string tokens []) { string commandLine; bool commandEntered; int tokenCount; do { cout << "HALshell> "; while (1) { getline (cin, commandLine); commandEntered = CheckForCommand (); if (commandEntered) { break; } } } while (commandLine.length () == 0); tokenCount = TokenizeCommandLine (tokens, commandLine); return tokenCount; } int TokenizeCommandLine (string tokens [], string commandLine) { char *token [MAX_COMMAND_LINE_ARGUMENTS]; char *workCommandLine = new char...
i need the whole html code Make a layout template that contains a header and two...
i need the whole html code Make a layout template that contains a header and two paragraphs. Use float to line up the two paragraphs as columns side by side. Give the header and two paragraphs a border and/or a background color so you can see where they are.
For the following code I need to open the file of a persons choice which I...
For the following code I need to open the file of a persons choice which I did. I can get it to read the file, but I need help with counting lines and characters feel like i have the code I just cant make it work. Either way, the program should then ask the user Analyze another file (y/n)? and repeat the process again (asking for a file and analyzing its meta-data). This should continue repeating until the user chooses...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT