Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

dit View Go Tools Window Help Project2.pdf (page 2 of 8) -Edited -2-ka Q Search

ID: 3754288 • Letter: D

Question

dit View Go Tools Window Help Project2.pdf (page 2 of 8) -Edited -2-ka Q Search ac Program Requirements I Location elass the class for identifying the four Locations a. The location are EARTH, BELT, MARS, RING. b. Implement the Location class as a Java Enum - https://docs.oracle.com/javase/tutorial/java/java00/enum.html ler.ja 2. Traveler class- the class storing the traveler's information. See UML Class Diagram below pt Traveler nextIDNum: int nane: String id: int current: Location pu Traveler(name:String,location:Location getName): String getIDO: int getlocation): Location setLocation location:Location) tostring): String sotNane(nane: String) pl pu a. static Variable- int nextiDNum-starts at o and is used to generate the nest Traveler's id. b. Instance Variables pu L u. u. name- the name of the traveler id -id number of traveler current- current location of the traveler pu s. publie Traveler0 -default constructor setting name and eurrent to null and the id to the next id number public Traveler(String name, Leation 1) , pu 1. 2. Sets name and location to parameter value Sets id instance variable to next id number d. Methods t. set/get methods according to the diagram u. Remember in setld method use nextIDNum to determine new ID number I. public String toString) returns a nicely formatted String (see below) that represents the state of the object (the value of the instance variables) Traveler (name-Klaes Ashford, id-s.current-BELT I. 2. ors (1

Explanation / Answer

// Location.java

public enum Location {

       EARTH,

       BELT,

       MARS,

       RING;

}

//end of Location.java

// Traveler.java

public class Traveler {

      

       private static int nextIDNum =0;

       private String name;

       private int id;

       private Location current;

      

       // constructors

       public Traveler()

       {

             name=null;

             current = null;

             nextIDNum++;

             id= nextIDNum;

       }

      

       public Traveler(String name,Location l)

       {

             this.name = name;

             this.current = l;

             nextIDNum++;

             id= nextIDNum;

       }

      

       //getters

       public String getName()

       {

             return name;

       }

      

       public int getID()

       {

             return id;

       }

      

       public Location getLocation()

       {

             return current;

       }

       //setters

       public void setLocation(Location l)

       {

             this.current = l;

       }

      

       public void setName(String name)

       {

             this.name = name;

       }

      

       //toString()

       public String toString()

       {

             return("Traveler[name ="+name+", id="+id+", current = "+current+"]");

       }

}

//end of Traveler.java

//SpaceShip.java

public class SpaceShip {

      

       private String name;

       private int capacity;

       private Traveler travelers[];

       private int numTravelers;

       private Location current;

       private Location destination;

      

       //constructors

       public SpaceShip()

       {

             name=null;

             current = null;

             destination = null;

             capacity = 10;

             travelers = 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;

             travelers = new Traveler[capacity];

             numTravelers=0;

       }

      

       //getters

       public String getName()

       {

             return name;

       }

      

       public int getCapacity()

       {

             return capacity;

       }

      

       public Location getCurrent()

       {

             return current;

       }

      

       public Location getDestination()

       {

             return destination;

       }

      

       //setters

       public void setName(String name)

       {

             this.name = name;

       }

      

       public void setCurrent(Location current)

       {

             this.current = current;

       }

      

       public void setDestination(Location destination)

       {

             this.destination = destination;

       }

      

       public void setCapacity(int capacity)

       {

             this.capacity = capacity;

       }

      

       // method to board a traveler in the spaceship

       public boolean board(Traveler t)

       {

             if((numTravelers == capacity) || isOnBoard(t))

                    return false;

             else {

                    travelers[numTravelers] = t;

                    numTravelers++;

                    return true;

             }

       }

      

       // method to make the traveler leave the spaceship

       public boolean leave(Traveler t)

       {

             if(isOnBoard(t))

             {

                    for(int i=0;i<numTravelers;i++)

                    {

                           if(travelers[i].getID() == t.getID())

                           {

                                 for(int j=i;j<numTravelers-1;j++)

                                        travelers[j] = travelers[j+1];

                                 numTravelers--;

                                 return true;

                           }

                    }

             }

            

             return false;

       }

      

       // method to check if a traveler is on board

       public boolean isOnBoard(Traveler t)

       {

             for(int i=0;i<numTravelers;i++)

                    if(t.getID() == travelers[i].getID())

                           return true;

             return false;

       }

      

       // method to move the spaceship to destination

       public void move()

       {

             current = destination;

             destination = null;

             for(int i=0;i<numTravelers;i++)

                    travelers[i].setLocation(current);

             numTravelers=0;

       }

      

       // toString()

       public String toString()

       {

             return("SpaceShip[name = "+name+", current ="+current+", destination ="+destination+",capacity = "+capacity+", passengers = "+numTravelers+"]");

       }

}

//end of SpaceShip.java