Question: Using Java, create a class Sword and main using the following outline: (The rest of the code will be below) 1) class is a regular
Using Java, create a class Sword and main using the following outline: (The rest of the code will be below)
1) class is a regular class called Sword, which will extend MeleeWeapon
a-Create a field called bladeLength you can include this as fields.
b- Any field you include needs a constructor and a getter.
c-Swords can be holstered unlike RayGuns (this is just an example dont focus on the logic).
d-We need to output the calculated damage
2)-The fifth class is Main, which will contain your main method (where your output comes from). In this class you will instantiate a Sword, and instantiate a Ray Gun, then output the calcualtedDamange for each, as well as the range. For the sword you will also need to output the fact that a sword can be holstered.
THIS CODE BELOW IS WHAT I HAVE, JUST NEED ANOTHER CLASS CALLED Sword AND THE MAIN CLASS THAT INSTANTIATES EVERYTHING!! public interface ICanHolster { //b-For now ICanholster is empty but once we fill it with method signatures } ==================================================================
public abstract class Weapon { protected String weaponName; public abstract void attackType(); public abstract void weaponName(String name); public Weapon(String weaponName) { this.weaponName = weaponName; } public int calculatesDamage(String attackType, int range) { if (attackType.equals("MeleeAttack")) { return 10; } else if (attackType.equals("rangedAttack ")) { if (range > 10) { return 2; } else if (range > 5) { return 5; } else if (range > 3) { return 8; } else { return 10; } } else { return 0; } } } ==================================================================
public abstract class MeleeWeapon extends Weapon implements ICanHolster { protected String attackType; public MeleeWeapon(String weaponName) { super(weaponName); attackType = "MeleeAttack"; } //a-MeleeWeapon has one abstract method called strike. public abstract void strike(); public void attackType() { attackType = "MeleeAttack"; } public int calculatesDamage(String attackType, int range) { return 10; } public void weaponName(String name) { this.weaponName = name; } } ==================================================================
public class RayGun extends Weapon { private int range; public RayGun(String weaponName, int range) { super(weaponName); this.range = range; } @Override public void attackType() { } @Override public void weaponName(String name) { } public void fire(int range) { this.range = range; } public int calculatesDamage(String attackType, int range) { this.range = range; if (range > 10) { return 2; } else if (range > 5) { return 5; } else if (range > 3) { return 8; } else { return 10; } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
