CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Jan 2011
    Posts
    3

    Post BlueJ Exercise Help

    Hi, I'm quite new at programming and I've been finding the external method calls quite hard to get right in completing this particular exercise...

    Exercise 4.28
    Add a close method to the Auction class. This should iterate over the collection of lots and print out details of all the lots. You can use either a for-each loop (Which I chose to use) or a while loop. Any lot that has had at least one bid for it is considered to be sold. For lots that have been sold, the details should include the name of the successful bidder, and the value of the winning bid. For lots that have not been sold, print a message that indicates this fact.

    The Auction code with my close method is:

    Code:
    import java.util.ArrayList;
    public class Auction
    {
        // The list of Lots in this auction.
        private ArrayList<Lot> lots;
        // The number that will be given to the next lot entered
        // into this auction.
        private int nextLotNumber;
    
        /**
         * Create a new auction.
         */
        public Auction()
        {
            lots = new ArrayList<Lot>();
            nextLotNumber = 1;
        }
    
        /**
         * Enter a new lot into the auction.
         * @param description A description of the lot.
         */
        public void enterLot(String description)
        {
            lots.add(new Lot(nextLotNumber, description));
            nextLotNumber++;
        }
    
        /**
         * Show the full list of lots in this auction.
         */
        public void showLots()
        {
            for(Lot lot : lots) {
                System.out.println(lot.toString());
            }
        }
        
        /**
         * Bid for a lot.
         * A message indicating whether the bid is successful or not
         * is printed.
         * @param number The lot number being bid for.
         * @param bidder The person bidding for the lot.
         * @param value  The value of the bid.
         */
        public void bidFor(int lotNumber, Person bidder, long value)
        {
            Lot selectedLot = getLot(lotNumber);
            if(selectedLot != null) {
                boolean successful = selectedLot.bidFor(new Bid(bidder, value));
                if(successful) {
                    System.out.println("The bid for lot number " +
                                       lotNumber + " was successful.");
                }
                else {
                    // Report which bid is higher.
                    Bid highestBid = selectedLot.getHighestBid();
                    System.out.println("Lot number: " + lotNumber +
                                       " already has a bid of: " +
                                       highestBid.getValue());
                }
            }
        }
    
        /**
         * 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 = 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;
            }
        }
        
        public void close(){
            for (Lot lotLoopVariable : lots ){
                System.out.println(lotLoopVariable);
                if(){
                    
                }
                
            }
        }
            
        
    }
    There is also a Person, Lot and Bid class...The person and bid classes are linked and the lot produces the highest bid...but I don't know how to get that information and display it in the close method...

    Person class

    Code:
    public class Person
    {
        // The name of this person.
        private final String name;
    
        /**
         * Create a new person with the given name.
         * @param name The person's name.
         */
        public Person(String name)
        {
            this.name = name;
        }
    
        /**
         * @return The person's name.
         */
        public String getName()
        {
            return name;
        }
    }
    Bid class

    Code:
    public class Bid
    {
        // The person making the bid.
        private final Person bidder;
        // The value of the bid. This could be a large number so
        // the long type has been used.
        private final long value;
    
        /**
         * Create a bid.
         * @param bidder Who is bidding for the lot.
         * @param value The value of the bid.
         */
        public Bid(Person bidder, long value)
        {
            this.bidder = bidder;
            this.value = value;
        }
    
        /**
         * @return The bidder.
         */
        public Person getBidder()
        {
            return bidder;
        }
    
        /**
         * @return The value of the bid.
         */
        public long getValue()
        {
            return value;
        }
    }
    Lastly the Lot class

    Code:
    public class Lot
    {
        // A unique identifying number.
        private final int number;
        // A description of the lot.
        private String description;
        // The current highest bid for this lot.
        private Bid highestBid;
    
        /**
         * Construct a Lot, setting its number and description.
         * @param number The lot number.
         * @param description A description of this lot.
         */
        public Lot(int number, String description)
        {
            this.number = number;
            this.description = description;
        }
    
        /**
         * Attempt to bid for this lot. A successful bid
         * must have a value higher than any existing bid.
         * @param bid A new bid.
         * @return true if successful, false otherwise
         */
        public boolean bidFor(Bid bid)
        {
            if((highestBid == null) ||
                   (bid.getValue() > highestBid.getValue())) {
                // This bid is the best so far.
                highestBid = bid;
                return true;
            }
            else {
                return false;
            }
        }
        
        /**
         * @return A string representation of this lot's details.
         */
        public String toString()
        {
            String details = number + ": " + description;
            if(highestBid != null) {
                details += "    Bid: " + 
                           highestBid.getValue();
            }
            else {
                details += "    (No bid)";
            }
            return details;
        }
    
        /**
         * @return The lot's number.
         */
        public int getNumber()
        {
            return number;
        }
    
        /**
         * @return The lot's description.
         */
        public String getDescription()
        {
            return description;
        }
    
        /**
         * @return The highest bid for this lot.
         *         This could be null if there is
         *         no current bid.
         */
        public Bid getHighestBid()
        {
            return highestBid;
        }
    }
    Any help would be much appreciated!

    Thanks.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: BlueJ Exercise Help

    In your close method, lotLoopVariable holds a reference to a Lot object. You can call any of the object's methods, such as the getHighestBid() method to get the reference to the associated Bid object and then call the appropriate method in that object to get a reference to the associated Person object.

    You call methods in other objects by using the variable name, a '.' and the name of the method.

    For example to call the getName() method in a Person Oobject reference stored in the myObject variable and store the returned value in myName variable use:
    Code:
     
    String myName = myObject.getName();
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jan 2011
    Posts
    3

    Unhappy Re: BlueJ Exercise Help

    Hi Keang,

    Thanks for your reply! I'm sorry for this, but when I tried :

    Code:
     public void close()
        {
            for (Lot lotLoopVariable : lots ){
                System.out.println(lotLoopVariable);
                int highest = Lot.getHighestBid();            
            }
        }
    ...in my Auction class, the compiler returned the error: non-static method getHighestBid() cannot be referenced from a static context.

    I think...that that's because the getHighestBid is of type Bid:
    Code:
    //From the Lot Class
    public Bid getHighestBid()
        {
            return highestBid;
        }
    (I'm currently using the Object First with Java book which is very bad at explaining enough in order to be able to answer the exercises! )

    I don't understand what the "myObject" is in your example code, in my case would the Lot that I used...be correct? However when I tried even that...I got the same static error..

    Code:
    String myName = myObject.getName();

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: BlueJ Exercise Help

    ...in my Auction class, the compiler returned the error: non-static method getHighestBid() cannot be referenced from a static context.
    That's because 'Lot' is the class name and not the name of a variable of type Lot. You can only use the class name to access static methods and variables.
    I think...that that's because the getHighestBid is of type Bid:
    No but that is also a problem. You have to assign the return value to something of the correct type ie of type Bid.
    I don't understand what the "myObject" is in your example code, in my case would the Lot that I used...be correc
    myObject was just an example of a variable of type Person. It has nothing to do with your code.

    You need to use a variable that reference objects of type Lot. Hint: look at the for each loop.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2011
    Posts
    3

    Re: BlueJ Exercise Help

    Sorry for the late reply.

    Thank you for your help I've finally gotten the hang of the dot notation! (Calling methods from objects etc.)

    Code:
     public void close()
        {
            for (Lot lot : lots ){
                
               if (!(lot == null)) {
                   Bid bid = lot.getHighestBid();
                   Person person = bid.getBidder();
                   System.out.println(person.getName());
               }
                     
                           
            }
        }

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured