Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1: Space Game (50 points) For this first question, you will create the code for a spaceship, planets, and have a game loop to

Question 1: Space Game (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 les. 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 nding an alien artifact on this planet during a search { Note that this chance is a number 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 non-static: A constructor that takes as input the name, chance of success for artifact nding, 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 chance of artifact success is less than zero or greater than one, or if the damage is less than 0. getName 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 from 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 nd 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 findPlanet(``Venus'', planets) must return 1.

(b) Spaceship Class The Spaceship class represents a spaceship travelling through to di erent planets in the solar system. Page 3 The Spaceship class must contain the following (private) attributes: A String name A double current health value A double maximum health value An int for the number of artifacts this spaceship has discovered An int number of wins in the space game A Planet which is where the spaceship is (the current planet) A static ArrayList of Planets { This will store the list of planets in the solar system Here are some of the public methods required in this class. You must decide if these methods are static or non-static. Note that you will also have to add getters and setters for attributes as needed. 1) A constructor The constructor for the Spaceship class takes one String, one double, and one int as input. These parameters represent the name, maximum health, and number of wins for the spaceship, in that order. Set the attributes of the spaceship using the parameters of the constructor. The current health of a new spaceship is the same as the maximum health. 2) The toString method This method must return a String consisting of the spaceship's name, current health, and number of alien artifacts found. Format the String in any way you want. This method will be very handy for debugging your code, and will be used during the space game to keep track of the status of each spaceship. 3) The setPlanets method This method sets the planets attribute. This method takes an ArrayList of planets as input. Remember that ArrayList is a mutable reference type and as such you should make a copy of the input ArrayList when initializing the attribute. That is, you cannot just set the attribute to be the parameter, as they will both point to the same data in memory. After the list is loaded, loop through the ArrayList and print out the details of every planet. Hint: Use the toString method of the Planet class 3) moveTo method This method will move the spaceship to a di erent planet. This method has only a String param- eter, which is the name of the planet to move to. This method does not return a value. This method must call the findPlanet method from the Planet class, which will return the index of the planet to move to. If the planet wasn't found, print a message that the spaceship could not move to that planet name. For example, The FES Hawking tried to move to Krypton, but that planet isn't in this solar system!. Otherwise, move the spaceship by setting the currPlanet attribute. Also print a message about moving. For example, The FES Hawking moved to Mars. 4) moveIn method Page 4 The method will move the spaceship closer into the solar system (closer to the sun). For example, calling moveIn when a spaceship is at Earth will move it to Venus. This method takes no parameters and returns no value. You must nd the index of the current planet by calling the findPlanet method. After changing the index, call the moveTo method with the new planet's name. If the spaceship is already at the rst planet in the planet list, print a message. For example, FES Hawking couldn't move in. No planet is closer in. 5) moveOut method This method is the opposite of the moveIn method. For example, calling moveOut when a spaceship is at Earth will move it to Mars. The instructions for the moveOut method are the same as for the moveIn method. Make sure to reverse how you get the index of the new planet, and modify the error message of this method to say no planet is farther out. 6) increaseWins method This method will increase the number of wins by the character by one, and does not return anything. This method will be called when the spaceship wins the space game.

(c) FileIO FileIO.java contains the following two methods for the rst question. You must gure out if the methods are static or non-static. 1) A method loadSpaceship This method takes as input a lename as a String parameter, and returns a new Spaceship, using the constructor de ned in the Spaceship class. The loadSpaceship method must use a FileReader and a BufferedReader to open the le speci ed by filename. Make sure to have two catch blocks to catch both FileNotFoundException and IOException when reading from the le. In these cases, throw an IllegalArgumentException with an appropriate error message that the le was not found, or that there was another error. Note that the catch block for the FileNotFoundException must be above the catch block for the IOException to this to work properly. Example player.txt and enemy.txt les are provided with the assignment. The format of these les is exactly this, one value per line: Name of the spaceship Maximum health Number of wins so far in the space game Use these values from the le to create and return a new Spaceship. Do not use the Scanner class. 2) The loadPlanets method. This method takes a lename of planets to read, and returns an ArrayList of Planets. An example of a le containing planets is found in the provided les as sol.txt. Read the le indicated by the method parameter filename, by using a FileReader and a BufferedReader to open the le speci ed by filename. Do not use the Scanner class. Make sure to have two catch blocks to catch both FileNotFoundException and IOException when reading from the le. In these cases, throw an IllegalArgumentException with an appropriate error message that the le was not found, or that there was another error. Note that the catch block for the FileNotFoundException must be above the catch block for the IOException for this to work properly. Page 5 The planets are de ned on each line, consisting of the planet name, the chance to nd an artifact, and the possible amount of damage. Hint: Use the split method from the String class. Use the three values on each line of the le to create new Planet instances, and then add these Planet instances to the new ArrayList of Planets which is returned from this method.

(d) SpaceGame The code for this part will go in a le named SpaceGame.java. The SpaceGame class must contain the following attributes: A non-static Scanner attribute for getting input from the user A non-static Spaceship which is the player A static final int NUM ARTIFACTS WIN = 5 { This is a constant, which stores the number of artifacts a ship must nd to win The SpaceGame class must contain the following methods. You are allowed to create as many other private methods as you want. 1) The SpaceGame constructor. This constructor takes one parameter, which is the lename of the planets in the solar system. This constructor must: Initialize the Scanner attribute to take input from the user Initialize the player Spaceship { An example player spaceship is given in \player.txt" in the provided les Use the method loadPlanets method from the FileIO class to load a le containing planets Use the setPlanets method from the Spaceship class to set the planets for all Spaceships Move the player to the rst Planet in the list (index 0) by using the moveTo method Print out a welcome message to the game, which informs the player how many artifacts they must nd 2) The private checkForDestroyed method This method returns 1 if the player's health is equal to or below zero. Return 0 otherwise 3) The private checkForWin method This method returns 1 if the player's number of artifacts found is equal to or greater than the NUM ARTIFACTS WIN constant. Return 0 otherwise 4) The public playGame method Loop while the player is still alive and has not found enough artifacts, using the checkForDestroyed and checkForWin methods { Ask the user for a command using the nextLine method of the Scanner instance { Choose a command based on the following choices, ignoring the case { If the input is moveIn: Call the moveIn method on the player spaceship { If the input is moveOut: Call the moveOut method on the player spaceship Page 6 { If the input is moveto Ask the user for a destination and get a line of input from the user Call the moveTo method on the player's spaceship with the input line { If the input is anything else Print a message that the input was not recognized When the above loop stops, print an appropriate message saying if their ship was destroyed, or they found enough artifacts. Here is some sample output produced by running the playGame method after Question 1 has been n- ished. Output for the nished assignment is found at the bottom of this document. Note that for this assignment, your output doesn't need to match this exactly. You are free to change these statements as you wish, as long as the required attributes still appear. Welcome to the SpaceGame! Loaded solar system from sol.txt: Name: Mercury Artifact Chance: 20.0% Possible Damage: 100.0 Name: Venus Artifact Chance: 60.0% Possible Damage: 200.0 Name: Earth Artifact Chance: 0.0% Possible Damage: 0.0 Name: Mars Artifact Chance: 20.0% Possible Damage: 200.0 Name: Jupiter Artifact Chance: 90.0% Possible Damage: 400.0 Name: Saturn Artifact Chance: 85.0% Possible Damage: 350.0 Name: Uranus Artifact Chance: 70.0% Possible Damage: 200.0 Name: Neptune Artifact Chance: 50.0% Possible Damage: 100.0 Name: Pluto Artifact Chance: 1.0% Possible Damage: 10.0 Spaceship FES Hawking moved to Mercury You must find 5 artifacts to win! Enter a command: moveout Spaceship FES Hawking moved to Venus Enter a command: moveto Enter the destination: mars Spaceship FES Hawking moved to Mars Enter a command: movein Spaceship FES Hawking moved to Earth Enter a command: explode Command not recognized: explode

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

Students also viewed these Databases questions