Index

Chapter Video


Here is a video about this chapter.

Code


You are given the code for a Investment class.

This code is ready to run -- just copy it into a file named Investment.java.

/*
	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);
	}

}

Code Doc


Here is the code doc (i.e., code documentation) for the Investment class.

It is much more concise -- does not include the code (method bodies).

For message sending, this code doc is all we need.

public Investment(String newName, double d, double i, int p, int y)
Returns new Investment initialized with passed parameters
public Investment()
Returns new uninitialized Investment
public void setName(String aName)
Sets name/label of this investment
public 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 compounded
public void setTime(int aTime)
Sets time (years) that investment has accrued/accumulated
public String toString()
Returns friendly (one-line) string describing this object
public 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 investment
public double computeTotalInterest()
Returns the total interest that this investment has accrued

Test


You are provided a template for the sending messages to the Investment object.

This code is ready to run -- just copy it into a file named InvestmentTests.java. You will want to enhance the test code -- put your work (message sends to the Investment object) at the "//YOUR CODE HERE" tags.

See the "Problem Statement" step below.


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);
	}

}

Try-It


The recommended approach to "try it" is to copy the code given above to .java files on your local computer (as described above under "Code" and "Test") and then compile, and run in the normal way (and tweak the code as needed).

For a quick peek, you could try sending some messages here

Problem Statement


Problem #1


Compute the "Balance" and "Interest Gained" for the following investment:

You should use the "Investment" class provided for the calculations (see above).


Problem #2


Compute the "Balance" and "Interest Gained" for the following investment:

You should use the "Investment" class provided for the calculations (see above).


Submitting and Rubrics (Grading Rules)


See the rubrics (grading rules) here.


Notes:

Here is an example format for "A1-ANSWERS.TXT":

5000.51  3500.30
12000.27  2200.93

Notes: