Question: /* * Spaceship Class 2. Additional instance variable flightHours int representing the number of flight hours of the last flight rounded to the nearest int.

/*

* Spaceship Class

2. Additional instance variable

flightHours int representing the number of flight hours of the

last flight 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

2.1. Takes in all parameters required to set the instance

variables.

3. move method same functionality as Project 2 with the following

modification

3.1. boolean method

> returns true if moved allowed

> returns false if moved not allowed.

> Travelers on board are updated by calling the move

method with the parameter this.

4 public String toString()

returns a nicely formatted String (see below) that represents the

Spaceship object information as well as a list of all Travelers.

Spaceship [name=Behemoth, current=RING, destination=null, capacity=20,

travelers=10, flighthours = 20]

It additionally contains a list of all Travelers on the flight (Hint: call

the toString on each Travelers object in the array)

Crew [name=Susan Ivanova, id=3, current=BELT, position =

Captain, pay = $234.00, flight hours = 6728]

Passenger [name=Klaes Ashford, id=3, current=BELT,

seat=FIRST, cost = $1499.99, rewardsPoints = 3459]

*

*/

public class SpaceShip {

private String name;

private Traveler[] passengers;

private Location current;

private Location destination;

private int capacity;

private int numPassengers;

private int flightHours;

private SpaceShip(String name, Traveler[] passengers, Location current, Location destination,

int capacity, int flightHours) {

this.name = name;

this.passengers = passengers;

this.current = current;

this.destination = destination;

this.capacity = capacity;

this.setFlightHours(flightHours);

numPassengers = 0;

}

public SpaceShip(String name, Location current, Location destination, int capacity, int flightHours) {

this(name, new Traveler[capacity], current, destination, capacity, flightHours);

}

public String getName() {

return name;

}

public Location getCurrent() {

return current;

}

public Location getDestination() {

return destination;

}

public void setDestination(Location destination) {

this.destination = destination;

}

public int getCapacity() {

return capacity;

}

private int positionOf(Traveler t) {

for (int i = 0; i < numPassengers; i++) {

if (passengers[i].getId() == t.getId()) return i;

}

return -1;

}

public boolean isOnBoard(Traveler t) {

return (positionOf(t) >= 0);

}

public boolean board(Traveler t) {

if ((capacity > numPassengers) && (t.getCurrent() == current)) {

passengers[numPassengers++] = t;

return true;

}

else

{

return false;

}

}

public boolean leave(Traveler t) {

int position = positionOf(t);

if (position < 0) return false;

for (int i = position; i < numPassengers; i++) {

passengers[i] = passengers[i-1];

}

passengers[numPassengers-1] = null;

numPassengers--;

t.setCurrent(this.current);

return true;

}

public boolean move(int numPassengers) {

current = destination;

destination = null;

this.numPassengers = numPassengers;

boolean moveVar = true;

if(moveVar) {

for (int i = 0; i < numPassengers; i++) {

passengers[i].setCurrent(current);

passengers[i] = null;

}

numPassengers = 0;

moveVar = false;

}

else{

}

return false;

}

@Override

public String toString() {

return "Spaceship [name=" + name + ", current=" + current

+ ", destination=" + destination

+ ", capacity=" + capacity + ", crew=" + numPassengers + "]";

}

public int getFlightHours() {

return Math.round(flightHours);

}

public void setFlightHours(int flightHours) {

this.flightHours = flightHours;

}

}

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!