Question
ok so this compiles and wont run and its suppose to give this as a response. import java.util.List; /** * The Class Spaceship is used
ok so this compiles and wont run and its suppose to give this as a response.
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
------------------------------------------------------
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 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
------------------------------------------
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() = 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(); } }
Part 2 that you complete al the warm-up questions before starting this We strongly problem. The questions in this part of the assignment will be graded. These three questions will guide you to create a space game where your ship travels through a solar system to search for artifacts left behind by aliens. Note that these steps refer to the same Java classes. That is, you will only hand in your finished classes, not the intermediate steps. Question 1: Space G (50 points) For this first question, you will create the code for a spaceship, planets, and have a game loop to move around the solar system. Note that your code for this question will go in multiple .java files. Note that in addition to the required methods below, you are free to add as many other private methods as you want. (a) Planet Write a class Planet. A Planet has the following private attributes: A String name . A double chance of finding an alien artifact on this planet during a search Note that this chance is a umber between 0.0 and 1.0 . A double representing the damage possible when searching for an artifact For example, Venus' acid rain can give up to 200 points of damage The Planet class also contains the following public methods. You must decide if they are static or . A constructor that takes as input the name, chance of success for artifact finding, and the potential amount of damage for artifact searching This constructor must set the values for the class attributes using the parameter values -An IllegalArgumentException must be thrown if the chanoe of artifact success is less than zero or greater than one, or if the damage is less than 0. getNane which returns the name of the planet A toString method. The String which is returned must contain the name, the success chance as a percentage frorn 0 to 100 (so a chance of 0.5 is reported as 50.0%), and the potential damage -For example: Name: Venus Artifact Chance: 60.0% Possible Damage: 200.0 A findPlanet method. This method takes a String and an ArrayList of Planets. This method must search the ArrayList to find the planet with a name matching the String parameter (ignoring the case) The index of the Planet must be returned as an int if the Planet is found. Otherwise, return -1. For example, if the "sol.txt" solar system is loaded into the ArrayList planets, then findPlanetVenua" planets) must return (b) Spaceship Class The Spaceship class represents a spaceship travelling through to different planets in the solar system. Page 3Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started