/* Investment.java For a bank account that has: deposit - Initial Deposit ($) interest - Annual interest rate (%) periods - Number of annual periods (i.e. periods per year) that interest is compounded time - Time in years The bank account balance at any time is: Note that interest in equations/calculations is decimal (e.g. 5% = 0.05) base = 1 + interest/periods balance = deposit * base^(periods*time) where a^b means a to the power of b e.g. 3^2 = 9 9^2 = 81 2^3 = 8 In Java use the Math.pow method. The total interest rate accrued (gained) at any time is: totalInterest = balance - deposit */ public class Investment { String name; double deposit; double interest; int periods; int time; /** Sets name/label of this investment */ public void setName(String aName) { this.name = aName; } /** Sets initial deposit amount ($) */ public void setDeposit(double aDeposit) { this.deposit = aDeposit; } /** Sets annual interest rate (%) */ public void setInterest(double anInterest) { this.interest = anInterest; } /** Sets number of annual periods (i.e. periods per year) that interest is compounded */ public void setPeriods(int aPeriods) { this.periods = aPeriods; } /** Sets time (years) that investment has accrued/accumulated */ public void setTime(int aTime) { this.time = aTime; } //=================================================== /** Returns new Investment initialized with passed parameters */ public Investment(String newName, double d, double i, int p, int y) { this.name = newName; this.deposit = d; this.interest = i; this.periods = p; this.time = y; } /** Returns new uninitialized Investment */ public Investment() { this.name = ""; } /** Returns friendly (one-line) string describing this object */ public String toString() { return String.format("%s d=%.2f i=%.2f periods=%d y=%d total=%.2f totalInterest=%.2f", this.name, this.deposit, this.interest, this.periods, this.time, this.computeBalance(), this.computeTotalInterest()); } /** Returns my inputs as vertical string (one value per line) */ public String inputAsVerticalString() { return String.format("\tDeposit: $%,.2f\n\tInterest: %,.2f%%\n\tAnnual Periods: %d\n\tInvestment Time (years): %d", this.deposit, this.interest, this.periods, this.time); } /** Returns my outputs as vertical string (one value per line) */ public String outputAsVerticalString() { return String.format("\tBalance: $%,.2f\n\tInterest Gained: $%,.2f", this.computeBalance(), this.computeTotalInterest()); } /** Returns the balance of this investment */ public double computeBalance() { /* * base = 1 + interest/periods balance = * deposit * base^(periods*time) balance * = deposit * Math.pow(base, * (periods*time)) */ double deposit, interest; int periods, time; double iAsDecimal, base, total; deposit = this.deposit; interest = this.interest; interest = interest / 100.0; //convert percent to decimal periods = this.periods; time = this.time; base = 1 + interest / periods; total = deposit * Math.pow(base, periods * time); return total; } /** Returns the total interest that this investment has accrued */ public double computeTotalInterest() { return this.computeBalance() - this.deposit; } public static void main(String[] args) { Investment test = new Investment("test", 4500, 7, 2, 9); println("Expecting: T=8358.70 and I=3858.70"); println(test); Investment investment1 = new Investment("I1", 1000, 5, 2, 30); Investment investment2 = new Investment("I2", 10000, 5, 2, 5); println(investment1); println(investment2); Investment ii = new Investment("ii", 0, 0, 0, 0); println(ii); } public static void println(Object o) { System.out.println(o.toString()); } public static void printf(String template, Object... args) { System.out.printf(template, args); } }
public Investment(String newName, double d, double i, int p, int y)Returns new Investment initialized with passed parameterspublic Investment()Returns new uninitialized Investmentpublic void setName(String aName)Sets name/label of this investmentpublic void setDeposit(double aDeposit)Sets initial deposit amount ($)public void setInterest(double anInterest)Sets annual interest rate (%)public void setPeriods(int aPeriods)Sets number of annual periods (i.e. periods per year)
that interest is compoundedpublic void setTime(int aTime)Sets time (years) that investment has accrued/accumulatedpublic String toString()Returns friendly (one-line) string describing this objectpublic String inputAsVerticalString()Returns my inputs as vertical string (one value per line)public String outputAsVerticalString()Returns my outputs as vertical string (one value per line)public double computeBalance()Returns the balance of this investmentpublic double computeTotalInterest()Returns the total interest that this investment has accrued
public class InvestmentTests { public static void main(String[] args) { InvestmentTests test = new InvestmentTests(); test.test_investment_1(); test.test_investment_2(); } public void test_investment_1() { Investment investment = new Investment(); //Send messages to setup the investment properties here investment.setName("Investment-#1"); //etc //YOUR CODE HERE //You can ask the investment for the results like this: println(investment.inputAsVerticalString()); println(investment.outputAsVerticalString()); println("\n"); } public void test_investment_2() { Investment investment = new Investment(); //Send messages to setup the investment properties here investment.setName("Investment-#2"); //etc //YOUR CODE HERE //You can ask the investment for the results like this: println(investment.inputAsVerticalString()); println(investment.outputAsVerticalString()); println("\n"); } //---------------------------------------- public static void println(Object o) { System.out.println(o.toString()); } public static void printf(String template, Object... args) { System.out.printf(template, args); } }
5000.51 3500.30 12000.27 2200.93