Help with objects and arrays...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davem11677
    New Member
    • Jul 2006
    • 1

    #1

    Help with objects and arrays...

    I hope someone can help.

    I am trying to write a Java program that will pass an Int-type, String-type and an object into an array that will loop 100 times, filling it with the items in parentheses. I have no clue how to accomplish this as I am brand new to Java and to programming in general. The last method is the one I need help filling. Please help!!

    public class Locator
    {
    // array sizes (and limits)
    private final static int STORE_LIMIT = 100;
    private final static int POSTAL_ZONE_LIM IT = 100;

    // instance variables
    Store [] storeArray;
    PostalZone [] postalZoneArray ;
    private int nextStore;
    private int nextPostalZone;

    /**
    * Constructor for objects of class Locator.
    */
    public Locator()
    {
    storeArray = new Store[STORE_LIMIT];
    postalZoneArray = new PostalZone[POSTAL_ZONE_LIM IT];
    }

    /**
    * addStore - add a store to the array of stores.
    *
    * @param inputStoreNumbe r int Store number to add
    * @param inputDescriptio n String Store description to add
    * @param inputLocation String Location of store
    *
    * @return int index of added store, -1 if unable to
    * add (array already full)
    */

    public int addStore(int inputStoreNumbe r, String inputDescriptio n,
    Location inputLocation)
    {


    }

    TIA,
    Dave
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    First, thanks for actually having a nice concise question, although you could have used [ code] and [/code ] tags (with no spaces between the brackets) to preserve whitespace.

    I don't see where Store is defined, I'm assuming something like this:
    Code:
    class Store
    {
      private int num;
      private String desc;
      private String loc;
    
      Store(int number, String description, String location)
      {
        num = number; 
        desc = description;
        loc = location;
      }
      ...
    }
    Code:
    /**
    * addStore - add a store to the array of stores.
    *
    * @param inputStoreNumber int Store number to add
    * @param inputDescription String Store description to add
    * @param inputLocation String Location of store
    *
    * @return int index of added store, -1 if unable to
    * add (array already full)
    */
    
    public int addStore(int inputStoreNumber, String inputDescription, Location inputLocation)
    {
      if(nextStoreIndex == STORE_LIMIT)
        return -1;
      storeArray[nextStoreIndex++] = new Store(inputStoreNumber, inputDescription, inputLocation);
      return (nextStoreIndex-1);
    }

    Comment

    Working...