Spaces:
Running
Running
constructor-super, new methods2-3
Browse files
Week 5: Class hierarchies/10B. Better wallet
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The program defines a class 'Wallet', which, in addition to the
|
| 2 |
+
constructor and
|
| 3 |
+
the balance detection method,
|
| 4 |
+
has the method addMoney(double money), which can be used to add money to the wallet.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
Write the class 'BetterWallet', which inherits the class 'Wallet'.
|
| 8 |
+
In addition to the constructor, the new class has 2 overloaded new versions of the method addMoney:
|
| 9 |
+
addMoney(int euros, int cents), which gets the euros and cents added separately
|
| 10 |
+
addMoney(String amount), which receives the amount of money as a string.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
An example to illustrate how the class works:
|
| 14 |
+
public static void main(String[] args) {
|
| 15 |
+
BetterWallet wallet = new BetterWallet(10);
|
| 16 |
+
|
| 17 |
+
System.out.println(wallet.getMoney());
|
| 18 |
+
|
| 19 |
+
wallet.addMoney(5.0);
|
| 20 |
+
System.out.println(wallet.getMoney());
|
| 21 |
+
|
| 22 |
+
wallet.addMoney(2, 75);
|
| 23 |
+
System.out.println(wallet.getMoney());
|
| 24 |
+
|
| 25 |
+
wallet.addMoney("3.50");
|
| 26 |
+
System.out.println(wallet.getMoney());
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
Program outputs:
|
| 30 |
+
10.0
|
| 31 |
+
15.0
|
| 32 |
+
17.75
|
| 33 |
+
21.25
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
import java.util.Random;
|
| 42 |
+
|
| 43 |
+
public class Test{
|
| 44 |
+
public static void main(String[] args){
|
| 45 |
+
final Random r = new Random();
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
double[] desit = {0.25, 0.5, 0.75, 0.99, 0.65, 0.30};
|
| 51 |
+
System.out.println("Testing class BetterWallet...");
|
| 52 |
+
|
| 53 |
+
BetterWallet bw = new BetterWallet(r.nextInt(100) + 1);
|
| 54 |
+
|
| 55 |
+
// testing for inheritance
|
| 56 |
+
Wallet wallet = (Wallet) bw;
|
| 57 |
+
|
| 58 |
+
System.out.println("Object created!");
|
| 59 |
+
|
| 60 |
+
System.out.println("Money now: " + bw.getMoney());
|
| 61 |
+
|
| 62 |
+
double money = r.nextInt(100) + 1 + desit[r.nextInt(desit.length)];
|
| 63 |
+
System.out.println("Adding " + money);
|
| 64 |
+
bw.addMoney(money);
|
| 65 |
+
|
| 66 |
+
System.out.println("Money now: " + bw.getMoney());
|
| 67 |
+
|
| 68 |
+
int euros = r.nextInt(100) + 1;
|
| 69 |
+
int cents = r.nextInt(99) + 1;
|
| 70 |
+
System.out.println("Adding " + euros + " euros and " + cents + " cents");
|
| 71 |
+
bw.addMoney(euros, cents);
|
| 72 |
+
|
| 73 |
+
System.out.println("Money now: " + bw.getMoney());
|
| 74 |
+
|
| 75 |
+
euros = r.nextInt(100) + 1;
|
| 76 |
+
cents = r.nextInt(99) + 1;
|
| 77 |
+
|
| 78 |
+
String increment = euros + "." + cents;
|
| 79 |
+
|
| 80 |
+
System.out.println("Adding \"" + increment + "\"");
|
| 81 |
+
bw.addMoney(increment);
|
| 82 |
+
|
| 83 |
+
System.out.println("Money now: " + bw.getMoney());
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
class Wallet {
|
| 88 |
+
protected double money;
|
| 89 |
+
|
| 90 |
+
public Wallet(double money) {
|
| 91 |
+
this.money = money;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
public void addMoney(double amount) {
|
| 95 |
+
money += amount;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
public double getMoney() {
|
| 99 |
+
return money;
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
//ADD
|
| 109 |
+
class BetterWallet extends Wallet {
|
| 110 |
+
|
| 111 |
+
//constructor1
|
| 112 |
+
public BetterWallet(double money) {
|
| 113 |
+
super(money);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// method2
|
| 117 |
+
public void addMoney(int euros, int cents) {
|
| 118 |
+
// dividing by 100 can return 0 when it is less than 100
|
| 119 |
+
// -> because 'cents/100.0' would be an int
|
| 120 |
+
// this.money += euros + cents/100;
|
| 121 |
+
// divide by 100.0 for 'cents/100.0' to become a double
|
| 122 |
+
this.money += euros + cents/100.0;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// method3
|
| 126 |
+
public void addMoney(String amount) {
|
| 127 |
+
// string to double
|
| 128 |
+
// this.money += Integer.parseInt(amount);
|
| 129 |
+
this.money += Double.parseDouble(amount);
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
Testing class BetterWallet...
|
| 138 |
+
Object created!
|
| 139 |
+
Money now: 100.0
|
| 140 |
+
Adding 3.75
|
| 141 |
+
Money now: 103.75
|
| 142 |
+
Adding 62 euros and 57 cents
|
| 143 |
+
Money now: 166.32
|
| 144 |
+
Adding "13.2"
|
| 145 |
+
Money now: 179.51999999999998
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|