Results 1 to 4 of 4
  1. #1
    uhertz is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default Complicated problem between classes

    I have a Room class as follows:
    Java 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]
    The task is to be able to add items to each room. ie
    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"

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    items is the name of your variable and not a type(class). For example would you do this:
    Java Code:
    String text = "hello";
    Integer number = 10;
    HashMap<text, number> map;
    Why do you need the map anyway. What purpose is the String key?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    private HashMap<String,items> rItem;
    // ...
    rItem=new Hashmap<String,items>();
    Are these the offending lines?

    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

    Java Code:
    private HashMap<String,ArrayList<Item>> rItem;
    // or consider
    private Map<String,List<Item>> rItem;
    You might want to check out the Generics section of Oracle's Tutorial.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The task is to be able to add items to each room.
    This suggests that the Room class would have an attribute that is some sort of collection of Item instances. (This is not materially affected by the somewhat unrealistic condition that an Item instance can be in two rooms at once.)

Similar Threads

  1. replacement for complicated if else?
    By TopNFalvors in forum New To Java
    Replies: 12
    Last Post: 04-09-2011, 07:05 PM
  2. inner classes problem
    By smallmos1 in forum New To Java
    Replies: 7
    Last Post: 11-18-2010, 03:07 PM
  3. Complicated Draw
    By Desdenova in forum New To Java
    Replies: 9
    Last Post: 05-27-2010, 08:44 PM
  4. Complicated Method
    By Desmond in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 11:31 AM
  5. writng event listners ( seems complicated)
    By Basit56 in forum AWT / Swing
    Replies: 1
    Last Post: 08-25-2009, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •