Skip to content

Instantly share code, notes, and snippets.

@doyonghoon
Created February 21, 2017 04:46
MATH 2B: Uncle Bill
/**
* Mini Project:
* Two different methods to calculate the amount that Nancy is to receive from her Uncle Bill.
*/
public class Bill {
public static void main(String[] args) {
List<Double> firstList = new ArrayList<>();
List<Double> secondList = new ArrayList<>();
int max = 15;
FirstCalculator c1 = new FirstCalculator();
SecondCalculator c2 = new SecondCalculator();
stackAmount(max, c1, firstList);
stackAmount(max, c2, secondList);
System.out.println(firstList);
System.out.println(secondList);
boolean[] comparedResult = new boolean[max - 1];
for (int i = 0; i < firstList.size(); i++) {
comparedResult[i] = firstList.get(i) > secondList.get(i);
}
System.out.println(Arrays.toString(comparedResult));
}
private static void stackAmount(int maxWeek, Calculator calculator, List<Double> list) {
for (int i = 1; i < maxWeek; i++) {
list.add(calculator.getAmmount(i));
}
}
private interface Calculator {
double getAmmount(int week);
}
private static abstract class CommonCalculator {
double roundUp(double amount) {
return Double.parseDouble(new DecimalFormat("##.00").format(amount));
}
}
private static class FirstCalculator extends CommonCalculator implements Calculator {
@Override
public double getAmmount(int week) {
switch (week) {
case 1:
return 0;
case 2:
return 0.01;
default:
double a = (5 * getAmmount(week - 1));
double b = (6 * getAmmount(week - 2));
double amount = a - b;
return roundUp(amount);
}
}
}
private static class SecondCalculator extends CommonCalculator implements Calculator {
@Override
public double getAmmount(int week) {
switch (week) {
case 1:
return 0;
case 2:
return 1;
default:
double a = getAmmount(week - 1);
double b = getAmmount(week - 2);
return roundUp(a + b);
}
}
}
}
@doyonghoon
Copy link
Author

The output when max = 15

FirstChoice: [0.0, 0.01, 0.05, 0.19, 0.65, 2.11, 6.65, 20.59, 63.05, 191.71, 580.25, 1750.99, 5273.45, 15861.31]
SecondChoice: [0.0, 1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0, 55.0, 89.0, 144.0, 233.0]
The amount from FirstChoice is larger than SecondChoice: [false, false, false, false, false, false, false, true, true, true, true, true, true, true]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment