I have all figured out I think except exercise 7.20 Extend zuul by adding an Class called item. Each item is created with a description such as rock or stick and a weight. When creating rooms and setting their exits, items for this game should also be created. When player enters a room information about an item present in this room should be displayed.

import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
 * Item class creates an item to go into a room
 * 
 * @author (Carolyn Leithner) 
 * @version (2/10/2011)
 */

// Exercise 7.20
public class Item
{
    // instance variables - replace the example below with your own
    private String description;
    private int weight;
    //private HashMap items;
    // a constant array that holds all items
    // exercise 7.20
    private ArrayList<Item> items;
       /**
         * Constructor for objects of class Item
         * Create an item described "description", and Weight
         */
    public Item(String description, int weight)
    {
        // initialise instance variables
        description = description;
        weight = weight;
        this.items = new ArrayList<Item>();

    }

    /**
     * Return the description of the item
     */
    public String getShortDescription()
    {
        return description;
    }

    /**
     * Return the weight of the item
     */
    public int weight()
    {
        return weight;
    }

    /**
     * Return a long description of the item that includes the description 
     * and weight.
     */
    public String getLongDescription()
    {
        return "Item " + description + " Weight " + weight;
    }
    // exercise 7.20
    //     /**
    //      * add an item to the room when it is created
    //      */
    //     public void addItem(String description,int weight)
    //     {
    //         items.put(new Item (description, weight));
    //     }
    /**
     * create a variety of items
     */
    public void add(Item aItem)
    {
        items.add(aItem);
    }

    /**
     * get the item
     */
    public ArrayList<Item> getItems()
    {
        return items;
    }

    /**
     * 
     */
    public String getItemString()
    {
        String returnString = "Item:";

        {
            returnString += " " + items;
        }
        return returnString;
    }

}
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
 *  This is the Zuul-Starter file used for the Homework.  Please add the 
 *  appropriate class comments below
 *  
 *  @author: Carolyn Leithner
 *  @version: Feb 10,2011 
 */

public class Room 
{
    public String description;
    private HashMap<String, Room> exits;
    /*
    private Room northExit;
    private Room southExit;
    private Room eastExit;
    private Room westExit;
     */
    // exercise 7.20
    private ArrayList<Item> items;
    //private HashMap<String, Item> items;
    private String item;
    /**
     * Create a room described "description". Initially, it has
     * no exits. "description" is something like "a kitchen" or
     * "an open court yard".
     * @param description The room's description.
     */
    public Room(String description) 
    {
        this.description = description;
        exits = new HashMap<String, Room>();
        //addItem(items);
        //items = new ArrayList<String, Room>();
    }

    // starter file

    /**
     * Define an exit from this room.
     * @param direction The direction of the exit.
     * @param neighbor  The room to which the exit leads.
     */
    public void setExit(String direction, Room neighbor) 
    {
        exits.put(direction, neighbor);
    }

    /**
     * @return The description of the room.
     */
    public String getDescription()
    {
        return description;
    }

    public Room getExit(String direction) {
        return exits.get(direction);
        /*
        if(direction.equals("north")) {
        return northExit;
        }
        if(direction.equals("east")) {
        return eastExit;
        }
        if(direction.equals("south")) {
        return southExit;
        }
        if(direction.equals("west")) {
        return westExit;
        }
        return null;
         */
    }

    /**
     * Return a string describing the room's exits, for example
     * "Exits: north west".
     * @return Details of the room's exits.
     */
    public String getExitString()
    {
        /*
        String exitString = "Exits: ";
        if(northExit != null)
        exitString += "north ";
        if(eastExit != null)
        exitString += "east ";
        if(southExit != null)
        exitString += "south ";
        if(westExit != null)
        exitString += "west ";
        return exitString;
         */

        String returnString = "Exits:";
        Set<String> keys = exits.keySet();
        for(String exit : keys) {
            returnString += " " + exit;
        }
        return returnString;
    }

    //exercise 7.11
    /**
     * Return a long description of this room, of the form:
     *  You are in the kitchen
     *  Exits: north west
     *  @return A description of the room, including exits.
     */
    public String getLongDescription()
    {
        return "You are " + description +".\n" + getExitString() + ".\n";
       // + "Items in Room" + getItemString();
    }
    //     // exercise 7.20
    /**
     * add an item to the room when it is created
     */
    public void addItem(Item item)
    {
        addItem (item);
    }
}
/**
 *  This is the Zuul-Starter file used for the Homework.  Please add the 
 *  appropriate class comments below
 *  
 *  @author:
 *  @version:
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;   
    private Item item;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, theatre, pub, lab, office, cellar;

        // create the rooms
        outside = new Room("outside the main entrance of the university");
        theatre = new Room("in a lecture theatre");
        pub = new Room("in the campus pub");
        lab = new Room("in a computing lab");
        office = new Room("in the computing admin office");
        cellar = new Room("in the cellar");
        // initialise room exits
        //outside.setExits(null, theatre, lab, pub);
        outside.setExit("east", theatre);
        outside.setExit("south", lab);
        outside.setExit("west", pub);
        //theatre.setExits(null, null, null, outside);
        theatre.setExit("west", outside);
        //pub.setExits(null, outside, null, null);
        pub.setExit("east", outside);
        //lab.setExits(outside, office, null, null);
        lab.setExit("north", outside);
        lab.setExit("east", office);
        //office.setExits(null, null, null, lab);
        office.setExit("west", lab);
        office.setExit("down", cellar);

        cellar.setExit("up", office);

        currentRoom = outside;  // start game outside
        

    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.

        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("Thank you for playing.  Good bye.");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("Welcome to the World of Zuul!");
        System.out.println("World of Zuul is a new, incredibly boring adventure game.");
        System.out.println("Type 'help' if you need help.");
        System.out.println();
        printLocationInfo();
        /*
        System.out.println("You are " + currentRoom.getDescription());
        System.out.print("Exits: ");
        if(currentRoom.northExit != null) {
        System.out.print("north ");
        }
        if(currentRoom.eastExit != null) {
        System.out.print("east ");
        }
        if(currentRoom.southExit != null) {
        System.out.print("south ");
        }
        if(currentRoom.westExit != null) {
        System.out.print("west ");
        }
        System.out.println();
         */
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        String commandWord = command.getCommandWord();
        if (commandWord.equals("help"))
            printHelp();
        else if (commandWord.equals("go"))
            goRoom(command);
        else if (commandWord.equals("quit"))
            wantToQuit = quit(command);
        // exercise 7.14
        else if (commandWord.equals("look"))
            look();

        return wantToQuit;
    }

    // starter file

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("You are lost. You are alone. You wander");
        System.out.println("around at the university.");
        System.out.println();
        System.out.println("Your command words are:");
        // exercise 7.16
        parser.showCommands();
        System.out.println("   go quit help");
    }

    /** 
     * Try to go to one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("Go where?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.getExit(direction);
        /*
        if(direction.equals("north")) {
        nextRoom = currentRoom.northExit;
        }
        if(direction.equals("east")) {
        nextRoom = currentRoom.eastExit;
        }
        if(direction.equals("south")) {
        nextRoom = currentRoom.southExit;
        }
        if(direction.equals("west")) {
        nextRoom = currentRoom.westExit;
        }
         */
        if (nextRoom == null) {
            System.out.println("There is no door!");
        }
        else {
            currentRoom = nextRoom;
            printLocationInfo();
            /*
            System.out.println("You are " + currentRoom.getDescription());
            System.out.print("Exits: ");
            if(currentRoom.northExit != null) {
            System.out.print("north ");
            }
            if(currentRoom.eastExit != null) {
            System.out.print("east ");
            }
            if(currentRoom.southExit != null) {
            System.out.print("south ");
            }
            if(currentRoom.westExit != null) {
            System.out.print("west ");
            }
            System.out.println();
             */
        }
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    //  exercise 7.14

    private void look()
    {
        System.out.println(currentRoom.getLongDescription());
    }

    public void printLocationInfo() {
        //System.out.println("You are " + currentRoom.getDescription());
        //System.out.println(currentRoom.getExitString());
        //exercise 7.11
        System.out.println(currentRoom.getLongDescription());
        /*
        System.out.print("Exits: ");
        if(currentRoom.getExit("north") != null) {
        System.out.print("north ");
        }
        if(currentRoom.getExit("east") != null) {
        System.out.print("east ");
        }
        if(currentRoom.getExit("south") != null) {
        System.out.print("south ");
        }
        if(currentRoom.getExit("west") != null) {
        System.out.print("west ");
        }
         */
        System.out.println();
    }
}

Recommended Answers

All 9 Replies

Sorry that was so long but I wanted to include as much of the code as I thought was needed for this issue. I think I'm putting some method calls into the wrong class.

In the Item constructor when you do this: description = description; You are not initializing the attributes of the class. The description is the same as the one of the argument because it is declared locally as an argument.
You need this: this.description = description; With "this" you say put the argument description in the description of the class. Do the same for the other.

Replace the getLongDescription() method with the toString method. The toString method is inherited from the Object class.

public String toString() {
  return "Item " + description + " Weight " + weight;
}

So when you do this:

Item it = new Item();
System.out.println(it);

The toString method you overridden is automatically called.


That is exactly what happens when you do this: returnString += " " + items; The toString method of the ArrayList class is automatically called.


Also you need to put the ArrayList in a different class. Every time you create a new instance you also create a new ArrayList. But you need to add all of tour items
in one list. So you are going to keep doing (new Item()) and then add all of those to a different item?

Remove the ArrayList from the Item class and do this:

class Items {
  private ArrayList<Item> list = null;
  
  public Items() {
     list = new ArrayList<Item>();
  }
  // add methods
  // get methods
  
  // And of course the toString method:
public String toString() {
  return "Items: "+list;
}
}

In the Room class you define an ArrayList of items. That is unnecessary, since you already have the Items class.
Also you have an addItem method that calls itself. That you will give an OutOfMemoryException.
You should probably take the Item argument and add it to the list of items that you have.

Try to see if you can move around the rooms and test the play method. Then after you are done you can add items

still getting a stack overflow error when trying to add said item to room. Any suggestions.

still getting a stack overflow error when trying to add said item to room. Any suggestions.

Have you made any changes to your code? Can you post the latest full version?

confused on the this.description = description. I thought with strings you must use .equals in lew of = .??? Also I've posted some updated code for game, room and item class again. I've tried making changes and following other examples but seem to always get a stack overflow error when I run the game. Also can't get for example office.addItem(item1) or office.add(Item item1) to add anything to the room or print it out.

import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
 * Item class creates an item to go into a room
 * 
 * @author (Carolyn Leithner) 
 * @version (2/10/2011)
 */

// Exercise 7.20
public class Item
{
    // instance variables - replace the example below with your own
    private String description;
    private int weight;
    //private HashMap items;
    // a constant array that holds all items
    // exercise 7.20
    //private ArrayList<Item> items;
    private ArrayList<Item> list;
    /**
     * Constructor for objects of class Item
     * Create an item described "description", and Weight
     */
    public Item(String description, int weight)
    {
        // initialise instance variables
        this.description = description;
        weight = weight;
        this.list = new ArrayList<Item>();
        //Item item = new Item();
        addItem();

    }

    /**
     * Return the description of the item
     */
    public String getShortDescription()
    {
        return description;
    }

    /**
     * Return the weight of the item
     */
    public int weight()
    {
        return weight;
    }

    /**
     * Return a long description of the item that includes the description 
     * and weight.
     */
    public String toString()
    {
        return "Item " + description + " Weight " + weight;
       // return "Item: " + list;
    }
    // exercise 7.20
    /**
     * add an item to the room when it is created
     */
    public void addItem(String description,int weight)
    {
        list.add(new Item (description, weight));
    }


    /**
     * create a variety of items
     */
    public void add(Item aItem)
    {
        list.add(aItem);
    }

    /**
     * get the item
     */
    public ArrayList<Item> getItems()
    {
        return list;
    }

    /**
     * 
     */
    public String getItemString()
    {
        
        String returnString = "Item:";

        {
            returnString += " " + list;
        }
        return returnString;
       
    }
    /**
     * 
     */
    public void addItem()
    {
        Item item1 = new Item("rock ", 3);
        Item item2 = new Item("book ", 4);
        Item item3 = new Item("pen ", 1);
        Item item4 = new Item("stick ", 2);
        Item item5 = new Item("beaker ", 1);
        Item item6 = new Item("phone " , 2);
//       this.list.add(new Item ("rock", 3)); 
//       this.list.add(new Item ("phone", 2));
//       this.list.add(new Item ("pen", 1));
//       this.list.add(new Item ("beaker", 1));
//       this.list.add(new Item ("stick", 2));
//       this.list.add(new Item ("book", 1));
    }
}

import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
 *  This is the Zuul-Starter file used for the Homework.  Please add the 
 *  appropriate class comments below
 *  
 *  @author: Carolyn Leithner
 *  @version: Feb 10,2011 
 */

public class Room 
{
    public String description;
    private HashMap<String, Room> exits;
    /*
    private Room northExit;
    private Room southExit;
    private Room eastExit;
    private Room westExit;
     */
    // exercise 7.20
    //private ArrayList<Item> items;
    //private HashMap<String, Item> items;
    private String item;
    /**
     * Create a room described "description". Initially, it has
     * no exits. "description" is something like "a kitchen" or
     * "an open court yard".
     * @param description The room's description.
     */
    public Room(String description) 
    {
        this.description = description;
        exits = new HashMap<String, Room>();
        //addItem();
        //items = new ArrayList<String, Room>();
    }

    // starter file

    /**
     * Define an exit from this room.
     * @param direction The direction of the exit.
     * @param neighbor  The room to which the exit leads.
     */
    public void setExit(String direction, Room neighbor) 
    {
        exits.put(direction, neighbor);
    }

    /**
     * @return The description of the room.
     */
    public String getDescription()
    {
        return description;
    }

    public Room getExit(String direction) {
        return exits.get(direction);
        /*
        if(direction.equals("north")) {
        return northExit;
        }
        if(direction.equals("east")) {
        return eastExit;
        }
        if(direction.equals("south")) {
        return southExit;
        }
        if(direction.equals("west")) {
        return westExit;
        }
        return null;
         */
    }

    /**
     * Return a string describing the room's exits, for example
     * "Exits: north west".
     * @return Details of the room's exits.
     */
    public String getExitString()
    {
        /*
        String exitString = "Exits: ";
        if(northExit != null)
        exitString += "north ";
        if(eastExit != null)
        exitString += "east ";
        if(southExit != null)
        exitString += "south ";
        if(westExit != null)
        exitString += "west ";
        return exitString;
         */

        String returnString = "Exits:";
        Set<String> keys = exits.keySet();
        for(String exit : keys) {
            returnString += " " + exit;
        }
        return returnString;
    }

    //exercise 7.11
    /**
     * Return a long description of this room, of the form:
     *  You are in the kitchen
     *  Exits: north west
     *  @return A description of the room, including exits.
     */
    public String getLongDescription()
    {
        return "You are " + description +".\n" + getExitString() + ".\n";
       // + "Items in Room" + getItemString();
    }
    //     // exercise 7.20
//     /**
//      * add an item to the room when it is created
//      */
//     public void addItem(Item item)
//     {
//         addItem (item);
//     }
}

/**
 *  This is the Zuul-Starter file used for the Homework.  Please add the 
 *  appropriate class comments below
 *  
 *  @author:
 *  @version:
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;   
    private Item item1;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, theatre, pub, lab, office, cellar;

        // create the rooms
        outside = new Room("outside the main entrance of the university");
        theatre = new Room("in a lecture theatre");
        pub = new Room("in the campus pub");
        lab = new Room("in a computing lab");
        office = new Room("in the computing admin office");
        cellar = new Room("in the cellar");
        // initialise room exits
        //outside.setExits(null, theatre, lab, pub);
        outside.setExit("east", theatre);
        outside.setExit("south", lab);
        outside.setExit("west", pub);
        //theatre.setExits(null, null, null, outside);
        theatre.setExit("west", outside);
        //pub.setExits(null, outside, null, null);
        pub.setExit("east", outside);
        //lab.setExits(outside, office, null, null);
        lab.setExit("north", outside);
        lab.setExit("east", office);
        //office.setExits(null, null, null, lab);
        office.setExit("west", lab);
        office.setExit("down", cellar);
            
        cellar.setExit("up", office);
        Item item1 = new Item("rock ", 3);
        Item item2 = new Item("book ", 4);
        Item item3 = new Item("pen ", 1);
        Item item4 = new Item("stick ", 2);
        Item item5 = new Item("beaker ", 1);
        Item item6 = new Item("phone " , 2);
        
        currentRoom = outside;  // start game outside
        

    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.

        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("Thank you for playing.  Good bye.");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("Welcome to the World of Zuul!");
        System.out.println("World of Zuul is a new, incredibly boring adventure game.");
        System.out.println("Type 'help' if you need help.");
        System.out.println();
        printLocationInfo();
        /*
        System.out.println("You are " + currentRoom.getDescription());
        System.out.print("Exits: ");
        if(currentRoom.northExit != null) {
        System.out.print("north ");
        }
        if(currentRoom.eastExit != null) {
        System.out.print("east ");
        }
        if(currentRoom.southExit != null) {
        System.out.print("south ");
        }
        if(currentRoom.westExit != null) {
        System.out.print("west ");
        }
        System.out.println();
         */
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        String commandWord = command.getCommandWord();
        if (commandWord.equals("help"))
            printHelp();
        else if (commandWord.equals("go"))
            goRoom(command);
        else if (commandWord.equals("quit"))
            wantToQuit = quit(command);
        // exercise 7.14
        else if (commandWord.equals("look"))
            look();

        return wantToQuit;
    }

    // starter file

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("You are lost. You are alone. You wander");
        System.out.println("around at the university.");
        System.out.println();
        System.out.println("Your command words are:");
        // exercise 7.16
        parser.showCommands();
        System.out.println("   go quit help");
    }

    /** 
     * Try to go to one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("Go where?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.getExit(direction);
        /*
        if(direction.equals("north")) {
        nextRoom = currentRoom.northExit;
        }
        if(direction.equals("east")) {
        nextRoom = currentRoom.eastExit;
        }
        if(direction.equals("south")) {
        nextRoom = currentRoom.southExit;
        }
        if(direction.equals("west")) {
        nextRoom = currentRoom.westExit;
        }
         */
        if (nextRoom == null) {
            System.out.println("There is no door!");
        }
        else {
            currentRoom = nextRoom;
            printLocationInfo();
            /*
            System.out.println("You are " + currentRoom.getDescription());
            System.out.print("Exits: ");
            if(currentRoom.northExit != null) {
            System.out.print("north ");
            }
            if(currentRoom.eastExit != null) {
            System.out.print("east ");
            }
            if(currentRoom.southExit != null) {
            System.out.print("south ");
            }
            if(currentRoom.westExit != null) {
            System.out.print("west ");
            }
            System.out.println();
             */
        }
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    //  exercise 7.14

    private void look()
    {
        System.out.println(currentRoom.getLongDescription());
    }

    public void printLocationInfo() {
        //System.out.println("You are " + currentRoom.getDescription());
        //System.out.println(currentRoom.getExitString());
        //exercise 7.11
        System.out.println(currentRoom.getLongDescription());
        /*
        System.out.print("Exits: ");
        if(currentRoom.getExit("north") != null) {
        System.out.print("north ");
        }
        if(currentRoom.getExit("east") != null) {
        System.out.print("east ");
        }
        if(currentRoom.getExit("south") != null) {
        System.out.print("south ");
        }
        if(currentRoom.getExit("west") != null) {
        System.out.print("west ");
        }
         */
        System.out.println();
    }
}

In the constructor you do this:

public Item(String description, int weight) {
  this.description = description;
  weight = weight;
}

When you do weight = weight; you simply take the argument and you put its value to the argument.
When you do this.description = description; you take the argument and you assign it to the class variable.

Then you completely ignored my advices. You don't need to put the ArrayList in the Item class. The item has weight and description. IT doesn't have an items list. You need to put that in a separate class.

And whenever you create a new instance of the Item class you call the addItem() method. Why? You need to create the item and then add that item to the list.
That list needs to be in a separate class, so when you create many items all of them will be put in one single list.

What you do is: Call the constructor, which calls the addITem, which calls many constructors, each of them call the addItem() method again, which each addItem methods create new instances, which each instance calls again the addItem, .......
And so on and so on.

Remove the list from the Item class. (As already told and didn't listen)
Put those items in one list. That list must not be in the Item class because each time you create an Item instance you create a new list.

ArrayList<Item> list = new ArrayList<Item>();

Item item1 = new Item("rock ", 3);
Item item2 = new Item("book ", 4);
Item item3 = new Item("pen ", 1);

list.add(item1);
list.add(item2);
list.add(item3);

The above code can be either in a single class called Items that is like this:

class Items {
  private ArrayList<Item> list = null;

  public Items() {
    list = new ArrayList<Item>();
  }

  public void addItem(Item item) {
     list.add(item);
  }

  // get methods
}

You will have one Items class and you will add your items to that class, or you can put this code in the main method as an initialization of the list:

public void static main(String [] args) {
ArrayList<Item> list = new ArrayList<Item>();

Item item1 = new Item("rock ", 3);
Item item2 = new Item("book ", 4);
Item item3 = new Item("pen ", 1);

list.add(item1);
list.add(item2);
list.add(item3);

// then use the list
}

You know we are learning and some of us older folks have a harder time. I did read your message and when the thing still was not working I tried others. Thanks for your help but don't degrade some one unless you know that they aren't a 18 year old not trying.

You know we are learning and some of us older folks have a harder time. I did read your message and when the thing still was not working I tried others. Thanks for your help but don't degrade some one unless you know that they aren't a 18 year old not trying.

I would like to apologize and I had no intention of insulting anyone but I don't believe that my post was so aggressive. Still, if you felt that way, you have my apologies and I will be more careful in the future.

Many times have I seen my advices completely ignored and posters wondering why their code doesn't work. It is always a good thing when people come in this forum with the will to learn and show effort.
It is a good think that you try out a things on your own. I hope that you understood from my previous post why you shouldn't mix the list with the class Item.

And from the rest of your code, even if I don't know what you are trying to accomplish, maybe you should forget the suggestion of creating a new class for the Items list.

Just have the Item class with only attributes the description, weight, get/set methods and in the Room class put the list. That list would be like any other attribute:

public class Room {
   public String description;
   private HashMap<String, Room> exits;
   private ArrayList<Item> items;
   ..........

You can have a method in the Room class that sets the items list. Like the setExits method.
In the createRooms method you can create multiple lists with items in the same way you create multiple Rooms.

Thank you, yes I'm trying very hard but at 42 and having learned on dos the programming of java is very strict on what will happen when. I post here if I can't get what I'm trying, to work. Thanks for your help and if you see a post from me, I do print out all suggestions and anything I find relevant. I try coding in many different ways until I can get it to work the way my professor wants it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.