Question: I NEED help understanding how to declare an instance variable of type Map that will map Items to GameObjects. Initialize the map in a constructor
I NEED help understanding how to declare an instance variable of type Map that will map Items to GameObjects. Initialize the map in a constructor using a HashMap putting the following Game objects in it (WOLF, GOOSE, BEANS, FARMER). Then use that map to be placed in 3 different methods: getName, getLocation and getItem.
Here is ALL of the necessary code:
GameEngine.java
package river;
public class GameEngine {
public enum Item {
WOLF, GOOSE, BEAN, FARMER;
}
public enum Location {
START, FINISH, BOAT;
}
private GameObject wolf;
private GameObject goose;
private GameObject bean;
private GameObject farmer;
private Location currentLocation;
public GameEngine() {
wolf = new Wolf();
goose = new Goose();
bean = new Beans();
farmer = new Farmer();
currentLocation = Location.START;
}
public String getName(Item id) {
switch (id) {
case WOLF:
return wolf.getName();
case GOOSE:
return goose.getName();
case BEAN:
return bean.getName();
default:
return farmer.getName();
}
}
public Location getLocation(Item id) {
switch (id) {
case WOLF:
return wolf.getLocation();
case GOOSE:
return goose.getLocation();
case BEAN:
return bean.getLocation();
default:
return farmer.getLocation();
}
}
public String getSound(Item id) {
switch (id) {
case WOLF:
return wolf.getSound();
case GOOSE:
return goose.getSound();
case BEAN:
return bean.getSound();
default:
return farmer.getSound();
}
}
public Location getCurrentLocation() {
return currentLocation;
}
GameObject
| package river; |
| import river.GameEngine.Location; |
| public class GameObject { |
| protected String name; |
| protected Location location; |
| public String getName() { |
| return name; |
| } |
| public void setName(String name) { |
| this.name = name; |
| } |
| public Location getLocation() { |
| return location; |
| } |
| public void setLocation(Location loc) { |
| this.location = loc; |
| } |
| public String getSound() { |
| switch (name) { |
| case "Wolf": |
| return "Howl"; |
| case "Goose": |
| return "Honk"; |
| default: |
| return ""; |
| } |
| } |
| } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
