I am working on a mortgage calculation program and I need assistance with calling arrays.
my instructions are this:
Declare four one dimensional arrays to store three mortgages. Use the first array to store the amounts, the second one for Interest Rates, the third one for Years/months, and the last one to store the calculated payment for each loan.
Call a method to fill these arrays. You need to send the arrays to the method to get them filled. Call a second method to print the values from these arrays. Also, calculate and print the total payment for each mortgage. You need to send the arrays to the method for printing. Use a loop to print the values from the three arrays.
Fill Array Method
Accept the arrays as parameters and ask the user to input the values for each mortgage. Calculate the monthly payment for each mortgage and store it in the payment array. You need a loop here as we are dealing with three mortgages. Please check the output below to see the spacing between each loan.
Print Array Method
Accept the arrays as parameters and print the output as shown. Make sure the output is formatted.
and my code looks like this:
*/
import java.io.*;
import java.text.DecimalFormat;
public class MultipleLoans_2
{
public static void main(
String[] args) throws IOException //Stores the exception rule
{
//declare the array using the following data types
double loanAmt[]=new double [3];//intializes loan amount array with assigned new double variable
double yearRate[]=new double [3];//intializes year rate array with assigned new double variable
double monthlyPmt[]=new double [3];//intializes monthly payment array with assigned new double variable
double numMonths []=new double [3];//intializes number of months array with assigned new double variable
double newInterest []=new double [3];//intializes new interest array with assigned new double variable
double totPmt []=new double [3];//initialize new total payment array with assigned new double variable
int loanTerm[]=new int [3];//intializes loan term with assigned new integer
int i;//declare function variable
DecimalFormat twoDigits = new DecimalFormat ("0.00");//Formats any specified amount with proper digits
String answer;//declare string variable named answer
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));//
for (i=0; i<3; i++)//creates a counter controlled loop
{
try//Note can not have a try statement without a finally or
{
System.out.print("Please enter the loan amount :$ ");//gets input from user
answer = dataIn.readLine();
loanAmt[i] = Double.parseDouble(answer);
System.out.print("What is the yearly interest rate(Eg. 5.0): ");
answer = dataIn.readLine();
yearRate[i] = Double.parseDouble(answer);
System.out.print("Enter the term in years: ");
answer = dataIn.readLine();
loanTerm[i] = Integer.parseInt(answer);
totPmt[i] = Double.parseDouble(answer);
System.out.print("\n");
while(loanAmt[i] <=0 && yearRate[i] <=0 && loanTerm[i] <=0);
throw new NumberFormatException();
} // Closes try statement
/*There are multiple components that must be analyzed in the above portion coding. The for loop loop creates a counter loop that will allow the user to the
loan amount,the yearly interest rate, and the loan term. The i=0 starts the loop, the i<2 is the stop point and the i++
increments the amount of times this particular portion of the program. The try gets user input including exceptions Inputs
are required from user. Program catches number format exception and catches any value that is negative. it
stores the error but continues through program for efficiency.
*/
catch (NumberFormatException nfe) //catches any incorrect information entered in the
{
System.out.print("\n");
System.out.println(" Input value not understood, Please input correct amount!");
System.out.print("\n");
System.out.print("\n");
}// This closes the Error output statement
}//This concludes the for loop
for (i=0; i<3; i++)//creates a new counter loop
//The formulas below Implement the calculation formulas to determine monthly payments
{
newInterest[i] = ((yearRate[i]/100)/12); //Convert interest rate to decimal
numMonths[i] = loanTerm[i]*12; // Convert yearly terms into months
monthlyPmt[i] = loanAmt[i]/((1/newInterest[i])-(1/(newInterest[i]*Math.pow(1+newInterest[i], numMonths[i]))));
//This formula calculates monthly payments for the mortgage plan
totPmt[i] = numMonths[i]*monthlyPmt[i];/* calculates the total payments based on
the number of months and monthly payments.*/
} // Needed to conclude this portion of the loop statement
System.out.print(" Loan\tRate\tMonths\tPayment\t\tTotpayment");
System.out.print("\n");
System.out.print("\n");
for (i=0; i<3; i++)//creates another counter loop statement
{
System.out.print("\n");
System.out.print(twoDigits.format(loanAmt[i])+"\t");//prints the loan amount and sends array specified row and column
System.out.print(twoDigits.format(yearRate[i])+"\t");// prints the year interest rate and sends array to its specified row and column
System.out.print(twoDigits.format(numMonths[i])+"\t");//prints the number of months for payment and sends array in its specified row and column
System.out.print(twoDigits.format(monthlyPmt[i])+ "\t\t");//prints the monthly payments and sends array its specified row and column
System.out.print(twoDigits.format(totPmt[i]));//prints the tot payment amount and sends array to its specified row and column
System.out.print("\n");
} // This concludes the for loop statement
}//Closes the main public static
}//Ends the public class file and ends program
am I addressing the instructions or no