// Spaceship.java: implementation of a spaceship // The spaceship is located inside a 10x10 grid. // Position (1,1) is at the upper left corner; // Position (1,10) is the upper right; // Position (10,1) is the lower left; // Position (10,10) is the lower right. // Spaceships use 1.0 ton of fuel to move 1 unit on the grid. // Earth is at position (1,1), and the ship can refuel if it // gets there. public class Spaceship { // Earth is at position (1,1) // Note this variable is "static" because it's the same // for all spaceships. static final Position Earth = new Position(1, 1); // The fuel tank has a maximum capacity final double MAXFUEL = 30.0; // Keep track of the amount of fuel the spaceship has double tonsOfFuel; // Keep track of the current position of the spaceship int xPosition, yPosition; // constructor: create a new Spaceship at the specified location // assume that x and y are valid location values. public Spaceship(int x, int y) { xPosition = x; yPosition = y; // Starts with a full tank. tonsOfFuel = MAXFUEL; } // -------- You must implement this method --------- // // getFuel: return the current fuel amount. // Input: none // Output: a double representing the fuel amount. public double getFuel() { // This is included just to get it to compile. // You should modify the method to do the right thing. return 0.0; } // -------- You must implement this method --------- // // isValidPosition: check if (x,y) is a valid position // on the 10x10 grid, which means that // it falls from (1,1) to (10,10). // Input: two integers representing the position to check // Output: true if the position is valid, otherwise false public boolean isValidPosition(int x, int y) { // This is included just to get it to compile. // You should modify the method to do the right thing. return false; } // -------- You must implement this method --------- // // move: move the spaceship 'horiz' units horizontally // and 'vert' units vertically. Also, decrement // the current fuel level appropriately. // If the specified movement is invalid, do not // update the ship's position or its fuel level. // Note: the ship moves diagonally, so you'll need to // calculate the *diagonal* distance to figure out how // much fuel will be used (1.0 ton per 1.0 unit of distance). // // Input: two integers, as described above // Output: if this succeeds, return 0. Otherwise: // 1) If moving as dictated by 'horiz' and 'vert' would // cause the ship to go outside the boundaries of the // 10x10 grid, return 1. // Hint: use isValidPosition(). // 2) If the ship does not have enough fuel to move // as dictated by 'horiz' and 'vert', return 2. // You should do the checks in this order (invalid move first.) public int move(int horiz, int vert) { // This is included just to get it to compile. // You should modify the method to do the right thing. return 0; } // -------- You must implement this method --------- // // hyperjump: jump the ship instantly to position // ('newXPos','newYPos'). This costs 5.0 tons // of fuel, regardless of how far the ship goes. // // Input: two integers indicating the new position // Output: if this succeeds, return 0. Otherwise: // 1) If ('newXPos','newYPos') indicates an invalid position // (outside of the 10x10 grid), return 1. // Hint: use isValidPosition(). // 2) If the ship does not have enough fuel to make // the jump, return 2. // You should do the checks in this order (invalid move first.) public int hyperjump(int newXPos, int newYPos) { // This is included just to get it to compile. // You should modify the method to do the right thing. return 0; } // -------- You must implement this method --------- // // refuel: fill the ship's tank. This will only succeed // if the ship has returned to Earth (position (1,1)). // Hint: if you have two variables of type Position, // you can check to see if they are equal in the // same way we do for Strings: use .equals(). // Input: none // Output: if this succeeds, return 0. Otherwise, return 1. public int refuel() { // This is included just to get it to compile. // You should modify the method to do the right thing. return 0; } // getLocation: returns the current position of the spaceship, // in (x,y) coordinates // Input: none // Output: a Position object (see Position.java) public Position getLocation() { // Create a new Position object, setting its x and y // values to the current location of the spaceship. Position pos = new Position(xPosition, yPosition); // Return this pair of values to the user. return pos; } // displayStats: Show the location and fuel status. // Input: none // Output: none public void displayStats() { Position pos = getLocation(); System.out.print("The ship is now at position "); pos.display(); // Check the fuel tank System.out.println(", with " + getFuel() + " tons of fuel left.\n"); } }