Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please code in Java: Room: ( 15p ) public class Room implements Location A Room is a room in a house or a building. It

Please code in Java:

Room: (15p) 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):

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 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.

Here is some background to help out (You do not need to code anything past this line):

Create the three primary interfaces used in this project, Named, Entity and 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, y coordinate pair as the entity moves around a map.

Create an EnterableArray class, which is a glorified ArrayList 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.

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.

Here is the overview and code for Location:

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.

Code:

public interface Location extends Named { public void enter(Entity e); public boolean leave(Entity e); public Location[] getNeighbors(); }

Here is the overview and code for Entity:

Entity: (5p)

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.

Code:

public interface Entity extends Named {

public void setLocation(Location loc); public Location getLocation();

}

And here is the code for Named:

public interface Named {

public String getName();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Why have benefits grown in strategic importance to employers?

Answered: 1 week ago

Question

Types of cultural maps ?

Answered: 1 week ago

Question

Discuss the various types of leasing.

Answered: 1 week ago

Question

Define the term "Leasing"

Answered: 1 week ago

Question

What do you mean by Dividend ?

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago