Java: Please help with the SpaceShip class. I already have the Location class an
ID: 3750648 • Letter: J
Question
Java: Please help with the SpaceShip class. I already have the Location class and Traveler class Thank you.
Location Class:
SpaceShip class - contains spaceship information and behaviors. See UML class diagram below: SpaceShip name: String capacity: int travelers:Collection (Traveler) numTraverlers: int current: Location -destination: Location +SpaceShip (name): setName +SpaceShip(name:String, current:Location, destination:Location,capacity:int +getName): String +getCapacity): int +getCurrent): Location +getDestination): Location +setName (name:String) +setCurrent (current:Location) +setcapacity( capacity:int) +setDestination (destination:Location) +board (t:Traveler): boolean leave(t: Traveler) tisOnBoard (t: Traveler): boolean +move () +toString) StringExplanation / Answer
class SpaceShip{
private String name;
private int capacity, numTravelers;
private Traveler Collection;
private Location current, destination;
//no argument constructor
public SpaceShip(){
current = null;
name = null;
destination = null;
capacity = 10;
Collection = new Traveler(capacity);
numTravelers = 0;
}
//parameterized constructors.
public SpaceShip(String name){
this.name = name;
current = null;
destination = null;
capacity = 10;
Collection = new Traveler(capacity);
numTravelers = 0;
}
public SpaceShip(String name,Location current,Location destination,int capacity){
this.name = name;
this.current = current;
this.destination = destination;
this.capacity = capacity;
Collection = new Traveler(capacity);
numTravelers = 0;
}
//accessors.
String getName(){
return name;
}
int getCapacity(){
return capacity;
}
Location getCurrent(){
return current;
}
Location getDestination(){
return destination;
}
//mutators.
void setName(String name){
this.name = name;
}
void setCurrent(Location current){
this.current = current;
}
void setCapacity(int capacity){
this.capacity = capacity;
}
void setDestination(Location destination){
thisi.destination = destination;
}
//board method
boolean board(Traveler t){
if(numTravelers<capacity&&isOnBoard(t)){
Collection[numTravelers++] = t;
return true;
}
return false;
}
//leave method.
boolean leave(Traveler t){
for(int i=0;i<numTravelers;i++){
if(Collection[i].getID()==t.getID()){
for(int j=i;j<numTravelers-1;j++)
Collection[j] = Collection[j+1];
numTravelers--;
return true;
}
}
return false;
}
//isOnBoard method.
boolean isOnBoard(Traveler t){
for(int i=0;i<numTravelers;i++){
if(Collection[i].getID()==t.getID())
return true;
return false;
}
//move method.
void move(){
current = destination;
destination = null;
for(int i=0;i<numTravelers;i++){
Collection[i].setLocation(current);
for(int i=0;i<numTravelers;i++){
Collection[i] = null;
numTravelers = 0;
}
//toString method which overrides the predefined method.
public String toString(){
return "SpaceShip[name="+getName()+", current="+getCurrent()+", destination="+getDestination()+", capacity"+getCapacity()+", passengers="+numTravelers+"]";
}
}
//I didn't run the program as I don't have the code of the other two classes, in case of any errors or queries, post in comment section. But I wrote it as sure as I can.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.