JAVA Implement BagInterface
BagInterface.java
BagInterface.java
/**
An interface that describes the operations of a bag of objects.
@author
Frank M. Carrano
@author
Timothy M. Henry
@version
4.1
*/
public
interface
BagInterface
<
T
>
{
/** Gets the current number of entries in this bag.
@return
The integer number of entries currently in the bag. */
public
int
getCurrentSize
();
/** Sees whether this bag is empty.
@return
True if the bag is empty, or false if not. */
public
boolean
isEmpty
();
/** Adds a new entry to this bag.
@param
newEntry The object to be added as a new entry.
@return
True if the addition is successful, or false if not. */
public
boolean
add
(
T newEntry
);
/** Removes one unspecified entry from this bag, if possible.
@return
Either the removed entry, if the removal.
was successful, or null. */
public
T remove
();
/** Removes one occurrence of a given entry from this bag.
@param
anEntry The entry to be removed.
@return
True if the removal was successful, or false if not. */
public
boolean
remove
(
T anEntry
);
/** Removes all entries from this bag. */
public
void
clear
();
/** Counts the number of times a given entry appears in this bag.
@param
anEntry The entry to be counted.
@return
The number of times anEntry appears in the bag. */
public
int
getFrequencyOf
(
T anEntry
);
/** Tests whether this bag contains a given entry.
@param
anEntry The entry to locate.
@return
True if the bag contains anEntry, or false if not. */
public
boolean
contains
(
T anEntry
);
/** Retrieves all entries that are in this bag.
@return
A newly allocated array of all the entries in the bag.
Note: If the bag is empty, the returned array is empty. */
public
T
[]
toArray
();
// public <T> T[] toArray(); // Alternate
// public Object[] toArray(); // Alternate
/** Creates a new bag that combines the contents of this bag
and anotherBag.
@param
anotherBag The bag that is to be added.
@return
A combined bag. */
// public BagInterface<T> union(BagInterface<T> anotherBag);
/** Creates a new bag that contains those objects that occur
in both this bag and anotherBag.
@param
anotherBag The bag that is to be compared.
@return
A combined bag. */
// public BagInterface<T> intersection(BagInterface<T> anotherBag);
/** Creates a new bag of objects that would be left in this bag
after removing those that also occur in anotherBag.
@param
anotherBag The bag that is to be removed.
@return
A combined bag. */
// public BagInterface<T> difference(BagInterface<T> anotherBag);
}
// end BagInterface
__MACOSX/._BagInterface.java
Coin.java
Coin.java
/**
A class that represents a coin.
@author
Frank M. Carrano
@author
Timothy M. Henry
@version
4.0
*/
public
class
Coin
{
private
enum
CoinSide
{
HEADS
,
TAILS
}
private
CoinName
myName
;
private
int
value
;
// in cents
private
int
year
;
// mint year
private
CoinSide
sideUp
;
/** Constructs an object for the coin having a given
value and mint year. The visible side of the new
coin is set at random. */
public
Coin
(
int
coinValue
,
int
mintYear
)
{
switch
(
coinValue
)
{
case
1
:
myName
=
CoinName
.
PENNY
;
break
;
case
5
:
myName
=
CoinName
.
NICKEL
;
break
;
case
10
:
myName
=
CoinName
.
DIME
;
break
;
case
25
:
myName
=
CoinName
.
QUARTER
;
break
;
case
50
:
myName
=
CoinName
.
FIFTY_CENT
;
break
;
case
100
:
myName
=
CoinName
.
DOLLAR
;
break
;
default
:
myName
=
CoinName
.
PENNY
;
break
;
}
// end switch
value
=
coinValue
;
year
=
mintYear
;
sideUp
=
getToss
();
}
// end constructor
/** Constructs an object for the coin having a given
name and mint year. The visible side of the new
coin is set at random. */
public
Coin
(
CoinName
name
,
int
mintYear
)
{
switch
(
name
)
{
case
PENNY
:
value
=
1
;
break
;
case
NICKEL
:
value
=
5
;
break
;
case
DIME
:
value
=
10
;
break
;
case
QUARTER
:
value
=
25
;
break
;
case
FIFTY_CENT
:
value
=
50
;
break
;
case
DOLLAR
:
value
=
100
;
break
;
default
:
value
=
1
;
break
;
}
// end switch
myName
=
name
;
year
=
mintYear
;
sideUp
=
getToss
();
}
// end constructor
/** Returns the name of the coin. */
public
CoinName
getCoinName
()
{
return
myName
;
}
// end getCoinName
/** Returns the value of the coin in cents. */
public
int
getValue
()
{
return
value
;
}
// end getValue
/** Returns the coin's mint year as an integer. */
public
int
getYear
()
{
return
year
;
}
// end getYear
/** Returns "HEADS" if the coin is heads-side up;
otherwise, returns "TAILS". */
public
String
getSideUp
()
{
/*
String result = "Tails";
if (sideUp == CoinSide.HEADS)
result = "Heads";
return result;
*/
return
sideUp
.
toString
();
}
// end getSideUp
/** Returns true if the coin is heads-side up. */
public
boolean
isHeads
()
{
return
sideUp
==
CoinSide
.
HEADS
;
}
// end isHeads
/** Returns true if the coin is tails-side up. */
public
boolean
isTails
()
{
return
sideUp
==
CoinSide
.
TAILS
;
}
// end isTails
/** Tosses the coin; sideUp will be either HEADS or TAILS at random. */
public
void
toss
()
{
sideUp
=
getToss
();
}
// end toss
/** Returns the coin as a string in the form "value/year/side-up". */
public
String
toString
()
{
return
value
+
"/"
+
year
+
"/"
+
sideUp
;
}
// end toString
// Returns a random value of either HEADS or TAILS.
private
CoinSide
getToss
()
{
CoinSide
result
;
if
(
Math
.
random
()
<
0.5
)
result
=
CoinSide
.
HEADS
;
else
result
=
CoinSide
.
TAILS
;
return
result
;
}
// end getToss
}
// end Coin
__MACOSX/._Coin.java
CoinName.java
CoinName.java
public
enum
CoinName
{
PENNY
,
NICKEL
,
DIME
,
QUARTER
,
FIFTY_CENT
,
DOLLAR
}
__MACOSX/._CoinName.java
HowToImportIntoNetBeans.pdf
__MACOSX/._HowToImportIntoNetBeans.pdf
PiggyBank.java
PiggyBank.java
/**
* A class that implements a piggy bank by using a bag.
*
*
@author
Frank M. Carrano
*
@author
Timothy M. Henry
*
@version
4.0
*/
public
class
PiggyBank
{
private
BagInterface
<
Coin
>
coins
;
public
PiggyBank
()
{
coins
=
new
DynamicBag
<>
();
}
// end default constructor
public
boolean
add
(
Coin
aCoin
)
{
return
coins
.
add
(
aCoin
);
}
// end add
public
Coin
remove
()
{
return
coins
.
remove
();
}
// end remove
public
boolean
isEmpty
()
{
return
coins
.
isEmpty
();
}
// end isEmpty
}
// end PiggyBank
__MACOSX/._PiggyBank.java
PiggyBankExample.java
PiggyBankExample.java
/**
* A class that demonstrates the class PiggyBank.
*
*
@author
Frank M. Carrano
*
@author
Timothy M. Henry
*
@version
4.0
*/
public
class
PiggyBankExample
{
public
static
void
main
(
String
[]
args
)
{
PiggyBank
myBank
=
new
PiggyBank
();
addCoin
(
new
Coin
(
1
,
2010
),
myBank
);
addCoin
(
new
Coin
(
5
,
2011
),
myBank
);
addCoin
(
new
Coin
(
10
,
2000
),
myBank
);
addCoin
(
new
Coin
(
25
,
2012
),
myBank
);
System
.
out
.
println
(
"Removing all the coins:"
);
int
amountRemoved
=
0
;
while
(
!
myBank
.
isEmpty
())
{
Coin
removedCoin
=
myBank
.
remove
();
System
.
out
.
println
(
"Removed a "
+
removedCoin
.
getCoinName
()
+
"."
);
amountRemoved
=
amountRemoved
+
removedCoin
.
getValue
();
}
// end while
System
.
out
.
println
(
"All done. Removed "
+
amountRemoved
+
" cents."
);
}
// end main
private
static
void
addCoin
(
Coin
aCoin
,
PiggyBank
aBank
)
{
if
(
aBank
.
add
(
aCoin
))
{
System
.
out
.
println
(
"Added a "
+
aCoin
.
getCoinName
()
+
"."
);
}
else
{
System
.
out
.
println
(
"Tried to add a "
+
aCoin
.
getCoinName
()
+
", but couldn't"
);
}
}
// end addCoin
}
// end PiggyBankExample
/*
Added a PENNY.
Added a NICKEL.
Added a DIME.
Added a QUARTER.
Removing all the coins:
Removed a QUARTER.
Removed a DIME.
Removed a NICKEL.
Removed a PENNY.
All done. Removed 41 cents.
*/