Spaces:
Running
Running
| The program defines a class 'Wallet', which, in addition to the | |
| constructor and | |
| the balance detection method, | |
| has the method addMoney(double money), which can be used to add money to the wallet. | |
| Write the class 'BetterWallet', which inherits the class 'Wallet'. | |
| In addition to the constructor, the new class has 2 overloaded new versions of the method addMoney: | |
| addMoney(int euros, int cents), which gets the euros and cents added separately | |
| addMoney(String amount), which receives the amount of money as a string. | |
| An example to illustrate how the class works: | |
| public static void main(String[] args) { | |
| BetterWallet wallet = new BetterWallet(10); | |
| System.out.println(wallet.getMoney()); | |
| wallet.addMoney(5.0); | |
| System.out.println(wallet.getMoney()); | |
| wallet.addMoney(2, 75); | |
| System.out.println(wallet.getMoney()); | |
| wallet.addMoney("3.50"); | |
| System.out.println(wallet.getMoney()); | |
| } | |
| Program outputs: | |
| 10.0 | |
| 15.0 | |
| 17.75 | |
| 21.25 | |
| import java.util.Random; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| final Random r = new Random(); | |
| double[] desit = {0.25, 0.5, 0.75, 0.99, 0.65, 0.30}; | |
| System.out.println("Testing class BetterWallet..."); | |
| BetterWallet bw = new BetterWallet(r.nextInt(100) + 1); | |
| // testing for inheritance | |
| Wallet wallet = (Wallet) bw; | |
| System.out.println("Object created!"); | |
| System.out.println("Money now: " + bw.getMoney()); | |
| double money = r.nextInt(100) + 1 + desit[r.nextInt(desit.length)]; | |
| System.out.println("Adding " + money); | |
| bw.addMoney(money); | |
| System.out.println("Money now: " + bw.getMoney()); | |
| int euros = r.nextInt(100) + 1; | |
| int cents = r.nextInt(99) + 1; | |
| System.out.println("Adding " + euros + " euros and " + cents + " cents"); | |
| bw.addMoney(euros, cents); | |
| System.out.println("Money now: " + bw.getMoney()); | |
| euros = r.nextInt(100) + 1; | |
| cents = r.nextInt(99) + 1; | |
| String increment = euros + "." + cents; | |
| System.out.println("Adding \"" + increment + "\""); | |
| bw.addMoney(increment); | |
| System.out.println("Money now: " + bw.getMoney()); | |
| } | |
| } | |
| class Wallet { | |
| protected double money; | |
| public Wallet(double money) { | |
| this.money = money; | |
| } | |
| public void addMoney(double amount) { | |
| money += amount; | |
| } | |
| public double getMoney() { | |
| return money; | |
| } | |
| } | |
| //ADD | |
| class BetterWallet extends Wallet { | |
| //constructor1 | |
| public BetterWallet(double money) { | |
| super(money); | |
| } | |
| // method2 | |
| public void addMoney(int euros, int cents) { | |
| // dividing by 100 can return 0 when it is less than 100 | |
| // -> because 'cents/100.0' would be an int | |
| // this.money += euros + cents/100; | |
| // divide by 100.0 for 'cents/100.0' to become a double | |
| this.money += euros + cents/100.0; | |
| } | |
| // method3 | |
| public void addMoney(String amount) { | |
| // string to double | |
| // this.money += Integer.parseInt(amount); | |
| this.money += Double.parseDouble(amount); | |
| } | |
| } | |
| Testing class BetterWallet... | |
| Object created! | |
| Money now: 100.0 | |
| Adding 3.75 | |
| Money now: 103.75 | |
| Adding 62 euros and 57 cents | |
| Money now: 166.32 | |
| Adding "13.2" | |
| Money now: 179.51999999999998 | |