Question: Java. Please review my code below: I have the following errors and not sure how to handle them. Thank you. First error: has to do
Java. Please review my code below: I have the following errors and not sure how to handle them. Thank you.
First error: has to do with my array: I get the following error: The type of the expression must be an array type but it resolved to Traveler
Second: line 101 error : Multiple markers at this line
- The type of the expression must be an array type but it resolved to Traveler
- The type of the expression must be an array type but it resolved to Traveler
Third: line 132 error: duplicate local variable
Here is my code:
public 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)
{
this.destination = destination;
}
//board method
boolean board(Traveler t)
{
if(numTravelers { Collection[numTravelers++] = t; return true; } return false; } //leave method. boolean leave(Traveler t) { for(int i=0;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. @Override public String toString() { return "SpaceShip[name="+getName()+", current="+getCurrent()+", destination="+getDestination()+", capacity"+getCapacity()+", passengers="+numTravelers+"]"; } } This is my other Class Nor errors here: enum Location { EARTH, MARS, BELT, RING; } class Traveler { private static int nextIDNum; private String name; private int id; private Location current; public Location getCurrent() { return current; } public void setCurrent(Location aCurrent) { current = aCurrent; } // constructor to set name and location public Traveler(String aName, Location aCurrent) { super(); name = aName; current = aCurrent; setId(); } public Traveler() { super(); } public String getName() { return name; } public void setName(String aName) { name = aName; } public int getID() { return id; } public void setId() { id = ++nextIDNum; } // toString to return object data @Override public String toString() { return "Traveler [name=" + name + ", id=" + id + ", location=" + current.name() + "]"; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
