Description
- The objective of this project is to apply interfaces to design several classes which are similar in outward behavior but very different in nature. This project will involve implementing several different types of location (a location on a map, or in a room of a house, or abstractly inside a list).
Overview:- Create the three primary interfaces used in this project,
Named
,Entity
andLocation
. an entity can be moved around to different locations, while locations can be moved to and away from. - Create an enumeration,
Direction
, which provides the four major compass directions. - Create the class
MovingEntity
, which is an entity which can move around to different locations and prints messages describing its progress. - Create the location class MapLocation, which tracks an
x, y
coordinate pair as the entity moves around a map. - Create an
EnterableArray
class, which is a glorifiedArrayList
which can be treated as a location. - Create a
Room
class, which represents a room in a house. A number of entities can move around the house at the same time. - Document all public methods, classes, interfaces and enumerations using JavaDoc style comments.
- You may import any standard Java library if you see the need.
- The
main
method will not be tested; you may use it any way you want. - All fields must be declared
private
orprotected
. This will be verified manually. - You may write your own helper methods, but any methods which are specifically asked for must match exactly (i.e. capitalization of the method name; number and order of the parameters).
- Any class, interface, and enumeration, as well as any public method, must be documented using JavaDoc style of comments.
public interface Named
TheNamed
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
AnEntity
is something which can move around from place to place. It has a name which it has inherited fromNamed
. In normal usage, setting a location would first involve calling setLocation withnull
(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 haveLocation
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 anEntity
. Furthermore, it can have a set of neighbors, which can be returned as a list.public void enter(Entity e)
enter theLocation
; this may involve informing theEntity
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 returnstrue
if the removal was successful.public Location[] getNeighbors()
returns a list ofLocations
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 beN
,S
,E
, andW
(in that order), but they will each have an overriddentoString()
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 actor which moves around to different locations. This actor has a name and a location (the location may benull
if it isn't anywhere at the moment). The actor also has the capability to move from one place to another, see what its neighbors are. It is also a talkative actor, so whenever it moves to another place, it will announce the fact, by printing to system output.public MovingEntity(String name)
construct a new actor with the given name and anull
location.@Override public String getName()
what is the actor's name?@Override public void setLocation(Location loc)
set a new location for the actor; in addition to simply setting the new location, this should do two other things: first, if the actor is currently at a location, it mustleave()
that location (see theLocation.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 actor right now?public Location[] getNeighbors()
what is the entity's list of neighbors right now (seeLocation.getNeighbors()
)? If the current location isnull
, then return an empty list.public void move(int i)
move the actor to thei
th location in the neighbor's list.public void move(Direction d)
move the actor 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
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 astoString()
(see below).@Override public void enter(Entity e)
simply inform the entity (seeentity.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 tonull
) and returntrue
; 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, ifx
is 2 andy
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 forLocation
to be an interface. AnEnterableArray
is just anArrayList
ofEntity
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 theArrayList
class by adding the necessary methods which will make it behave like aLocation
. 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 theEntity
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 tonull
@Override public String getName()
returns the string"EnterableArray"
.@Override public Location[] getNeighbors()
returns a list with zero elements.
public class Room implements Location
ARoom
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 anEnterableArray
be useful here?). Furthermore, it has a list of links to all of the other rooms which it is connected to (hint: anotherArrayList
?). 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
'stoString()
is suitable for printing the list of entities):entity_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 ofLocation
, which is not the same as anArrayList
!public static void link(Room r1, Room r2)
create a link between the two roomsr1
andr2
, so that each one becomes a neighbor of the other one.
All of your public methods, classes, interfaces, and enumerations must be documented using JavaDoc-style comments. This includes declaring method parameters and return types. - Create the three primary interfaces used in this project,