I
io
Hi --
I'm reading the Object First With BlueJ Book (this is not homework -
self-taught). I have to iterate through an ArrayList object. This
object represents where I store auction lots. I have to go through
that collection and check if an object has _not_ been sold, in which
case I should store in another ArrayList and return that.
The solution I came up with is this, which works (I also post the other relevvant classes for your reference):
public ArrayList getUnsold()
{
ArrayList unsold;
new unsold();
Iterator it = lots.iterator();
while(it.hasNext()) {
Lot lot = (Lot) it.next();
// if a highestBid wasn't made, then the lot wasn't sold at
the auction.
Bid highBid = lot.getHighest();
if( highBid == null ) {
unsold.add(new Lot(lot.getNumber(), lot.getDescription()));
}
}
return unsold;
}
My question is why I *have* to use "new" in:
unsold.add(new Lot(lot.getNumber(), lot.getDescription()));
instead of
unsold.add(Lot(lot.getNumber(), lot.getDescription()));
which result in an error?
As I understand this, I already had a lot object. Why "new"?
Under the "if" block is there a new lexical scope, and what you effectively have is then a copy of the object created in the line
Lot lot = (Lot) it.next();
Where can I read more about this?
TIA
PM
//////////////////
// Lot class
/////////////////
public class Lot
{
private final int number;
private String description;
private Bid highestBid;
private Person bidMaker;
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 are no current bids.
*/
public Bid getHighestBid()
{
return highestBid;
}
public String getPerson()
{
return bidMaker.getName();
}
}
/////////////////
// Bid
/////////////////
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;
}
}
I'm reading the Object First With BlueJ Book (this is not homework -
self-taught). I have to iterate through an ArrayList object. This
object represents where I store auction lots. I have to go through
that collection and check if an object has _not_ been sold, in which
case I should store in another ArrayList and return that.
The solution I came up with is this, which works (I also post the other relevvant classes for your reference):
public ArrayList getUnsold()
{
ArrayList unsold;
new unsold();
Iterator it = lots.iterator();
while(it.hasNext()) {
Lot lot = (Lot) it.next();
// if a highestBid wasn't made, then the lot wasn't sold at
the auction.
Bid highBid = lot.getHighest();
if( highBid == null ) {
unsold.add(new Lot(lot.getNumber(), lot.getDescription()));
}
}
return unsold;
}
My question is why I *have* to use "new" in:
unsold.add(new Lot(lot.getNumber(), lot.getDescription()));
instead of
unsold.add(Lot(lot.getNumber(), lot.getDescription()));
which result in an error?
As I understand this, I already had a lot object. Why "new"?
Under the "if" block is there a new lexical scope, and what you effectively have is then a copy of the object created in the line
Lot lot = (Lot) it.next();
Where can I read more about this?
TIA
PM
//////////////////
// Lot class
/////////////////
public class Lot
{
private final int number;
private String description;
private Bid highestBid;
private Person bidMaker;
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 are no current bids.
*/
public Bid getHighestBid()
{
return highestBid;
}
public String getPerson()
{
return bidMaker.getName();
}
}
/////////////////
// Bid
/////////////////
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;
}
}