File size: 3,492 Bytes
36a8302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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