The Artima Developer Community
Sponsored Link

Java Answers Forum
How do I call methods from the driver class ?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Stan Dominski

Posts: 5
Nickname: stanley
Registered: Nov, 2002

How do I call methods from the driver class ? Posted: Nov 7, 2002 4:25 AM
Reply to this message Reply
Advertisement
Dear People,
In doing "Objects First with Java Chapter 4 page 90 an Auction
program. Even though I have used the bidFor() method my showLots()
method indicates "no bids"

below is the output I received, the TryAuction driver, Auction class,
Bid class , Lot class, and Person class.

Thank you in advance
Stan


// Here is the output that indicates my logic is lacking

//The first lot for sale: bicycle
//The second lot for sale: lamp
//The third lot for sale: trailer
//Lot number: 3 (trailer) already has a bid of: 900460

//1: A bicycle in so so condition
// (No bid)
//2: A brand new lamp
// (No bid)
//3: A trailer built in 2001
// (No bid)

package stan_bluej_ch4p90;
//Purpose of project: To demonstrate collections of objects
//Version: 2001.05.31
//How to start this project:
// Create an Auction object.
// Enter a few lots via its enterLot method. Only String
// descriptions of the lots are required.
// Create one or more Person objects to represent bidders.
// Show the lots and select one to bid for.
// Get the required Lot onto the object bench.
// Enter a bid for the lot, passing the Person who is
// bidding to the bidFor method.

public class TryAuction
{
public static void main(String[] args)
{
//create the auction
Auction cityAuction = new Auction();

//create the lots for sale
Lot bicycle = new Lot(1,"bicycle");
Lot lamp = new Lot(2, "lamp");
Lot trailer = new Lot(3, "trailer");

//enter the lots into the city Auction
cityAuction.enterLot("A bicycle in so so condition");
cityAuction.enterLot("A brand new lamp");
cityAuction.enterLot("A trailer built in 2001");

//show the lots
System.out.println();
System.out.println("The first lot for sale: " + bicycle.getDescription());
System.out.println("The second lot for sale: " + lamp.getDescription());
System.out.println("The third lot for sale: " + trailer.getDescription());

//create the people who will bid for the lots
Person Steve = new Person("Steve");
Person Maria = new Person("Maria");


//create the people's bids
Bid mariaLampBid = new Bid(Maria, 460);
Bid steveLampBid = new Bid(Steve, 510);


//create the people's bids
Bid mariaBicycleBid = new Bid(Maria, 1460);
Bid steveBicycleBid = new Bid(Steve, 1510);

//create the people's bids
Bid mariaTrailerBid = new Bid(Maria, 900460);
Bid steveTrailerBid = new Bid(Steve, 700510);

//give the bids
lamp.bidFor(Maria,460);
lamp.bidFor(Steve,510);

bicycle.bidFor(Maria,1460);
bicycle.bidFor(Steve,1510);

trailer.bidFor(Maria, 900460);
trailer.bidFor(Steve, 700510);

System.out.println();
cityAuction.showLots();
}
}

package stan_bluej_ch4p90;
import java.util.*;
/**
* A simple model of an auction.
* The auction maintains a list of lots of arbitrary length.
* @author David J. Barnes and Michael Kolling.
* @version 2001.06.08
*/
public class Auction
{
// The list of Lots in this auction.
private ArrayList 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();
nextLotNumber = 1;
}

/**
* Enter a new lot into the auction.
* Lots can only by entered into the auction by an
* Auction object.
* @param description A description of the lot.
*/
public void enterLot(String description)
{
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}

/**
* Show the full list of lot numbers and lot descriptions in
* this auction. Include any details of the highest bids.
*/
public void showLots()
{
Iterator it = lots.iterator();
while(it.hasNext()) {
Lot lot = (Lot) it.next();
System.out.println(lot.getNumber() + ": " +
lot.getDescription());
// Include any details of a highest bid.
Bid highestBid = lot.getHighestBid();
if(highestBid != null) {
System.out.println(" Bid: " +
highestBid.getValue());
}
else {
System.out.println(" (No bid)");
}
}
}

/**
* Return the lot with the given number. Return null
* if a lot with this number does not exist.
* @param number The number of the lot to return.
*/
public Lot getLot(int number)
{
if((number >= 1) && (number < nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = (Lot) lots.get(number-1);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != number) {
System.out.println("Internal error: " +
"Wrong lot returned. " +
"Number: " + number);
}
return selectedLot;
}
else {
System.out.println("Lot number: " + number +
" does not exist.");
return null;
}
}
}

package stan_bluej_ch4p90;
/**
* A class to model an item (or set of items) in an
* auction: a lot.
* @author David J. Barnes and Michael Kolling.
* @version 2001.06.08
*/
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 bidder Who is bidding.
* @param value The value of the bid.
*/
public void bidFor(Person bidder, long value)
{
// We trust that lot is genuine. There is nothing to
// prevent a spurious lot from being bid for, but it
// would not appear in the auction list.
if((highestBid == null) ||
(highestBid.getValue() < value)) {
// This bid is the best so far.
setHighestBid(new Bid(bidder, value));
}
else {
System.out.println("Lot number: " + getNumber() +
" (" + getDescription() + ")" +
" already has a bid of: " +
highestBid.getValue());
}
}

/**
* @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 are no current bids.
*/
public Bid getHighestBid()
{
return highestBid;
}

/**
* @param highestBid The new highest bid.
*/
private void setHighestBid(Bid highestBid)
{
this.highestBid = highestBid;
}
}

package stan_bluej_ch4p90;
/**
* A class that models an auction bid. The bid contains a reference
* to the Lot bid for and the user making the bid.
* @author David J. Barnes and Michael Kolling.
* @version 2001.05.31
*/
public class Bid
{
// The user 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;
}
}

package stan_bluej_ch4p90;
/**
* Maintain details of someone who participates in an auction.
* @author David J. Barnes and Michael Kolling.
* @version 2001.05.31
*/
public class Person
{
// The name of this user.
private final String name;

/**
* Create a new user with the given name.
* @param name The user's name.
*/
public Person(String name)
{
this.name = name;
}

/**
* @return The user's name.
*/
public String getName()
{
return name;
}
}

Topic: java hanging (ghost) process Previous Topic   Next Topic Topic: Java homework help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use