# Homework exercise #2 Due: Start of class on 9/11/2003 10 points Modify the attached program (also given to you on diskette) to create a better Java program. 1.Change the name of the class to an appropriate name using the Java conventions for a class name. 2.One variable is not named according to the Java conventions used in this program. Change it.

Λογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 8 μήνες)

129 εμφανίσεις

Homework exercise #2

Due: Start of class on 9/11/2003

10 points

Modify the attached program (also given to you on diskette) to create a better Java program.

1.

Change the name of the class to an appropriate name using the Java conventions for a class name.

2.

One variable is not named according to the Java conventions used in this program. Change it.

3.

Change the user input from the command line to Dialog Boxes. Be sure to import javax.swing.* at
the start of the program.

4.

Use the mathematical shortcuts of +=,
-
=, *=, /= and %= wherever appropriate.

5.

Add an accumulator for the total amount deposited (including the beginning balance). It should be
equal to the different between the total in the account (which I displayed at the end) and the
total interest accumul
ated (which I also displayed). BUT, I do not want you to subtract these
fields or use a geometric progression or financial formula. I want you to initialize the
accumulator to the beginning balance, add the monthly deposit to it in the loop and display i
t at
the end. You can determine if you are correct by subtracting my fields.

6.

If you compile and execute my program (javac homework2.java & java homework2 100 10 10 30) you
will see that the interest paid would be 20,888.74 and the total in the account is
24,588.74.
Thus the amount deposited should be 3,700.00 (100 beginning and 120 a year for 30 years).

import java.text.*;

public class homework2

{

public static void main (String[] args)

{

// input variables

double inte
rest; // yearly interest rate

double monthlyDeposit; // amount of monthly deposit to account

int years; // number of years for account/deposit

double beginningAmount;

// starting balance of savings account

int months; // years times 12 = number of months

double monthlyInterest; // interest divided by 12 = monthly interes
t

// financial variables

double interestThisMonth = 0; // interest accrued this month

int integer_field; // used to round to two decimals

double prevBalance; /
* balance of the loan for the previous month

the beginning balance for this month */

double newBalance; /* balance of the loan for the current month

the ending balance for this month */

// accumulators

double totalInterest = 0;

NumberFormat formatter = NumberFormat.getCurrencyInstance();

// get the input variables

beginningAmount =
Double.parseDouble(args[0]);

monthlyDeposit = Double.parseDouble(args[1]);

interest = Double.parseDouble(args[2]);

years = Integer.parseInt(args[3]);

months = years * 12;

monthlyInterest = (interest / 12.0) * .01;

// start the deposit/interest compounding interest compounded monthly

prevBalance = beginningAmount;

for (int i = 1; i <= months; i++)

{

inter
estThisMonth = monthlyInterest * prevBalance;

integer_field = (int) Math.round(interestThisMonth * 100);

interestThisMonth = integer_field / 100.00;

newBalance = prevBalance + interestThisMonth + monthlyDep
osit;

totalInterest = totalInterest + interestThisMonth;

prevBalance = newBalance;

}

// print out accumulators

System.out.println("total interest: " + formatter.format(totalInterest))
;

System.out.println("total in account ");

System.out.println ( " after " + years + " years: " + formatter.format(prevBalance));

}

}