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 |
|---|---|
boolean |
addMartian(Martian m)
|
ArrayList<Martian> |
battle(ArrayList<Martian> invaders)
This method accepts a list of
invaders, which are Martians. |
ArrayList<Martian> |
battle2(ArrayList<Martian> invaders)
DO NOT WRITE THIS, It was for an alternate version of the battle method.
|
boolean |
contains(int id)
Returns
true if martians contains a Martians with
id and false otherwise. |
Martian |
getMartianAt(int i)
|
Martian |
getMartianClosestTo(int id)
Returns the Martian in
martians that has an id
closest to the id supplied; Note: “closest” doesn’t mean the next martian,
For example, if you have martians with ids: 3, 4, 7, 8, 9 and you call the method with 5,
then the martian with id=4 should be returned. |
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. |
private int |
getPower(Martian m)
OPTIONAL METHOD - NOT REQUIRED, Returns the power for the input Martian.
|
ArrayList<Martian> |
getSortedMartians()
Returns a sorted list of
Martians according to their id. |
Teleporter |
getTeleporterAt(int i)
|
String |
groupSpeak()
Returns the 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. |
String |
groupTeleport(String dest)
Returns the a string that contains the returns from each
teleporters having their
teleport method called on it using the argument, dest,
with a line break after each one. |
void |
obliterateTeleporters()
Removes all
Teleporters from teleporters and martians. |
Martian |
removeMartian(int id)
Removes the
Martian with id from martians
and from teleporters if it is a Teleporter. |
String |
toString()
Returns a list of all the
Martians and Teleporters in this
format: |
protected ArrayList<Martian> martians
Martian.protected ArrayList<Teleporter> teleporters
Martian instances that implement Teleporter.public boolean addMartian(Martian m)
m to martians if 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 be added to teleportersm - The Martian to be addedtrue if m was added to martians, otherwise falsepublic ArrayList<Martian> battle(ArrayList<Martian> invaders)
invaders, which are Martians.
The method loops over the invaders. At each iteration, an invader "kills" the first Martian
in martians that has less power that it does. It is possible that an invader does not kill a Martian,
in which case we just continue to the next invader.
For GreenMartians, power is simply the volume of a Martian.
For RedMartians, power is volume+tenacity.
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 martianspublic ArrayList<Martian> battle2(ArrayList<Martian> invaders)
invaders - public boolean contains(int id)
true if martians contains a Martians 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 id supplied; Note: “closest” doesn’t mean the next martian,
For example, if you have martians with ids: 3, 4, 7, 8, 9 and you call the method with 5,
then the martian with id=4 should be returned. If you call the method with 7,
then the martian with id=7 should be returned.id - public Martian getMartianClosestTo(Martian martian)
martians that is closest to Martian
martian as measured by 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.private int getPower(Martian m)
m - Martian to return the power for.public ArrayList<Martian> getSortedMartians()
Martians 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.public Teleporter getTeleporterAt(int i)
i - public String groupSpeak()
martians having their speak
method called on it, with a line break after each one.public String groupTeleport(String dest)
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 removeMartian(int id)
Martian with id from martians
and from teleporters if it is a Teleporter. If no martian is
found, return null.id - public String toString()
Martians and Teleporters in this
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