Please help solve this question sol.txt provided Space ship class: import java.u
ID: 3707873 • Letter: P
Question
Please help solve this question sol.txt provided
Space ship class:
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<Planet> 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++;
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
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<Planet> 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++;
}
public void loadSpaceShip(String fileName) {
BufferedReader br;
try {
br = new BufferedReader(new FileReader("C:/" + fileName));
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
----------------------------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Planet {
private String name;
private double maximumHealth;
private int numberOfWins;
private List<String> listOfPlanates;
public double getMaximumHealth() {
return maximumHealth;
}
public void setMaximumHealth(double maximumHealth) {
this.maximumHealth = maximumHealth;
}
public int getNumberOfWins() {
return numberOfWins;
}
public void setNumberOfWins(int numberOfWins) {
this.numberOfWins = numberOfWins;
}
public void setName(String name) {
this.name = name;
}
public Planet() {
// NO LOGIC
}
public Planet(String name, double maximumHealth, int numberOfWins) {
this.name = name;
this.maximumHealth = maximumHealth;
this.numberOfWins = numberOfWins;
}
public void addPlanate(String planate) {
if (listOfPlanates == null) {
listOfPlanates = new ArrayList<String>();
listOfPlanates.add(planate);
}
}
public List<Planet> loadPlanates(String fileName) {
BufferedReader br;
List<Planet> planets = null;
int index = 0;
try {
Planet planet = null;
br = new BufferedReader(new FileReader("C:/" + fileName));
planets = new ArrayList<Planet>();
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
planet = new Planet();
planet.addPlanate(sCurrentLine);
planets.add(planet);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return planets;
}
public static void main(String[] args) {
Planet planet = new Planet();
List<Planet> loadPlanates = planet.loadPlanates("sol.txt");
System.out.println(loadPlanates);
System.out.println("-------------------");
System.out.println(planet);
}
public int findPlanet(String planetName, List<Planet> allPlanets) {
return 0;
}
public String getName() {
return getName();
}
}
--------------------------------------------------------------------------------------------
sol.txt
------
Mercury 0.2 100
Venus 0.6 200
Earth 0 0
Mars 0.2 200
Jupiter 0.9 400
Saturn 0.85 350
Uranus 0.7 200
Neptune 0.5 100
Pluto 0.01 10
----------------------------------------------------------------------------------------------------------
player.txt
------------
SpaceShipName: Spacecruiser
MaximumHealth: 1500 base health
NumberOfWins: 20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.