- 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).
- Create the three primary interfaces used in this project,
Location. 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, ycoordinate pair as the entity moves around a map.
- Create an
EnterableArrayclass, which is a glorified
ArrayListwhich can be treated as a location.
- Create a
Roomclass, 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.
We often run into situations where we can think of a lot of different things which behave in a very similar way at some level, but which are in fact very different in nature. Consider a location – something which you can go to or leave. The idea is a simple one, but it can apply to many different types of things. From where you are inside your home, to coordinates on a GPS map, to a spot on a game board. For these situations, it may make sense to use interfaces. Interfaces allow us to preserve some level of compatibility between different objects, without constraining them to be too similar. We may even want to derive from different existing classes, which would preclude us from using most other approaches besides interfaces. In this project, we create two related interfaces, representing entities and locations, and use them to produce several different types of location-based classes.
- You may import any standard Java library if you see the need.
mainmethod will not be tested; you may use it any way you want.
- All fields must be declared
protected. 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.
Named: ( 5p)
public interface NamedThe
Namedinterface 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 NamedAn
Entityis 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.
Entityhas a couple of methods which have
Locationas a parameter or return type? It’s ok for two interfaces to depend on one another. Location: (5p)
public interface Location extends NamedA 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
Entitythat 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
trueif the removal was successful.
public Location getNeighbors()returns a list of
Locationswhich represent neighbors of the current location.
public enum DirectionThis is an enumeration which represents the four compass directions, north, south, east, and west. The actual members of the enumeration will be
W(in that order), but they will each have an overridden
toString()method which holds the string values
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.
public class MovingEntity implements EntityNow we actually implement our actor which moves around to different locations. This actor has a name and a location (the location may be
nullif 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 a
@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 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 actor right now?
public Location getNeighbors()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 actor to the
ith 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.
Sis 1, etc). Additionally, the entity should report its movement by printing:
entity_name goes direction
public class MapLocation implements LocationThis 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 xthe 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
@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
xis 2 and
yis 4, then this method will return
public class EnterableArray extends ArrayList<Entity> implements LocationHere we start to see why it makes sense for
Locationto be an interface. An
EnterableArrayis just an
Entityelements, 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
ArrayListclass 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
Entityto 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
@Override public String getName()returns the string
@Override public Location getNeighbors()returns a list with zero elements.
public class Room implements LocationA
Roomis 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
EnterableArraybe 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 (
toString()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 of
Location, which is not the same as an
public static void link(Room r1, Room r2)create a link between the two rooms
r2, 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,
Unemployedprofessor.net guarantee you peace of mind, gives the value of your money, and trust for us to help you with your assignments. We assure you of original work that meets your academic and project’s needs. We at Unemployedprofessor.net have a mark of quality, professionalism, and timely delivery of given projects.
+1 (940) 905 5542