Jump to content
xisto Community

Java Exercise Help I need help to understand this question

Recommended Posts

Ok at my Uni they teach you OOP with java, They used to do with C++ i believe but swiched to java because its easier to teach people OOP with java apparently.


Anyway I've been working on some exercises with java (book is 'objects first with java'). I've got this one exercise which I cant understand.


Exercise 4.29


Rewrite getLot so that it does not rely on a lot with a particular number being stored at index (number - 1) in the collection. For instance, if lot number 2 has been removed, then lot number 3 will have been moved from index 2 to index 1, and all higher lot numbers will also have been moved one index position. You may assume that lots are always stored increasing order of their lot number.

this is the existing getLot method

/**     * Return the lot with the given number. Return null     * if a lot with this number does not exist.     * @param lotNumber The number of the lot to return.     */    public Lot getLot(int lotNumber)    {        if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {            // The number seems to be reasonable.            Lot selectedLot = (Lot) 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: " +                                   "Wrong lot returned. " +                                   "Number: " + lotNumber);            }            return selectedLot;        }        else {            System.out.println("Lot number: " + lotNumber +                               " does not exist.");            return null;        }    }

I dont get what the exercise is asking me to do :) any help apprecaited
Edited by sonesay (see edit history)

Share this post

Link to post
Share on other sites

Does it say anywhere above that what lots exactly is? I don't see where it came from or what it is.The only thing I can gather from the question is that they want you to make lots a dynamic list rather than a static array so that you can remove and add in the middle without having to shift everything after it. Though, I don't see anywhere in the getLot method where they are actually shifting anything... so I don't see how they want you to change it.Sorry I couldn't be of more help. I hate it when I have questions like those to do. :)

Share this post

Link to post
Share on other sites

Lots is like an item being sold at the auction house. sorry I didnt want to paste the entire code/classes its too long :). I think I've found the solution now on the net. The website for the book is so cheap they got text based discussions lol. I hate those its so hard to read and find info.

anyway heres the sulotion I found in PDF format. it only covers chapter 4 of the book. if anyone else needs it.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.