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
Martians 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 Martians 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
invaders to this MartianManager that have
power greater than the average power of all Martians 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
Martians 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
Teleporters 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)
Martians in colony to this MartianManagercolony - List of Martians to be added to this MartianManagerpublic 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 teleportersm - The Martian to be addedtrue if m was added to martians; otherwise, falseprivate double averagePower()
public ArrayList<Martian> battle(ArrayList<Martian> invaders)
invaders, which battle with the martains resulting
in some Martians 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 Martians are returned in a list and
removed from martians and teleporters (if applicable).invaders - List of Martians who are invading the Martians in martians.Martians 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)
invaders to this MartianManager that have
power greater than the average power of all Martians 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 - Martians that may be added to this MartianManagerMartianManager.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 theNote that it if the ID you are searching for is the ID of a Martian in the MartianManager, it should return the Martian closest to the ID, but not the one that matches. For example, from the example above:Martainwithid=5getMartianClosestTo(7) returns theMartainwithid=8
getMartianClosestTo(5) returns theMartainwithid=3getMartianClosestTo(8) returns theMartainwithid=9
id - Find the closest Martian to this id.Martian in martians that is closest to the input idpublic 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()
Martians 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.Martians.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()
Teleporters 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.Martianpublic 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 nullid - The id of the Martian to remove.Martianpublic 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