Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In java how would I write the following? move method following modification 1. boolean method 1. returns true if moved allowed 2. returns false if

In java how would I write the following?

move method

following modification

1. boolean method

1. returns true if moved allowed

2. returns false if moved not allowed.

2. Calls addFlightToHistory for all travelers on board

4. public String toString()

1. returns a nicely formatted String (see below) that

represents the Spaceship object information as well as

a list of all Travelers.

2. Display a list of all Travelers on the flight (hint call the

toString on each Travelers object in the array)

Spaceship [name=Behemoth, current=RING, destination=EARTH, capacity=20,

travelers=10, tripLength = 20]

Crew [name=Klaes Ashford, id=3, current=BELT, position = Captain, flight hours = 6728]

Passenger [name=Klaes Ashford, id=3, current=BELT, seat=FIRST, cost = $1499.99,

rewardsPoints = 3459]

my code:

import java.util.*;

public class Spaceship

{

private String name;

private int capacity;

private int numTraverlers;

private ArrayList travelers;

private Location current;

private Location destination;

private int tripLength;

public Spaceship()

{

this.name = null;

this.current = null;

this.destination = null;

this.tripLength = 0;

this.capacity = 10;

this.travelers = new ArrayList<>(capacity);

}

public Spaceship(String name)

{

this.name = name;

this.current = null;

this.destination = null;

this.tripLength = 0;

this.capacity = 10;

this.travelers = new ArrayList<>(capacity);

}

public Spaceship(String name, Location current, int capacity)

{

this.name = name;

this.current = null;

this.destination = null;

this.tripLength = 0;

this.capacity = 10;

this.travelers = new ArrayList<>(capacity);

}

public Spaceship(String name, int capacity, Location current, Location destination, int tripLength)

{

this.name = name;

this.capacity = capacity;

this.current = current;

this.destination = destination;

this.tripLength = tripLength;

travelers = new ArrayList<>(capacity);

}

public String getName()

{

return name;

}

public int getCapacity()

{

return capacity;

}

public Location getCurrent()

{

return current;

}

public Location getDestination()

{

return destination;

}

public int getTripLength()

{

return tripLength;

}

public void setTripLength(int tripLength)

{

this.tripLength = tripLength;

}

public void setName(String name)

{

this.name = name;

}

public void setCapacity(int capacity)

{

this.capacity = capacity;

}

public void setCurrent(Location current)

{

this.current = current;

}

public void setDestination(Location destination)

{

this.destination = destination;

}

public boolean board(Traveler t)

{

if(numTraverlers

{

travelers.add(t);

numTraverlers++;

return true;

}

return false;

}

public boolean leave(Traveler t)

{

if(isOnBoard(t))

{

travelers.remove(t);

numTraverlers--;

return true;

}

return false;

}

public boolean isOnBoard(Traveler t)

{

if(travelers.contains(t))

{

return true;

}

return false;

}

public void move()

{

current = destination;

destination = null;

for(Traveler t:travelers)

{

t.setLocation(current);

}

travelers.clear();

}

public String toString()

{

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

{

}

return System.out.printf("Spaceship [name = %s, current = " + + ", destination = " + destination +

", capacity = " + capacity + ", passengers = " + numTraverlers + ", trip length = " + tripLength + "]", getName(),);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

What is topology? Explain with examples

Answered: 1 week ago

Question

2. Write two or three of your greatest weaknesses.

Answered: 1 week ago