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

In java how would you write a boolean method that has an if statement checking f

ID: 3750909 • Letter: I

Question

In java how would you write a boolean method that has an if statement checking for an object, the paramaters are below:

boolean board(Traveler t) – travelers board the spaceship

1. If spaceship is not full (at capacity) and the traveler is not already on board a. add traveler to array b. increment numOfTravelers c. return true.

2. If spaceship is full or traveler already on board, return false.

3. Travelers are exclusively identified by their ID!  

boolean isOnBoard(Traveler t) – checks if a traveler is on-board.

1. returns true if passenger on-board

2. returns false is passenger not on-board.

3. Called a Predicate method (Hint may be used in others methods above to keep code modular)

MY CODE:

import java.util.*;

public class SpaceShip

{

private String name;

private int capacity;

private int numOfTravelers = 0;

Traveler []travelers;

Location destination;

Location current;

SpaceShip()

{

this.name = null;

this.current = null;

this.destination = null;

this.capacity = 10;

this.travelers = new Traveler[capacity];

}

SpaceShip(String name, Location current, Location destination,

int capacity)

{

this.name = name;

this.current = current;

this.destination = destination;

this.capacity = capacity;

this.travelers = new Traveler[capacity];

}

public void setName(String name)

{

this.name = name;

}

public void setCurrent()

{

this.current = current;

}

public void setDestination()

{

this.destination = destination;

}

public void setCapacity()

{

this.capacity = capacity;

}

boolean board(Traveler t)

{

if(//TODO Check for travelers on board

//TODO Add travelers to array if capacity isn't full

)

{

this.travelers[numOfTravelers] = t;

}

return true;//place holder

}

boolean leave()

{

//If traveler is on-board, in the array, remove them and return true.

//2. If not on-board, do nothing and return false.

return true;//place holder

}

boolean isOnBoard(Traveler t)

{

if() //is traverler on board

{

//if yes return true

}else {

return false;

}

}

void move()

{

}

public String toString()

{

return "name"; //place holder

}

}

Explanation / Answer

NOTE: The above methods have been modified based on the given information in the question. Due care has been taken to complete the methods with the given info, even then it may be possible that program may produce incorrect results in that case you are requested to post the remaining classes such as Traveler.java, Location.java, with other required information in another question post to get the exact results. But as per the information given in this question, all the requirements have been completed.

Modified SpaceShip class with required methods
  import java.util.*;    public class SpaceShip  {      private String name;      private int capacity;      private int numOfTravelers = 0;      Traveler []travelers;      Location destination;      Location current;        SpaceShip()      {          this.name = null;          this.current = null;          this.destination = null;          this.capacity = 10;          this.travelers = new Traveler[capacity];      }        SpaceShip(String name, Location current, Location destination,              int capacity)      {          this.name = name;          this.current = current;          this.destination = destination;          this.capacity = capacity;          this.travelers = new Traveler[capacity];      }        public void setName(String name)      {          this.name = name;      }        public void setCurrent()      {          this.current = current;      }        public void setDestination()      {          this.destination = destination;      }        public void setCapacity()      {          this.capacity = capacity;      }        boolean board(Traveler t)      {          // checking whether traveler is already on space ship          // or not          for(int i = 0; i < numOfTravelers; i++)          {              if(t.getID == travelers[i].getID)              {                  return false;              }          }            if(numOfTravelers < capacity-1)          {                this.travelers[numOfTravelers] = t;              numOfTravelers++;              return true;//return true if boarding is successful          }          return false; // else return false if boarding is not successful      }        boolean leave(Traveler t)      {          //If traveler is on-board, in the array, remove them and return true.          //2. If not on-board, do nothing and return false.          boolean check = false;          int index = 0;          for(int i = 0; i < numOfTravelers; i++)          {              if(t.getID == travelers[i].getID)              {                  travelers[i] = null;                  numOfTravelers--;                  check = true;                  index = i;              }          }          // checking whether the traveler was removed or not          // if yes then shift the travelers in the array to left           // to remove the gap after removing the traveler          if(check)          {              Traveler temp = travelers[index];              int i;              for(i = index+1; i < numOfTravelers+1; i++)               {                   travelers[i-1] = travelers[i];              }              travelers[i-1] = temp;          }          return check;      }        boolean isOnBoard(Traveler t)      {          for(int i = 0; i < numOfTravelers; i++)          {              if(t.getID == travelers[i].getID)              {                  return true;              }          }          return false;      }        void move()      {        }        public String toString()      {          return "name"; //place holder      }    }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote