Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the “promptInt” method in class Global, a constructor for class PlayerShip

ID: 3898413 • Letter: U

Question

Using the “promptInt” method in class Global, a constructor for class PlayerShip reads input from the user and initialises the “position” field. Your code should provide the String “Player position: “ as a parameter to method “promptInt”. (Note that in the String there is a space after the colon.) The following is an example of what this I/O looks like when the constructor executes (the bold and underlined text indicates the user’s input):
The PlayerShip constructor also creates two guns with power 5, with the first gun immediately one position to the left of the player ship, and the second gun immediately one position to the right of the player ship. public class Game // instance variables - replace the example below wi private PlayerShip player; private EnemyShip enemy1; private EnemyShip enemy2; private EnemyShip enemy3; public Game () PlayerShip player new PlayerShip() EnemyShip enemy! = new EnemyShip(); EnemyShip enemy2 new EnemyShip); EnemyShip enemy3 = new EnemyShip ( );

Explanation / Answer

class PlayerShip { private int position; private Gun gun1, gun2; public PlayerShip() { position = Global.promptInt("Player position: "); // assuming you have some gun class with below constructor gun1= new Gun(5, position - 1); gun2= new Gun(5, position + 1); } public int getPosition() { return position; } public Gun getGun1() { return gun1; } public Gun getGun2() { return gun2; } }