Question: This is a java class that Im working on and I'm problems doing the following, the class I have provided was the class orginally from

This is a java class that Im working on and I'm problems doing the following, the class I have provided was the class orginally from project to and what must be modified to the below parameters. :

Spaceship Class:

1. Similar to Project 2 with the following modification

2. Instance variables 1. tripLength int representing the number of flight hours of the route flown by this spaceship rounded to the nearest int. 3. Methods 1. Default constructor sets all instance variables to a default value include new instance variables.

2. Parameterized Constructor 1. Takes in all parameters required to set the instance variables.

3. move method same functionality as Project 2 with the following modification 1. boolean method 1. returns true if moved allowed 2. returns false if moved not allowed. 2. Calls addFlightToHistory for all travelers on board

4. public String toString() 1. returns a nicely formatted String (see below) that represents the Spaceship object information as well as a list of all Travelers. 2. Display a list of all Travelers on the flight (hint call the toString on each Travelers object in the array) Spaceship [name=Behemoth, current=RING, destination=EARTH, capacity=20, travelers=10, tripLength = 20]

Crew [name=Klaes Ashford, id=3, current=BELT, position = Captain, flight hours = 6728]

Passenger [name=Klaes Ashford, id=3, current=BELT, seat=FIRST, cost = $1499.99, rewardsPoints = 3459]

My code :

import java.util.*;

public class Spaceship {

private String name; private int capacity; private int numTraverlers; private ArrayList travelers; private Location current; private Location destination; private int tripLength;

public Spaceship() { this.name = null; this.current = null; this.destination = null; this.tripLength = 0; this.capacity = 10; this.travelers = new ArrayList<>(capacity); }

public Spaceship(String name) { this.name = name; this.current = null; this.destination = null; this.tripLength = 0; this.capacity = 10; this.travelers = new ArrayList<>(capacity); }

public Spaceship(String name, int capacity, Location current, Location destination, int tripLength) { this.name = name; this.capacity = capacity; this.current = current; this.destination = destination; this.tripLength = tripLength; travelers = new ArrayList<>(capacity); }

public String getName() { return name; }

public int getCapacity() { return capacity; }

public Location getCurrent() { return current; }

public Location getDestination() { return destination; } public int getTripLength() { return tripLength; } public void setTripLength(int tripLength) { this.tripLength = tripLength; }

public void setName(String name) { this.name = name; }

public void setCapacity(int capacity) { this.capacity = capacity; }

public void setCurrent(Location current) { this.current = current; }

public void setDestination(Location destination) { this.destination = destination; }

public boolean board(Traveler t) { if(numTraverlers { travelers.add(t); numTraverlers++; return true; } return false; }

public boolean leave(Traveler t) { if(isOnBoard(t)) { travelers.remove(t); numTraverlers--; return true; } return false; }

public boolean isOnBoard(Traveler t) { if(travelers.contains(t)) { return true; } return false; }

public void move() { current = destination; destination = null; for(Traveler t:travelers) { t.setLocation(current); } travelers.clear(); }

public String toString() { Passenger p; return "Spaceship [name = " + name + ", current = " + current + ", destination = " + destination + ", capacity = " + capacity + ", passengers = " + numTraverlers + "]"; } }

The original project 2 parameters are below:

Program Requirements 1. Location class - the class for identifying the four Locations: a. The location are EARTH, BELT, MARS, RING. b. Implement the Location class as a Java Enum https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html 2. Traveler class the class storing the travelers information. See UML Class Diagram below: a. static Variable - int nextIDNum - starts at 0 and is used to generate the next Travelers id. b. Instance Variables i. name - the name of the traveler. ii. id - id number of traveler iii. current current location of the traveler c. Constructors i. public Traveler() default constructor setting name and current to null and the id to the next id number ii. public Traveler(String name, Location l) 1. Sets name and location to parameter value 2. Sets id instance variable to next id number. d. Methods i. set/get methods according to the diagram ii. Remember in setId method use nextIDNum to determine new ID number. iii. public String toString() 1. returns a nicely formatted String (see below) that represents the state of the object (the value of the instance variables) 2. Traveler [name=Klaes Ashford, id=3, current=BELT] 3. SpaceShip class contains spaceship information and behaviors. See UML class diagram below: a. Instance Variables i. name - the name of the Spaceship. ii. capacity maximum number of travelers allowed on the spaceship iii. current current location of the spaceship iv. numOfTravelers actual number of Travelers on spaceship, partially filled array value initialize to zero. v. travelers array that contains Traveler Objects that have boarded the spaceship vi. destination destination location of the spaceship b. Constructors i. public SpaceShip() - default constructor setting name, current and destination to null, capacity to 10 and creates the travelers array and sets size to capacity. ii. public SpaceShip(String name, Location current, Location destination, int capacity) 1. sets instance variables to matching parameter value 2. creates the travelers array and sets size to capacity. c. Methods i. Accessor methods for all instance variables as describe in UML class diagram above. (No accessor or mutator for collections such as Arrays!!!!) ii. No mutator method for numOfTravelers as this is our partially filled array value. iii. boolean board(Traveler t) travelers board the spaceship 1. If spaceship is not full (at capacity) and the traveler is not already on board a. add traveler to array b. increment numOfTravelers c. return true. 2. If spaceship is full or traveler already on board, return false. 3. Travelers are exclusively identified by their ID! iv. boolean leave(Traveler t) travelers leave the spaceship 1. If traveler is on-board, in the array, remove them and return true. 2. If not on-board, do nothing and return false. v. boolean isOnBoard(Traveler t) checks if a traveler is on-board. 1. returns true if passenger on-board 2. returns false is passenger not on-board. 3. Called a Predicate method (Hint may be used in others methods above to keep code modular) vi. void move()- moves the ship to its destination 1. sets the ships current location to its destination. 2. sets the destination to null 3. sets all travelers locations to the current location of the spaceship 4. removes all travelers from the spaceship (array) vii. public String toString() 1. returns a nicely formatted String (see below) that represents the state of the object (the value of the instance variables) 2. Spaceship [name=Behemoth, current=RING, destination=null, capacity=20, passengers=0] 4. SpaceShipTester class with a main method: A text-based driver program that performs the following steps: a. It asks for four Travelers and two StarShips (Part 1, below) b. It executes a text-menu with the following options (Parts 2-4 below) i. Add traveler to a Spaceship ii. Search for traveler on a Spaceship iii. Move a spaceship to a new location iv. Exit the program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!