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)

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

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!