need help creating a getmethod so that i can access the private int points in th
ID: 3642293 • Letter: N
Question
need help creating a getmethod so that i can access the private int points in the Gun class to access it in the toString method in the PlayerShip class, gun1.points or gun2.points does not work since the points field is set to private in the gun classpublic String toString() {
String ret = "Player[" + x + ", " + (gun1.points + gun2.points) + "pts]";
return ret;
}
import java.util.Scanner;
public class Game {
private EnemyShip enemy1;
private EnemyShip enemy2;
private EnemyShip enemy3;
private PlayerShip player;
public Game(int initialPosition) {
player = new PlayerShip(initialPosition);
enemy1 = new EnemyShip(0,1);
enemy2 = new EnemyShip(-4, 1);
enemy3 = new EnemyShip(2, -1);
}
public Game() {
int x,y;
Scanner input = new Scanner(System.in);
System.out.println("Enemy #1");
System.out.print(" - Inital x Position: ");
x = input.nextInt();
System.out.print(" - Inital velocity: ");
y = input.nextInt();
enemy1 = new EnemyShip(x,y);
System.out.println("Enemy #2");
System.out.print(" - Inital x Position: ");
x = input.nextInt();
System.out.print(" - Inital velocity: ");
y = input.nextInt();
enemy2 = new EnemyShip(x, y);
System.out.println("Enemy #3");
System.out.print(" - Inital x Position: ");
x = input.nextInt();
System.out.print(" - Inital velocity: ");
y = input.nextInt();
enemy3 = new EnemyShip(x, y);
}
public String toString() {
String ret = enemy1.toString() + enemy2.toString() + enemy3.toString() + player.toString();
return ret;
}
}
class Gun {
int x;
int power;
int points;
int bonus;
boolean justFired;
public Gun(int initialX, int initialPower) {
x = initialX;
power = initialPower;
points = 0;
justFired = false;
bonus = 1;
}
}
public class PlayerShip {
int x;
Gun gun1;
Gun gun2;
public PlayerShip(int initialX) {
x = initialX;
gun1 = new Gun(-1,5);
gun2 = new Gun(1,5);
}
public String toString() {
String ret = "Player[" + x + ", " + (gun1.points + gun2.points) + "pts]";
return ret;
}
}
public class EnemyShip {
int x;
int direction;
int life;
boolean justHit;
public EnemyShip(int initialX, int nDirection) {
x = initialX;
direction = nDirection;
life = 10;
justHit = false;
}
public String toString() {
String ret = "Enemy(" + x + ")";
if(life == 0) {
ret += "* ";
}
else {
ret += " ";
}
return ret;
}
}
Explanation / Answer
Please find below the Gun class with Private "points" Instance variable and the get and set methods. In the PlayerShip class , the toString method will have String ret = "Player[" + x + ", " + (gun1.getPoints() + gun2.getPoints()) + "pts]"; Find below the Gun and PlayerShip Class. class Gun { private int x; private int power; private int points; private int bonus; boolean justFired; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getPower() { return power; } public void setPower(int power) { this.power = power; } public int getPoints() { return points; } public void setPoints(int points) { this.points = points; } public int getBonus() { return bonus; } public void setBonus(int bonus) { this.bonus = bonus; } public Gun(int initialX, int initialPower) { x = initialX; power = initialPower; points = 0; justFired = false; bonus = 1; } } public class PlayerShip { int x; Gun gun1; Gun gun2; public PlayerShip(int initialX) { x = initialX; gun1 = new Gun(-1, 5); gun2 = new Gun(1, 5); } public String toString() { String ret = "Player[" + x + ", " + (gun1.getPoints() + gun2.getPoints()) + "pts]"; return ret; } } Hope it helps.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.