This program is for calculating the monthly mortgage for 5 properties. I have made the whole program work without methods but now need to break it up into different methods. I need help with creating a method that will calculate mortgage and return it to an array. This is what I have so far
import javax.swing.JOptionPane;
public class test1
{
public static void main(String[] args)
{
//variables
String[] address = new String[5];
double[] cost = new double[5];
double interest = 0.0;
double length = 0.0;
double[] monthlyMortgage = new double[5];
fillData(address,cost,interest,length);
monthlyMortgage = getMortgage(cost, interest,length);
getLowest(address,monthlyMortgage);
}
public static void fillData(String[] address,double[] cost,double interest,double length)
{
//Get address and cost from user
for (int i = 0; i < 5; i++)
{
address[i] = JOptionPane.showInputDialog("Enter property address: ");
cost[i] = Double.parseDouble(JOptionPane.showInputDialog("Enter cost of property: "));
}
//Get interest and length from user
interest = Double.parseDouble(JOptionPane.showInputDialog("Enter mortgage interest rate: "));
length = Double.parseDouble(JOptionPane.showInputDialog("Enter mortgage length: "));
}
//Calculate monthly mortgage for each property
public static double[]getMortgage(double[] cost, double interest,double length)
{
double[] monthlyMortgage1 = new double[5];
for (int i = 0; i < 5; i++)
{
monthlyMortgage1[i] = ((cost[i] + (cost[i] * interest)) / (12 * length));
}
return monthlyMortgage1;
}
public static void getLowest(String[] address,double[] monthlyMortgage)
{
//Find lowest monthly mortgage
double lowestMonthlyMortgage = monthlyMortgage[0];
String lowestMortgageAddress = "";
for (int i = 0; i < 5; i++)
{
if (monthlyMortgage[i] < lowestMonthlyMortgage)
{
lowestMonthlyMortgage = monthlyMortgage[i];
lowestMortgageAddress = address[i];
}
}
//Print out monthly mortgage and address
for (int i = 0; i < 5; i++)
{
JOptionPane.showMessageDialog(null, "Address: " + address[i] + "\n"
+ "Monthly Mortgage: " + monthlyMortgage[i]);
}
//Print lowest monthly mortgage
JOptionPane.showMessageDialog(null, "Address of lowest monthly mortgage: " + lowestMortgageAddress);
}
}
When I run this, I keep getting "infinity" as the total for the monthly mortgage.