CS 1063, Fall 2005
|
The lectures for Week 8 had you work on an Purse class. The directions didn't specify how to represent the internal private data in this class. One approach (not the simplest) might be to let variables myCents, myNickels, myDimes, and myQuarters be doubles that give the total value of each of the different kinds of coins. Here is a version of the Purse class that uses this approach.
| File: Purse.java | File: PurseTest.java |
|---|---|
public class Purse {
// constants
final double CENT_VALUE = 0.01;
final double NICKEL_VALUE = 0.05;
final double DIME_VALUE = 0.10;
final double QUARTER_VALUE = 0.25;
// private data members
private double myCents;
private double myNickels;
private double myDimes;
private double myQuarters;
// Construct an empty purse
public Purse() {
myCents = 0;
myNickels = 0;
myDimes = 0;
myQuarters = 0;
}
// addCents: Add cents to the purse
// count: the number of cents to add
public void addCents(int count) {
myCents += CENT_VALUE * count;
}
// addNickels: Add nickels to the purse
// count: the number of nickels to add
public void addNickels(int count) {
myNickels += NICKEL_VALUE * count;
}
// addDimes: Add dimes to the purse
// count: the number of dimes to add
public void addDimes(int count) {
myDimes += DIME_VALUE * count;
}
// addQuarters: Add quarters to the purse
// count: the number of quarters to add
public void addQuarters(int count) {
myQuarters += QUARTER_VALUE * count;
}
// calculate the total value in purse
// return the sum of all coin values
public double calculateTotal() {
return myCents +
myNickels +
myDimes +
myQuarters;
}
public String toString() {
return "Purse: \n" +
" " + (myCents/CENT_VALUE) +
" Cents = " + myCents + "\n" +
" " + (myNickels/NICKEL_VALUE) +
" Nickels = " + myNickels + "\n" +
" " + (myDimes/DIME_VALUE) +
" Dimes = " + myDimes + "\n" +
" " + (myQuarters/QUARTER_VALUE) +
" Quarters = " + myQuarters + "\n";
}
}
| public class PurseTest {
public static void main(String[] args) {
Purse aPurse = new Purse();
aPurse.addCents(7);
aPurse.addNickels(3);
aPurse.addDimes(5);
aPurse.addQuarters(2);
double totalValue = aPurse.calculateTotal();
System.out.println("aPurse, total value: " +
totalValue);
System.out.println(aPurse);
Purse bPurse = new Purse();
bPurse.addCents(14);
bPurse.addNickels(31);
bPurse.addDimes(7);
bPurse.addQuarters(5);
totalValue = bPurse.calculateTotal();
System.out.println("bPurse, total value: " +
totalValue);
System.out.println(bPurse);
}
}
Output of a run:
aPurse, total value: 1.22
Purse:
7.000000000000001 Cents = 0.07
3.0000000000000004 Nickels = 0.15000000000000002
5.0 Dimes = 0.5
2.0 Quarters = 0.5
bPurse, total value: 3.64
Purse:
14.000000000000002 Cents = 0.14
31.0 Nickels = 1.55
7.0 Dimes = 0.7000000000000001
5.0 Quarters = 1.25
|
After studying the above class, you should think what the class might look like if myQuaters used an int to store the number of quarters in the purse, instead of being a double that holds the total value of the quarters. This is a simpler and less confusing way to represent the amount of money in the purse.
The second program below uses this new approach, where changes are made to the Purse class, but the PurseTest class stays the same. This is a very important point: that we can change a class without changing other classes that make use of the first class (as client code). The code in red, both above and below, shows the code that has been changed.
| File: Purse.java | File: PurseTest.java |
|---|---|
public class Purse {
// constants
final double CENT_VALUE = 0.01;
final double NICKEL_VALUE = 0.05;
final double DIME_VALUE = 0.10;
final double QUARTER_VALUE = 0.25;
// private data members
private int myCents;
private int myNickels;
private int myDimes;
private int myQuarters;
// Constructs an empty purse
public Purse() {
myCents = 0;
myNickels = 0;
myDimes = 0;
myQuarters = 0;
}
// addCents: Add cents to the purse
// count: the number of cents to add
public void addCents(int count) {
myCents += count;
}
// addNickels: Add nickels to the purse
// count: the number of nickels to add
public void addNickels(int count) {
myNickels += count;
}
// addDimes: Add dimes to the purse
// count: the number of dimes to add
public void addDimes(int count) {
myDimes += count;
}
// addQuarters: Add quarters to purse
// count: the number of quarters to add
public void addQuarters(int count) {
myQuarters += count;
}
// calculate the total value in purse
// return the sum of all coin values
public double calculateTotal() {
return CENT_VALUE * myCents +
NICKEL_VALUE * myNickels +
DIME_VALUE * myDimes +
QUARTER_VALUE * myQuarters;
}
public String toString() {
return "Purse: \n" +
" " + myCents + " Cents = " +
(myCents * CENT_VALUE) + "\n" +
" " + myNickels + " Nickels = " +
(myNickels * NICKEL_VALUE) + "\n" +
" " + myDimes + " Dimes = " +
(myDimes * DIME_VALUE) + "\n" +
" " + myQuarters + " Quarters = " +
(myQuarters * QUARTER_VALUE) + "\n";
}
}
| public class PurseTest {
public static void main(String[] args) {
Purse aPurse = new Purse();
aPurse.addCents(7);
aPurse.addNickels(3);
aPurse.addDimes(5);
aPurse.addQuarters(2);
double totalValue = aPurse.calculateTotal();
System.out.println("aPurse, total value: " +
totalValue);
System.out.println(aPurse);
Purse bPurse = new Purse();
bPurse.addCents(14);
bPurse.addNickels(31);
bPurse.addDimes(7);
bPurse.addQuarters(5);
totalValue = bPurse.calculateTotal();
System.out.println("bPurse, total value: " +
totalValue);
System.out.println(bPurse);
}
}
Output of a run:
aPurse, total value: 1.22
Purse:
7 Cents = 0.07
3 Nickels = 0.15000000000000002
5 Dimes = 0.5
2 Quarters = 0.5
bPurse, total value: 3.64
Purse:
14 Cents = 0.14
31 Nickels = 1.55
7 Dimes = 0.7000000000000001
5 Quarters = 1.25
|