Question: Java: Please help with the following class. Thank you. Here is the Class from Project 2 as mentioned in the instructions: CLASS I NEED TO
Java: Please help with the following class. Thank you.


Here is the Class from Project 2 as mentioned in the instructions:
CLASS I NEED TO MODIFY:
public class SpaceShip {
/**
* Instance variables
*/
private String name;
private Traveler[] passengers;
private Location current;
private Location destination;
private int capacity;
private int numPassengers;
/**
* Instance variable flightHours - int representing the
* number of flight hours of the last flight rounded to the
* nearest int
*/
private int flightHours;
/**
* default constructor setting
* name, current, destination to null capacity to 10 and
* creates the travelers array and sets size to capacity
*/
private SpaceShip(String name, Traveler[] passengers, Location current, Location destination,
int capacity) {
this.name = name;
this.passengers = passengers;
this.current = current;
this.destination = destination;
this.capacity = capacity;
//additional constructor
this.flightHours = hours;
numPassengers = 0;
}
/**
* constructor, sets instance variables to match
* parameter values
* creates the traveler array and sets size to capacity.
*/
public SpaceShip(String name, Location current, Location destination, int capacity) {
this(name, new Traveler[capacity], current, destination, capacity);
}
/**
* accessor method name
*/
public String getName() {
return name;
}
//accessor method current
public Location getCurrent() {
return current;
}
//accessor method destination
public Location getDestination() {
return destination;
}
//mutator method
public void setDestination(Location destination) {
this.destination = destination;
}
//accessor capacity
public int getCapacity() {
return capacity;
}
//
private int positionOf(Traveler t) {
for (int i = 0; i
if (passengers[i].getId() == t.getId()) return i;
}
return -1;
}
/oolrsn isOnBoard
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
for (int i = position; i
passengers[i] = passengers[i-1];
}
passengers[numPassengers-1] = null; // We're in Java, remember?
numPassengers--;
t.setCurrent(this.current);
return true;
}
public void move() {
current = destination;
destination = null;
// Get 'em off board!
for (int i = 0; i
passengers[i].setCurrent(current);
passengers[i] = null; // Still Java...
}
numPassengers = 0;
}
@Override
public String toString() {
return "Spaceship [name=" + name + ", current=" + current
+ ", destination=" + destination
+ ", capacity=" + capacity + ", crew=" + numPassengers + "]";
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
