import java.util.List; /** * The Class Spaceship is used to store the spaceship
ID: 3708082 • Letter: I
Question
import java.util.List;
/**
* The Class Spaceship is used to store the spaceship details of a player.
*/
public class Spaceship {
/** The name. */
private String name;
/** The current health. */
private double currentHealth;
/** The maximum health. */
private double maximumHealth;
/** The number of artifact. */
private int numberOfArtifact;
/** The number of wins. */
private int numberOfWins;
/** The current planet. */
private Planet currentPlanet;
/** The all planets. */
private static List allPlanets;
/**
* Instantiates a new spaceship.
*
* @param name the name
* @param maximumHealth the maximum health
* @param numberOfWins the number of wins
*/
public Spaceship(String name, double maximumHealth, int numberOfWins) {
this.name = name;
this.maximumHealth = maximumHealth;
this.numberOfWins = numberOfWins;
this.currentHealth = maximumHealth;
this.currentPlanet = new Planet(null, 0, 0);
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the current health.
*
* @return the current health
*/
public double getCurrentHealth() {
return currentHealth;
}
/**
* Sets the current health.
*
* @param currentHealth the new current health
*/
public void setCurrentHealth(double currentHealth) {
this.currentHealth = currentHealth;
}
/**
* Gets the maximum health.
*
* @return the maximum health
*/
public double getMaximumHealth() {
return maximumHealth;
}
/**
* Sets the maximum health.
*
* @param maximumHealth the new maximum health
*/
public void setMaximumHealth(double maximumHealth) {
this.maximumHealth = maximumHealth;
}
/**
* Gets the number of artifact.
*
* @return the number of artifact
*/
public int getNumberOfArtifact() {
return numberOfArtifact;
}
/**
* Sets the number of artifact.
*
* @param numberOfArtifact the new number of artifact
*/
public void setNumberOfArtifact(int numberOfArtifact) {
this.numberOfArtifact = numberOfArtifact;
}
/**
* Gets the number of wins.
*
* @return the number of wins
*/
public int getNumberOfWins() {
return numberOfWins;
}
/**
* Sets the number of wins.
*
* @param numberOfWins the new number of wins
*/
public void setNumberOfWins(int numberOfWins) {
this.numberOfWins = numberOfWins;
}
/**
* Gets the current planet.
*
* @return the current planet
*/
public Planet getCurrentPlanet() {
return currentPlanet;
}
/**
* Sets the current planet.
*
* @param currentPlanet the new current planet
*/
public void setCurrentPlanet(Planet currentPlanet) {
this.currentPlanet = currentPlanet;
}
/**
* Sets the planets.
*
* @param allPlanets the new planets
*/
public static void setPlanets(List allPlanets) {
Spaceship.allPlanets = allPlanets;
System.out.println("Loaded solar system from sol.txt:");
for (Planet planet : Spaceship.allPlanets) {
System.out.println(planet.toString());
}
}
@Override
public String toString() {
return String.format("Spaceship's name: %s Current health: %.1f Number of alien aritfact found: %d", name, currentHealth, numberOfArtifact);
}
/**
* Move to.
*
* @param planetName the planet name
*/
public void moveTo(String planetName) {
int index = currentPlanet.findPlanet(planetName, allPlanets);
if (index < 0) {
System.out.println("The " + name + " tried to move to " + planetName + ", but that planet isn't in this solar system!.");
}
else {
this.currentPlanet = allPlanets.get(index);
System.out.println("The " + name + " moved to " + allPlanets.get(index).getName());
}
}
/**
* Move in.
*/
public void moveIn() {
int currIndex = currentPlanet.findPlanet(currentPlanet.getName(), allPlanets);
if(currIndex !=0){
String newPlanetName = allPlanets.get(currIndex - 1).getName();
moveTo(newPlanetName);
}
}
/**
* Move out.
*/
public void moveOut() {
int currIndex = currentPlanet.findPlanet(currentPlanet.getName(), allPlanets);
String newPlanetName = allPlanets.get(currIndex + 1).getName();
int newPlanetIndex = currentPlanet.findPlanet(newPlanetName, allPlanets);
if (newPlanetIndex < 0) {
System.out.println(" No planet is farther out.");
}
else {
moveTo(newPlanetName);
}
}
/**
* Increase wins.
*/
public void increaseWins() {
numberOfWins++;
}
}
------------------------------------------------------
import java.util.List;
/**
* The Class Planet is used to store planet details.
*/
public class Planet {
/** The name. */
private String name;
/** The artificat chance. */
private double artificatChance;
/** The possible damage. */
private double possibleDamage;
/**
* Instantiates a new planet.
*
* @param name the name
* @param artificatChance the artificat chance
* @param possibleDamage the possible damage
*/
public Planet(String name, double artificatChance, double possibleDamage) {
if (artificatChance < 0 || artificatChance > 1.0) {
throw new IllegalArgumentException("Artificate change must be between 0.0 to 1.0");
}
this.name = name;
this.artificatChance = artificatChance;
this.possibleDamage = possibleDamage;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the artificat chance.
*
* @return the artificat chance
*/
public double getArtificatChance() {
return artificatChance;
}
/**
* Sets the artificat chance.
*
* @param artificatChance the new artificat chance
*/
public void setArtificatChance(double artificatChance) {
this.artificatChance = artificatChance;
}
/**
* Gets the possible damage.
*
* @return the possible damage
*/
public double getPossibleDamage() {
return possibleDamage;
}
/**
* Sets the possible damage.
*
* @param possibleDamage the new possible damage
*/
public void setPossibleDamage(double possibleDamage) {
this.possibleDamage = possibleDamage;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("Name: %s Artificat Chance: %.1f Possible Damage: %.1f", name, artificatChance * 100, possibleDamage);
}
/**
* Find planet.
*
* @param planetName the planet name
* @param planets the planets
* @return the int
*/
public int findPlanet(String planetName, List planets) {
int index = -1;
for (int i = 0; i < planets.size(); i++) {
if (planetName.equals(planets.get(i).getName())) {
index = i;
break;
}
}
return index;
}
}
------------------------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* The Class FileIO is used to read the input files.
*/
public class FileIO {
//Q1: loadSpaceship method
public static Spaceship loadSpaceship(String filename){
String name = "";
double maxHealth = 0;
int numWins = 0;
Spaceship p = new Spaceship(name,maxHealth,numWins);
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
name = br.readLine();
maxHealth = Double.parseDouble(br.readLine());
numWins = Integer.parseInt(br.readLine());
p = new Spaceship(name,maxHealth,numWins);
br.close();
}catch(FileNotFoundException e){
System.out.println("File not found " + filename);
}catch(IOException e){
System.out.println("There was a problem. File: " + filename);
}
return p;
}
//Q1: loadPlanets method
public static ArrayList loadPlanets (String filename){
ArrayList planets = new ArrayList();
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String input = br.readLine();
while(input != null){
//System.out.println(input);
String[] tokens = input.split(" ");
Planet p = new Planet(tokens[0], Double.parseDouble(tokens[1]),Double.parseDouble(tokens[2]));
planets.add(p);
input = br.readLine();
}
br.close();
}catch(FileNotFoundException e){
System.out.println("File not found " + filename);
}catch(IOException e){
System.out.println("There was a problem. File: " + filename);
}
return planets;
}
}
--------------------------------------------
import java.util.ArrayList;
import java.util.Scanner;
/**
* The Class SpaceGame is used to play the space game.
*/
public class SpaceGame {
/** The scan. */
private Scanner scan;
/** The player. */
private Spaceship player;
/** The Constant NUM_ARTIFACTS_WIN. */
private static final int NUM_ARTIFACTS_WIN = 5;
/**
* Instantiates a new space game.
*
* @param planetFileName the planet file name
*/
public SpaceGame(String planetFileName) {
System.out.println("Welcome to the SpaceGame!");
scan = new Scanner(System.in);
player = FileIO.loadSpaceship("player.txt");
ArrayList planets = FileIO.loadPlanets(planetFileName);
Spaceship.setPlanets(planets);
player.moveTo(planets.get(0).getName());
System.out.println("You must find " + NUM_ARTIFACTS_WIN + " to win!");
}
/**
* Check for destroyed.
*
* @return the int
*/
private int checkForDestroyed() {
if (player.getCurrentHealth() <= 0) {
return 1;
}
else {
return 0;
}
}
/**
* Check for win.
*
* @return the int
*/
private int checkForWin() {
if (player.getNumberOfArtifact() >= NUM_ARTIFACTS_WIN) {
return 1;
}
else {
return 0;
}
}
/**
* Play game.
*/
public void playGame() {
while (checkForDestroyed() == 0 && checkForWin() == 0) {
System.out.println("Enter command:");
String command = scan.nextLine().toLowerCase();
if(command =="moveIn"){
player.moveIn();
}
else if(command=="moveOut"){
player.moveOut();
}
else if(command=="moveto"){
System.out.println("Enter the destination:");
String dest = scan.nextLine();
player.moveTo(dest);
}
else{
System.out.println("Command not recognized: " + command);
}
if (checkForWin() == 1) {
System.out.println("You have found enough artifacts");
}
if (checkForDestroyed() == 1) {
System.out.println("Your ship was destroyed");
}
}
}
public static void main(String[] args) {
SpaceGame spaceGame = new SpaceGame("sol.txt");
spaceGame.playGame();
}
}
Explanation / Answer
Please find the corrected code below.
CODE
===========================
Planet.java
*************************
import java.util.List;
/**
* The Class Planet is used to store planet details.
*/
public class Planet {
/** The name. */
private String name;
/** The artificat chance. */
private double artificatChance;
/** The possible damage. */
private double possibleDamage;
/**
* Instantiates a new planet.
*
* @param name the name
* @param artificatChance the artificat chance
* @param possibleDamage the possible damage
*/
public Planet(String name, double artificatChance, double possibleDamage) {
if (artificatChance < 0 || artificatChance > 1.0) {
throw new IllegalArgumentException("Artificate change must be between 0.0 to 1.0");
}
this.name = name;
this.artificatChance = artificatChance;
this.possibleDamage = possibleDamage;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the artificat chance.
*
* @return the artificat chance
*/
public double getArtificatChance() {
return artificatChance;
}
/**
* Sets the artificat chance.
*
* @param artificatChance the new artificat chance
*/
public void setArtificatChance(double artificatChance) {
this.artificatChance = artificatChance;
}
/**
* Gets the possible damage.
*
* @return the possible damage
*/
public double getPossibleDamage() {
return possibleDamage;
}
/**
* Sets the possible damage.
*
* @param possibleDamage the new possible damage
*/
public void setPossibleDamage(double possibleDamage) {
this.possibleDamage = possibleDamage;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("Name: %s Artificat Chance: %.1f Possible Damage: %.1f", name, artificatChance * 100, possibleDamage);
}
/**
* Find planet.
*
* @param planetName the planet name
* @param planets the planets
* @return the int
*/
public int findPlanet(String planetName, List<Planet> planets) {
int index = -1;
for (int i = 0; i < planets.size(); i++) {
if (planetName.equals(planets.get(i).getName())) {
index = i;
break;
}
}
return index;
}
}
Spaceship.java
*************************
import java.util.List;
/**
* The Class Spaceship is used to store the spaceship details of a player.
*/
public class Spaceship {
/** The name. */
private String name;
/** The current health. */
private double currentHealth;
/** The maximum health. */
private double maximumHealth;
/** The number of artifact. */
private int numberOfArtifact;
/** The number of wins. */
private int numberOfWins;
/** The current planet. */
private Planet currentPlanet;
/** The all planets. */
private static List<Planet> allPlanets;
/**
* Instantiates a new spaceship.
*
* @param name the name
* @param maximumHealth the maximum health
* @param numberOfWins the number of wins
*/
public Spaceship(String name, double maximumHealth, int numberOfWins) {
this.name = name;
this.maximumHealth = maximumHealth;
this.numberOfWins = numberOfWins;
this.currentHealth = maximumHealth;
this.currentPlanet = new Planet(null, 0, 0);
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the current health.
*
* @return the current health
*/
public double getCurrentHealth() {
return currentHealth;
}
/**
* Sets the current health.
*
* @param currentHealth the new current health
*/
public void setCurrentHealth(double currentHealth) {
this.currentHealth = currentHealth;
}
/**
* Gets the maximum health.
*
* @return the maximum health
*/
public double getMaximumHealth() {
return maximumHealth;
}
/**
* Sets the maximum health.
*
* @param maximumHealth the new maximum health
*/
public void setMaximumHealth(double maximumHealth) {
this.maximumHealth = maximumHealth;
}
/**
* Gets the number of artifact.
*
* @return the number of artifact
*/
public int getNumberOfArtifact() {
return numberOfArtifact;
}
/**
* Sets the number of artifact.
*
* @param numberOfArtifact the new number of artifact
*/
public void setNumberOfArtifact(int numberOfArtifact) {
this.numberOfArtifact = numberOfArtifact;
}
/**
* Gets the number of wins.
*
* @return the number of wins
*/
public int getNumberOfWins() {
return numberOfWins;
}
/**
* Sets the number of wins.
*
* @param numberOfWins the new number of wins
*/
public void setNumberOfWins(int numberOfWins) {
this.numberOfWins = numberOfWins;
}
/**
* Gets the current planet.
*
* @return the current planet
*/
public Planet getCurrentPlanet() {
return currentPlanet;
}
/**
* Sets the current planet.
*
* @param currentPlanet the new current planet
*/
public void setCurrentPlanet(Planet currentPlanet) {
this.currentPlanet = currentPlanet;
}
/**
* Sets the planets.
*
* @param allPlanets the new planets
*/
public static void setPlanets(List allPlanets) {
Spaceship.allPlanets = allPlanets;
System.out.println("Loaded solar system from sol.txt:");
for (Planet planet : Spaceship.allPlanets) {
System.out.println(planet.toString());
}
}
@Override
public String toString() {
return String.format("Spaceship's name: %s Current health: %.1f Number of alien aritfact found: %d", name, currentHealth, numberOfArtifact);
}
/**
* Move to.
*
* @param planetName the planet name
*/
public void moveTo(String planetName) {
int index = currentPlanet.findPlanet(planetName, allPlanets);
if (index < 0) {
System.out.println("The " + name + " tried to move to " + planetName + ", but that planet isn't in this solar system!.");
}
else {
this.currentPlanet = allPlanets.get(index);
System.out.println("The " + name + " moved to " + allPlanets.get(index).getName());
}
}
/**
* Move in.
*/
public void moveIn() {
int currIndex = currentPlanet.findPlanet(currentPlanet.getName(), allPlanets);
if(currIndex !=0){
String newPlanetName = allPlanets.get(currIndex - 1).getName();
moveTo(newPlanetName);
}
}
/**
* Move out.
*/
public void moveOut() {
int currIndex = currentPlanet.findPlanet(currentPlanet.getName(), allPlanets);
String newPlanetName = allPlanets.get(currIndex + 1).getName();
int newPlanetIndex = currentPlanet.findPlanet(newPlanetName, allPlanets);
if (newPlanetIndex < 0) {
System.out.println(" No planet is farther out.");
}
else {
moveTo(newPlanetName);
}
}
/**
* Increase wins.
*/
public void increaseWins() {
numberOfWins++;
}
}
FileIO.java
*************************
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* The Class FileIO is used to read the input files.
*/
public class FileIO {
//Q1: loadSpaceship method
public static Spaceship loadSpaceship(String filename){
String name = "";
double maxHealth = 0;
int numWins = 0;
Spaceship p = new Spaceship(name,maxHealth,numWins);
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
name = br.readLine();
maxHealth = Double.parseDouble(br.readLine());
numWins = Integer.parseInt(br.readLine());
p = new Spaceship(name,maxHealth,numWins);
br.close();
}catch(FileNotFoundException e){
System.out.println("File not found " + filename);
}catch(IOException e){
System.out.println("There was a problem. File: " + filename);
}
return p;
}
//Q1: loadPlanets method
public static ArrayList<Planet> loadPlanets (String filename){
ArrayList<Planet> planets = new ArrayList<>();
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String input = br.readLine();
while(input != null){
//System.out.println(input);
String[] tokens = input.split(" ");
Planet p = new Planet(tokens[0], Double.parseDouble(tokens[1]),Double.parseDouble(tokens[2]));
planets.add(p);
input = br.readLine();
}
br.close();
}catch(FileNotFoundException e){
System.out.println("File not found " + filename);
}catch(IOException e){
System.out.println("There was a problem. File: " + filename);
}
return planets;
}
}
SpaceGame.java
*************************
import java.util.ArrayList;
import java.util.Scanner;
/**
* The Class SpaceGame is used to play the space game.
*/
public class SpaceGame {
/** The scan. */
private Scanner scan;
/** The player. */
private Spaceship player;
/** The Constant NUM_ARTIFACTS_WIN. */
private static final int NUM_ARTIFACTS_WIN = 5;
/**
* Instantiates a new space game.
*
* @param planetFileName the planet file name
*/
public SpaceGame(String planetFileName) {
System.out.println("Welcome to the SpaceGame!");
scan = new Scanner(System.in);
player = FileIO.loadSpaceship("player.txt");
ArrayList<Planet> planets = FileIO.loadPlanets(planetFileName);
Spaceship.setPlanets(planets);
player.moveTo(planets.get(0).getName());
System.out.println("You must find " + NUM_ARTIFACTS_WIN + " to win!");
}
/**
* Check for destroyed.
*
* @return the int
*/
private int checkForDestroyed() {
if (player.getCurrentHealth() <= 0) {
return 1;
}
else {
return 0;
}
}
/**
* Check for win.
*
* @return the int
*/
private int checkForWin() {
if (player.getNumberOfArtifact() >= NUM_ARTIFACTS_WIN) {
return 1;
}
else {
return 0;
}
}
/**
* Play game.
*/
public void playGame() {
while (checkForDestroyed() == 0 && checkForWin() == 0) {
System.out.println("Enter command:");
String command = scan.nextLine().toLowerCase();
if(command =="moveIn"){
player.moveIn();
}
else if(command=="moveOut"){
player.moveOut();
}
else if(command=="moveto"){
System.out.println("Enter the destination:");
String dest = scan.nextLine();
player.moveTo(dest);
}
else{
System.out.println("Command not recognized: " + command);
}
if (checkForWin() == 1) {
System.out.println("You have found enough artifacts");
}
if (checkForDestroyed() == 1) {
System.out.println("Your ship was destroyed");
}
}
}
public static void main(String[] args) {
SpaceGame spaceGame = new SpaceGame("sol.txt");
spaceGame.playGame();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.