-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoney.java
44 lines (36 loc) · 916 Bytes
/
Money.java
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
/*
* CSC 300 Sections 602,611 Homework assignment 1, Problem 2
*/
package hw1solution;
public class Money {
private int dollars, cents;
// in the Money constructor, you may assume that d >= 0,
// and that 0 <= c < 100
public Money(int d, int c) {
dollars = d;
cents = c;
}
public String toString() {
String ans = "$" + dollars + ".";
if (cents == 0) return ans + "00";
else if (cents < 10) return ans + "0" + cents;
else return ans + cents;
}
public Money add(Money other) {
int d = dollars + other.dollars;
int c = cents + other.cents;
if (c >= 100) {
d += 1;
c -= 100;
}
return new Money(d,c); // replace this
}
public Money tax(double rate) {
int allCents = dollars * 100 + cents;
int tax = (int) ((allCents * rate)/100.);
System.out.println(tax);
int d = tax / 100;
int c = tax - d * 100;
return new Money(d,c); // replace this
}
}