Menu

[solved]-Class Thing Thing Encompasses Persons Threats Abstract Class Implements Representable Pass Q39012785

class Thing

Thing encompasses both persons and threats. This abstract classimplements both Representable and Passable (though it relegates allmethods from Passable to child classes).

Manual Inspection Criteria Reminder

  • For this class and its child classes, you need to use theprovided (public) methods defined here when you can, rather thanre-visiting the internal state. As an example, Thing::getLoc(),Thing::setLoc(), and Person::isSafe() should be used when they canrather than trying to re-invent the same computation.

Fields

  • private Coord loc, prevLoc
  • public final String repr
  • protected java.io.PrintStream log
  • protected Map map

Methods

  • public Thing(Coord c, String repr, Map map, PrintStreamlog). Initializes the fields. Both loc and prevLoc are setto the given coordinate c. (This is the only time they’llmatch).
  • public abstract void doAction(). All thingscan do an action. People might move, and threats might spawn orkill. This will be overridden lower in the class hierarchy.
  • public Coord getLoc(). Returns the loc.
  • public Coord getPrevLoc(). Returns theprevLoc.
  • public void setLoc(Coord c). Updates bothprevLoc and loc appropriately.
  • @Override public String repr(). Returns thefield.
  • @Override public String toString(). Returnsthe concatenation of repr() and getLoc().

class Threat

Threat is an abstract class that extends Thing. Threats are thebad things from which people are trying to escape. They spread(spawn) at regular intervals; we store how long it’s been since thelast spawning (via charge), and also at what point they will againspawn (via fullCharge).

Manual Inspection Criteria (2%) : Threat housesmost of teh code related to increasing/resetting charge, andspawning potentially four children in four new locations. Childclasses only define the specifics of spawning and any specialadditional actions during doAction().

Fields

  • protected int charge
  • protected final int fullCharge

Methods

  • public Threat(Coord c, String repr, int fullCharge, Mapmap, PrintStream log). Initializes the fields. chargestarts at 0.
  • public abstract void spawn(Coord c). Childclasses will provide an implementation that spawns (creates) a newthing identical to itself, but at the given c location. During thespawn() method, a child places a copy of the new object on the mapand carries out any further actions such eliminating unfortunatehumans in their path. (do not call doAction here, only actions suchas a StickyIcky immediately killing present people willoccur).
  • @Override public void doAction(). First actionis to increment charge. If it reaches fullCharge, it’s time tospawn! When at full charge, take the following actions.
    • Log a message of the form

      which indicates the type and location of the threat that isspreading/spawning. (Smokes also can spread; ~@(1,8) spreading alsoappropriate).

    • Call the spawn(c) with a Coord for all four cardinal directions(north, east, south, west) from the spawning threat.
    • Only call spawn(c) if the spot canPassThrough() which is truefor non-wall spots.
    • reset charge to zero.
  • Note: Threat doesn’t provide implementations for anymethods from Passable, so those are still the child classes’responsibilities.

class StickyIcky

It has no fields, but has a constructor and overrides variousabstract methods it inherited, in order to be concrete.

Methods

  • public StickyIcky(Coord loc, Map map, PrintStreamlog). Via the parent constructor, sets all fields.fullcharge must always be 4, and the starting value for charge is0.
  • @Override public void spawn(Coord c). Createsa new StickyIcky and adds it to the map at location c. Thefollowing considerations should be made
    • Do not spawn a new sticky in a location that already has one.This will require checking that there is no StickyIcky at thecoordinate c.
    • Generate a log message of the form

      where the location is the position of the new sticky.

    • Any Person at the new spawn location is eliminated. Whenkilling humans, call their die() method. Also generate a logmessage of the following form
  • @Override public void doAction(). Aftercalling the version of doAction inherited from Threat, thisStickyIcky object will check what non-dead persons are present atits location on the map, and kills them (calls their die() method).Since multiple people may be present, mulitple deaths may occur.Whenever a person is killed, a message must be sent to the log,utilizing toString() definitions. Example:
  • @Override public boolean canLookThrough().People can actually look through a spot containing a StickyIcky; itdoesn’t obscure the whole view like a Wall does.
  • @Override public boolean canPassThrough().Return true here. A Person can attempt to pass through a StickyIckybut it will result in death.
  • Suggestion: In several places, StickyIckysmust kill all humans at their location (during spawn() anddoAction(). This suggests a private method to kill all humans atthe sticky’s location is useful. Such a method might help with partof the manual inspection criteria.

Expert Answer


Answer to class Thing Thing encompasses both persons and threats. This abstract class implements both Representable and Passable (… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *