Learning Java Help please!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Speckydodge
    New Member
    • Feb 2009
    • 2

    #1

    Learning Java Help please!

    Hi im learning java through blue j and im having trouble doing an exercise if anyone can help that would be great.
    the question is:

    Rewrite the getLot method so it doesn't rely on a lot with a particular number being stored at index (number -1) in the collection.You may assume that lots are always stored in increasing order of their lot number.

    The method is written ass :

    Code:
    public Lot getLot(int lotNumber)
        {
            if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
                // The number seems to be reasonable.
                Lot selectedLot = lots.get(lotNumber - 1);
                // Include a confidence check to be sure we have the
                // right lot.
                if(selectedLot.getNumber() != lotNumber) {
                    System.out.println("Internal error: Lot number " +
                                       selectedLot.getNumber() +
                                       " was returned instead of " +
                                       lotNumber);
                    // Don't return an invalid lot.
                    selectedLot = null;
                }
                return selectedLot;
            }
            else {
                System.out.println("Lot number: " + lotNumber +
                                   " does not exist.");
                return null;
            }
    Any help would be much appreciated again
    Last edited by JosAH; Feb 20 '09, 04:52 PM. Reason: fixed the [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Would a Map<Integer, Lot> be of help? The Integer is the lot number, stored as the key in the Map, the associated value is the Lot itself.

    kind regards,

    Jos

    Comment

    • Speckydodge
      New Member
      • Feb 2009
      • 2

      #3
      maybe but i havent come across maps yet...im just a wee beginner ha :)

      thanks for the help though bud.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Speckydodge
        maybe but i havent come across maps yet...im just a wee beginner ha :)

        thanks for the help though bud.
        Well, then you're stuck with your arrays then. The index number of the array has a functional dependency on the Lot number. There's no way to get rid of it.

        kind regards,

        Jos

        Comment

        Working...