Task 1: Classes and fields (for background purposes) Read the following system d
ID: 3642093 • Letter: T
Question
Task 1: Classes and fields (for background purposes)Read the following system description and identify the Task 1: Classes and fields (for background purposes) Read the following system description and identify the classes (written in italics) and their "fields" (enclosed in double quotes): The Game has 3 enemy ships ("enemy1", "enemy2", "enemy3") and 1 player ship ("player"). Each PlayerShip has an "x" position (an int) and two guns ("gun1" and "gun2"). Each Gun has an "x" position, a "power" level, "points", a "bonus" (all of which are ints) and a "justFired" status which can be true or false. Class names must begin with an uppercase letter while field names must begin with a lowercase letter. For compound words, the first letter of the second word should be capitalised. All classes must be public and all fields must be private. Class names must begin with an uppercase letter while field names must begin with a lowercase letter. For compound words, the first letter of the second word should be capitalised. All classes must be public and all fields must be private. //Code so far (for background purposes) //Gun.java 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;
}
} //EnemyShip.java public class EnemyShip
{
int x;
int direction;
int life;
boolean justHit;
public EnemyShip(int initialX, int nDirection)
{
x = initialX;
direction = nDirection;
life = 10;
sjustHit = false;
}
}
//PlayerShip.java public class PlayerShip {
int x;
Gun gun1 = new Gun(-1,5);
Gun gun2 = new Gun(1,5);
public PlayerShip(int initialX)
{
x = initialX;
}
}
//Game.java import java.util.Scanner;
public class Game {
private EnemyShip enemy1;
private EnemyShip enemy2;
private EnemyShip enemy3;
private PlayerShip player;
public Game(int initiaPosition)
{
player = new PlayerShip(initiaPosition);
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);
}
}
Explanation / Answer
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.