Java. Please review my code below: I have the following errors and not sure how
ID: 3752216 • Letter: J
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 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<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.
@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() + "]";
}
}
Explanation / Answer
Please find error free code below.
CODE
================
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() + "]";
}
}
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<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].setCurrent(current);
for(int j = 0; j < numTravelers; j++)
{
Collection[j] = 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+"]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.