Named
, Entity
and Location
. an entity can be moved around to different locations, while locations can be moved to and away from.Direction
, which provides the four major compass directions.
MovingEntity
, which is an entity which can move around to different locations and prints messages describing its progress.
x, y
coordinate pair as the entity moves around a map.
EnterableArray
class, which is a glorified ArrayList
which can be treated as a location.
Room
class, which represents a room in a house. A number of entities can move around the house at the same time.
main
method will not be tested; you may use it any way you want.private
or protected
. This will be verified manually.
public interface Named
The Named
interface consists of objects which have been given a name. It is comprised of one single method,
public String getName()
lots of different things can have a name. This interface could represent any of them.
public interface Entity extends Named
An Entity
is something which can move around from place to place. It has a name which it has inherited from Named
. In normal usage, setting a location would first involve calling setLocation with null
(in order to leave the previous location), and then immediately set location again with the new location.
public void setLocation(Location loc)
typically this method would cause the entity to leave a previous location (if one was defined) and update the current location..
public Location getLocation()
report back the current location of the entity.
Entity
has a couple of methods which have Location
as a parameter or return type? It's ok for two interfaces to depend on one another.
Location: (5p)public interface Location extends Named
A location has a name, but it can also be entered or left by an Entity
. Furthermore, it can have a set of neighbors, which can be returned as a list.
public void enter(Entity e)
enter the Location
; this may involve informing the Entity
that it's been added, and possibly recording the presence of the entity.
public boolean leave(Entity e)
the opposite of enter, this may involve informing the
entity that it's left, and possibily removing the entity from some list; also returns true
if the removal was successful.
public Location[] getNeighbors()
returns a list of Locations
which represent neighbors of the current location.
public enum Direction
This is an enumeration which represents the four compass directions, north, south, east, and west. The actual members of the enumeration will be N
, S
, E
, and W
(in that order), but they will each have an overridden toString()
method which holds the string values "north"
, "south"
, "east"
, and "west"
.
private Direction(String name)
initializes the element of the enumeration using the given name.
@Override public String toString()
returns the name of the direction (i.e. "north"
).
public class MovingEntity implements Entity
Now we actually implement our entity, an actor which moves around to different locations. This entity has a name and a location (the location may be null
if it isn't anywhere at the moment). The entity also has the capability to move from one place to another, see what its neighbors are. It is also a talkative entity, so whenever it moves to another place, it will announce the fact, by printing to system output.
public MovingEntity(String name)
construct a new entity with the given name and a null
location.
@Override public String getName()
what is the entity's name?
@Override public void setLocation(Location loc)
set a new location for the entity; in addition to simply setting the new location, this should do two other things: first, if the entity is currently at a location, it must leave()
that location (see the Location.leave(Entity)
method, and beware of creating an infinite loop); second after it sets a new location, it should announce the fact by printing the following line:
entity_name moves to location_name
@Override public Location getLocation()
where is the entity right now?
public Location[] getNeighbors()
an entity's neighbors are the list of locations which are immediately neighboring the entity's current location. What is the entity's list of neighbors right now? See Location.getNeighbors()
. If the current location is null
, then return an empty list.
public void move(int i)
move the entity to the i
th location in the neighbor's list.
public void move(Direction d)
move the entity in the given direction (assuming that the direction corresponds to list index, i.e. N
is 0, S
is 1, etc). Additionally, the entity should report its movement by printing:
entity_name goes direction
@Override public String toString()
returns the same result as getName()
MovingEntity
relies on the setLocation()
method to update its knowledge about its current location. The method spec warns that doing this incorrectly can result in an infinite loop. How is an infinite loop possible? When we enter(e)
or leave(e)
a new location, it is responsible for notifying the entity (using a call to setLocation(loc)
) of its new location information. Meanwhile, when an entity's location is updated using setLocation(loc)
it may be required to leave(e)
an old location during the call. The fact that leave
may call setLocation
and setLocation
may call leave creates a potential for a loop. However, when leave
calls setLocation
, the location passed is null
. You can use this fact to help avoid calling back and forth forever.
MapLocation: (15p)public class MapLocation implements Location
This is a location on a map, expressed as a 2D pair of integer coordinates, x and y. If you begin at position (0, 0)
in the map, then moving north would take you to (0, 1)
, east would take you to (1, 0)
, etc. Each set of coordinates is its own location, and there can be any number of coordinates, so don't rely on pre-allocating all possible coordinates.
private final int x
the x-coordinate.
private final int y
public MapLocation(int x, int y)
public int getX()
public int getY()
@Override public String getName()
this should produce the same output as toString()
(see below).
@Override public void enter(Entity e)
simply inform the entity (see entity.setLocation()
) that it has entered this location.
@Override public boolean leave(Entity e)
inform the entity that it has left the location (i.e. set the location to null
) and return true
; beware of infinite loops!
@Override public Location[] getNeighbors()
create and return a list of this location's neighbors. The neighbors are the locations one coordinate to the north, south, east, and west (in that order).
@Override public String toString()
returns the coordinates of the location as an ordered pair; so for example, if x
is 2 and y
is 4, then this method will return "(2, 4)"
.
public class EnterableArray extends ArrayList<Entity> implements Location
Here we start to see why it makes sense for Location
to be an interface. An EnterableArray
is just an ArrayList
of Entity
elements, but a list is a place where an element can go, so it's possible to treat it as a location. This class just extends the ArrayList
class by adding the necessary methods which will make it behave like a Location
. Since we've already chosen a base class, we wouldn't be able to do this without using an interface.
public EnterableArray()
doesn't need to do anything special.
@Override public void enter(Entity e)
add the Entity
to the list, but be sure to notify the entity that it's been added.
@Override public boolean leave(Entity e)
remove the entity from the list, returning whether it was successful or not; don't forget to notify the entity that it's been removed (by setting its location to null
@Override public String getName()
returns the string "EnterableArray"
.
@Override public Location[] getNeighbors()
returns a list with zero elements.
public class Room implements Location
A Room
is a room in a house or a building. It has a name, and it has a list of people who occupy it (hint: would inheriting from an EnterableArray
be useful here?). Furthermore, it has a list of links to all of the other rooms which it is connected to (hint: another ArrayList
?). Moving from one room to another would typically involve picking from the neightboring rooms. The entity which leaves a room is no longer part of the list of people in that room, and is now part of the list of people in the new room.
public Room(String name)
construct a room with the given name.
@Override public void enter(Entity e)
the reason for this override is because we should now also print who's in the room every time a new entity enters. The message would look like (ArrayList
's toString()
is suitable for printing the list of entities):
room_name now contains list_of_entities
@Override public String getName()
what is the name of this room?
@Override public Location[] getNeighbors()
which rooms are neighbors of this room? Note that this should be returned as a list of Location
, which is not the same as an ArrayList
!
public static void link(Room r1, Room r2)
create a link between the two rooms r1
and r2
, so that each one becomes a neighbor of the other one.
> MovingEntity ent = new MovingEntity("nobody");
> ent.getNeighbors()
{ }
> MapLocation map = new MapLocation(0, 0)
> map.enter(ent);
nobody moves to (0, 0)
> ent.getNeighbors()
{ (0, 1), (0, -1), (1, 0), (-1, 0) }
> ent.move(Direction.N);
nobody goes north
nobody moves to (0, 1)
> ent.move(Direction.N);
nobody goes north
nobody moves to (0, 2)
> ent.move(Direction.E);
nobody goes east
nobody moves to (1, 2)
> ent.move(Direction.S);
nobody goes south
nobody moves to (1, 1)
> ent.move(Direction.W);
nobody goes west
nobody moves to (0, 1)
> ent.move(Direction.W);
nobody goes west
nobody moves to (-1, 1)
> MovingEntity a1 = new MovingEntity("actor 1");
> MovingEntity a2 = new MovingEntity("actor 2");
> MovingEntity a3 = new MovingEntity("actor 3");
> EnterableArray elist = new EnterableArray();
> elist.enter(a1);
actor 1 moves to EnterableArray
> elist.enter(a2);
actor 2 moves to EnterableArray
> elist.enter(a3);
actor 3 moves to EnterableArray
> MovingEntity a1 = new MovingEntity("actor 1");
> MovingEntity a2 = new MovingEntity("actor 2");
> MovingEntity a3 = new MovingEntity("actor 3");
> Room r1 = new Room("Room 1");
> Room r2 = new Room("Room 2");
> Room r3 = new Room("Room 3");
> Room.link(r1,r2);
> Room.link(r2,r3);
> Room.link(r3,r1);
> r1.enter(a1);
actor 1 moves to Room 1
Room 1 now contains [actor 1]
> r2.enter(a2);
actor 2 moves to Room 2
Room 2 now contains [actor 2]
> r3.enter(a3);
actor 3 moves to Room 3
Room 3 now contains [actor 3]
> a1.getNeighbors() // note that Room.toString() prints its entity list rather than its name; because of this, it looks like it's printing actors when it is really printing rooms.
{ [actor 2], [actor 3] }
> a1.move(0);
actor 1 moves to Room 2
Room 2 now contains [actor 2, actor 1]
> a2.move(1);
actor 2 moves to Room 3
Room 3 now contains [actor 3, actor 2]
> a1.move(1);
actor 1 moves to Room 3
Room 3 now contains [actor 3, actor 2, actor 1]
> a3.move(0);
actor 3 moves to Room 2
Room 2 now contains [actor 3]
Submission instructions are as follows.
xxx_yyyyyyyy_P4/
ID.txt
in the format shown below, containing your name, userid, G#, lecture section
and lab section, and add it to the directory. Your directory should contain nine files
total.
Full Name: Donald Knuth