public class MartianManager
extends Object
Martian
objects.Modifier and Type | Field and Description |
---|---|
protected ArrayList<Martian> |
martians
Stores subclass instances of
Martian . |
protected ArrayList<Teleporter> |
teleporters
Stores
Martian instances that implement Teleporter . |
Constructor and Description |
---|
MartianManager() |
Modifier and Type | Method and Description |
---|---|
void |
absorbColony(ArrayList<Martian> colony)
Add the
Martian s in colony to this MartianManager |
boolean |
addMartian(Martian m)
|
private double |
averagePower()
OPTIONAL - Helper method to calculate the average power.
|
ArrayList<Martian> |
battle(ArrayList<Martian> invaders)
This method accepts a list of
invaders , which battle with the martains resulting
in some Martian s being removed from martians and returned in a list. |
ArrayList<Martian> |
battle2(ArrayList<Martian> invaders)
DO NOT WRITE THIS METHOD - It was for an alternate version of the battle method.
|
ArrayList<Martian> |
captureInvaders(ArrayList<Martian> invaders)
Adds all
invader s to this MartianManager that have
power greater than the average power of all Martian s in this
MartianManager . |
boolean |
contains(int id)
Returns
true if martians contains a Martian with
id and false otherwise. |
Martian |
getMartianAt(int i)
|
Martian |
getMartianClosestTo(int id)
|
Martian |
getMartianClosestTo(Martian martian)
|
Martian |
getMartianWithId(int id)
Returns the
Martian with id or null if not found. |
int |
getNumMartians()
Returns the number of Martains in
martians . |
int |
getNumTeleporters()
Returns the number of Teleporters in
teleporters . |
ArrayList<Martian> |
getSortedMartians()
Returns a list of
Martian s sorted according to their id . |
Teleporter |
getTeleporterAt(int i)
|
String |
groupSpeak()
Returns a string representing all the
martians speaking. |
String |
groupTeleport(String dest)
Returns a string representing all the
teleporters teleporting to dest . |
void |
obliterateTeleporters()
Removes all
Teleporter s from teleporters and martians . |
Martian |
removeMartianAt(int loc)
Removes the
Martian at index loc from martians and returns it. |
Martian |
removeMartianWithId(int id)
Removes the
Martian with id from martians and returns it. |
String |
toString()
Returns a list of all the
Martians and Teleporters in this exact format: |
protected ArrayList<Martian> martians
Martian
.protected ArrayList<Teleporter> teleporters
Martian
instances that implement Teleporter
.public void absorbColony(ArrayList<Martian> colony)
Martian
s in colony
to this MartianManager
colony
- List of Martian
s to be added to this MartianManager
public boolean addMartian(Martian m)
m
to martians
provide that a Martian
doesn't already exist there with the same id
. Hint: use the contains or indexOf
methods. If the Martian
is also a Teleporter
, it should also be added to teleporters
m
- The Martian
to be addedtrue
if m
was added to martians
; otherwise, false
private double averagePower()
public ArrayList<Martian> battle(ArrayList<Martian> invaders)
invaders
, which battle with the martains
resulting
in some Martian
s being removed from martians
and returned in a list.
The method loops over the invaders
. At each iteration, an invader "kills" the first
Martian
in martians
that has less power
than it does and then
moves on to the next invader. It is possible that an invader does not kill a Martian
,
in which case move on to the next invader. Killed Martian
s are returned in a list and
removed from martians
and teleporters
(if applicable).invaders
- List of Martian
s who are invading the Martian
s in martians
.Martian
s who were killed and removed from martians
and teleporters
(if applicable).public ArrayList<Martian> battle2(ArrayList<Martian> invaders)
invaders
- public ArrayList<Martian> captureInvaders(ArrayList<Martian> invaders)
invader
s to this MartianManager
that have
power
greater than the average power of all Martian
s in this
MartianManager
. The average power is computed once, before the invasion occurs,
i.e. before any invaders have been added. This method should also return a list of all invaders
that were added to this MartianManager
.invaders
- Martian
s that may be added to this MartianManager
MartianManager
.public boolean contains(int id)
true
if martians
contains a Martian
with
id
and false
otherwise. Hint: use a dummy Martian
and the contains method,
example is probably in most recent lab and in notes.id
- public Martian getMartianAt(int i)
i
- public Martian getMartianClosestTo(int id)
martians
that has an id
closest to the input id
. For example, if martians
contains Martian
objects with ids: 3, 5, 8, 9; then:
getMartianClosestTo(6) returns theMartain
withid=5
getMartianClosestTo(7) returns theMartain
withid=8
id
- Find the closest Martian
to this id.Martian
in martians
that is closest to the input id
public Martian getMartianClosestTo(Martian martian)
martians
that has an id
closest to the input Martian
's id
. Hint: this is a convenience method;
it simply needs to call getMartianClosestTo(id:int)/martian
- public Martian getMartianWithId(int id)
Martian
with id
or null if not found. Hint: use
indexOf.id
- public int getNumMartians()
martians
.martians
.public int getNumTeleporters()
teleporters
.teleporters
.public ArrayList<Martian> getSortedMartians()
Martian
s sorted according to their id
.
This method should not alter the order of martians
list instance variable. Hint: just
create a new list of martians that contains the existing ones, sort it, and return.Martian
s.public Teleporter getTeleporterAt(int i)
i
- public String groupSpeak()
martians
speaking. In other words, a string that contains the
returns from each of the martians
having their speak
method called on it, with a line break after each one.public String groupTeleport(String dest)
teleporters
teleporting to dest
. In other words,
a string that contains the returns from each of the teleporters
having their teleport
method
called on it using the argument, dest
, with a line break after each one.dest
- The destination that all teleporters teleport
to.public void obliterateTeleporters()
Teleporter
s from teleporters
and martians
.public Martian removeMartianAt(int loc)
Martian
at index loc
from martians
and returns it.
If it is a Teleporter
then it should be removed fromteleporters
also.
If loc
is invalid, then null
should be returned.loc
- The index in martians
of the Martian
to remove.Martian
public Martian removeMartianWithId(int id)
Martian
with id
from martians
and returns it.
If it is a Teleporter
then it should also be removed from teleporters
.
If no Martian
exists in martians
with id
then return null
id
- The id
of the Martian
to remove.Martian
public String toString()
Martians
and Teleporters
in this exact format:
Martians: Red Martian - id=1 vol=1, ten=1 Red Martian - id=2 vol=2, ten=2 Green Martian - id=3 vol=1 Green Martian - id=4 vol=5 Teleporters: Green Martian - id=3 vol=1 Green Martian - id=4 vol=5
toString
in class Object