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 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...
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...
hey i need perfect answer to following query. Needs 200 words for complete query. i need...
hey i need perfect answer to following query. Needs 200 words for complete query. i need typed solution . Don't post any pic in solution. Describe some important changes you would suggest to a “trebuchet” manufacturer. Explain your improvement in terms of motion in two dimensions.
I need the code in SAS and R and outputs please 2. The data below come...
I need the code in SAS and R and outputs please 2. The data below come from a study investigating a method of measuring body composition, and give the body fat percentage (% fat), age and sex for 18 adults aged between 23 and 61 years. Source: Mazess, R.B., Peppler, W.W., and Gibbons, M. (1984) Total body composition by dual-photon (153GD) absorptiometry. American Journal of Clinical Nutrition, 40, 834-839. age % fat sex 23 9.5 male 23 27.9 female 27...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT