15th Apr 2022, 6:57 AM
Hassan Wasfy
Hassan Wasfy - avatar
3 Answers
0
or public interface Fireable { void fire(); } public class Gun { Fireable ammo = new NoAmmo(); public void performUpgrade( Fireable ammoType) { ammo = ammoType; } public void fire() { ammo.fire(); } } class NoAmmo implements Fireable { public void fire() { System.out.println("click"); } } public class Rockets implements Fireable { @Override public void fire() { System.out.println("Rocket fired"); } } ... // input 500 myGun.fire(); //click myGun.performUpgrade(upgrades); myGun.fire(); //Rocket fired
15th Apr 2022, 6:07 PM
zemiak
0
public class BaseGun extends Gun { Upgradeable upgradeable; @Override public void performUpgrade(Upgradeable upgradeable) { this.upgradeable = upgradeable; upgradeable.upgradeNow(); // added } } ... if (score > 0 && score < 500) { upgrades = new Bullets(); //upgrades.upgradeNow(); } else if (score >= 500 && score < 1000) { upgrades = new Rockets(); //upgrades.upgradeNow(); } else if (score >= 1000) { upgrades = new Nuclear(); //upgrades.upgradeNow(); } ps don't forget, in case score=500 or score=1000 you need >= operator if (score >= 500 instead of if (score > 500
15th Apr 2022, 3:28 PM
zemiak
0
zemiak rate it šŸ‘šŸ‘
15th Apr 2022, 4:23 PM
Hassan Wasfy
Hassan Wasfy - avatar