Commit e2f0956
Changed files (4)
assignments
assignment1
src
main
java
ca
mokhan
assignment1
test
java
ca
mokhan
assignment1
assignments/assignment1/src/main/java/ca/mokhan/assignment1/BonusOnSavings.java
@@ -14,10 +14,10 @@ public class BonusOnSavings
this.annualRate = annualRate;
}
- public double computeBonus(double commitment, double q1, double q2, double q3, double q4)
+ public double computeBonus(double monthlyCommitment, double q1, double q2, double q3, double q4)
{
- double quarterlyTarget = commitment * 3;
- double annualTarget = commitment * 12;
+ double quarterlyTarget = monthlyCommitment * 3;
+ double annualTarget = monthlyCommitment * 12;
return this.quarterlyBonus(quarterlyTarget, q1) +
this.quarterlyBonus(quarterlyTarget, q2) +
assignments/assignment1/src/main/java/ca/mokhan/assignment1/EmployeeSavings.java
@@ -0,0 +1,46 @@
+package ca.mokhan.assignment1;
+
+import java.util.Objects;
+
+public class EmployeeSavings extends AddressBook
+{
+ //private double accountValue;
+ //private double[] monthlyInterests;
+ //private double[] monthlySavings;
+ public static final double ANNUAL_INTEREST_RATE = 0.05;
+
+ private double monthlyContribution;
+
+ public EmployeeSavings(String firstName, String lastName) {
+ this(firstName, lastName, new double[12], new double[12]);
+ }
+
+ public EmployeeSavings(String firstName, String lastName, double monthlyContribution) {
+ super(firstName, "", lastName);
+ this.monthlyContribution = monthlyContribution;
+ }
+
+ // what is d1, d2? These are not intention revealing variable names.
+ public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) {
+ super(firstName, "", lastName);
+ }
+
+ //public double getAccountValue() { }
+ //public double[] calculateInterests() { }
+ //public double[] generateMonthlySavings() { }
+ //public double[] getMonthlyInterests() {}
+ //public double[] getMonthlySavings() {}
+ //public static String getReport(EmployeeSavings[] savings) {}
+
+ public double predictSavingsAfterMonths(int months)
+ {
+ double monthlyRate = ANNUAL_INTEREST_RATE / 12.0;
+ double balance = 0;
+
+ for (int i = 0; i < months; i++) {
+ balance = (balance + this.monthlyContribution) * (1 + monthlyRate);
+ }
+
+ return Math.round(balance * 1000) / 1000.0;
+ }
+}
assignments/assignment1/src/test/java/ca/mokhan/assignment1/EmployeeSavingsTest.java
@@ -0,0 +1,31 @@
+package ca.mokhan.assignment1;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class EmployeeSavingsTest extends TestCase
+{
+ private EmployeeSavings subject;
+
+ public EmployeeSavingsTest(String testName)
+ {
+ super(testName);
+ double monthlyContribution = 100.00;
+ this.subject = new EmployeeSavings("Tsuyoshi", "Garrett", monthlyContribution);
+ }
+
+ public static Test suite()
+ {
+ return new TestSuite(EmployeeSavingsTest.class);
+ }
+
+ public void testPredictSavingsAfter1Month()
+ {
+ // Bankers rounding rules would round this amount down to $100.41
+ // the $0.007 would go to the bank.
+ assertEquals(100.417, subject.predictSavingsAfterMonths(1));
+ assertEquals(201.252, subject.predictSavingsAfterMonths(2));
+ assertEquals(302.507, subject.predictSavingsAfterMonths(3));
+ }
+}
assignments/assignment1/README.md
@@ -118,3 +118,16 @@ Find the second fastest runner.
Print the name, address, his/her time (in minutes), and the difference in time with the fastest runner.
Compute the average time of completion taken by these runners.
Finally, print the name and number of years participated for each runner if the runner’s time of completion is equal to or better than the average time of completion.
+
+5. Solve the following problem using a program:
+Suppose you save $100 each month into a savings account with an annual interest rate of 5%.
+Thus, the monthly interest rate is 0.05/12 = 0.00417.
+After the first month, the value in the account becomes 100 * (1 + 0.00417) = 100.417
+After the second month, the value in the account becomes (100 + 100.417) * (1 + 0.00417) = 201.252
+And after the third month, the value in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.507
+... and so on.
+
+Write a program that randomly generates monthly savings amounts for the 15 runners in Problem 4.
+Each monthly saving should be in the range of $100 to $800.
+Extend the AddressBook class to store the monthly savings generated by the random number generator.
+Then, display the final account value for each of the 15 runners.