Results 1 to 4 of 4
- 06-20-2011, 01:47 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Complicated problem between classes
I have a Room class as follows:
The task is to be able to add items to each room. ieJava Code:[SIZE="1"] import java.util.*; import java.util.Set; public class Room { private String description; private HashMap<String, Room> exits; private ArrayList<Item> items; private HashMap<String,items> rItem; public Room (String description) { this.description=description; exits=new HashMap<String, Room>(); items=new ArrayList<Item>(); rItem=new Hashmap<String,items>(); } public void setExits(Room north, Room east, Room south, Room west) { if (north!=null) exits.put("north", north); if(east !=null) exits.put("east",east); if(south!=null) exits.put("south",south); if(west!=null) exits.put("west",west); } public Room getExit(String direction) { return exits.get(direction); } public void setExit(String direction, Room neighbor){ exits.put(direction, neighbor); } public String getDescription() { return description; } public String getExitString(){ Set<String>keys=exits.keySet(); String r=""; for (String key: keys){ String rt=exits.get(key).getDescription(); r+= " " + rt; } return r; } public String gld(){ return ("" + description.toString() + getExitString()); } } public class Item { private int weight; private String desc; public Item() { weight=0; desc=""; } public int getWeight() { // put your code here return weight; } public String desc(){ return desc; } } [/SIZE]
Create an Item class
Add several items to each room
Unlimited number of items is said to be addable to each Room
Item type must be uncoupled from room so two rooms can have the same item if needed
Exercise suggests that I used a collection in the room class, I was thinking hashmap(String,items) where items is an ArrayList of Item
Here is the code but I get compile time error:
"Cannot find symbol class items"
- 06-20-2011, 01:54 AM #2
items is the name of your variable and not a type(class). For example would you do this:
Why do you need the map anyway. What purpose is the String key?Java Code:String text = "hello"; Integer number = 10; HashMap<text, number> map;
- 06-20-2011, 01:55 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Are these the offending lines?Java Code:private HashMap<String,items> rItem; // ... rItem=new Hashmap<String,items>();
If so, it is because you don't have a class items. The class is Item.
To declare a map of string to lists of items use
You might want to check out the Generics section of Oracle's Tutorial.Java Code:private HashMap<String,ArrayList<Item>> rItem; // or consider private Map<String,List<Item>> rItem;
- 06-20-2011, 01:58 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
replacement for complicated if else?
By TopNFalvors in forum New To JavaReplies: 12Last Post: 04-09-2011, 07:05 PM -
inner classes problem
By smallmos1 in forum New To JavaReplies: 7Last Post: 11-18-2010, 03:07 PM -
Complicated Draw
By Desdenova in forum New To JavaReplies: 9Last Post: 05-27-2010, 08:44 PM -
Complicated Method
By Desmond in forum New To JavaReplies: 5Last Post: 03-17-2010, 11:31 AM -
writng event listners ( seems complicated)
By Basit56 in forum AWT / SwingReplies: 1Last Post: 08-25-2009, 09:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks