Mortgage calculador with array

I finish my code, it run with no error.  Can some one take a look and help me to make it more presentable, with out using loops.  


Here is the code

import java.text.DecimalFormat;//This funtion allow to format the numeric output
class MortgageCalculator
{
      public static void main(String arguments[])
      {

            int[] terms = { 7*12, 15*12, 30*12 };
            double[] rates = { 0.0535, 0.055, 0.0575 };

            for (int i=0; i<3; i++)
            {

            /*Program Variables*/
            double I = rates[i];//*Interest rate
            double P = 200000;//*Loan amount
            double J = I/12; //*Monthy interest
            int    N = terms[i];

            //calculate monthly payment
            double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
            double payment = P / discount; //*Rate Calculation

            DecimalFormat twoDigits = new DecimalFormat("$#,000.00");//Defines pattern for formatting numeric output


            /*Output*/
            System.out.println("\t Mortgage Calculator");
            System.out.println("For a $200,000.00 Mortgage");
            System.out.println("With a interest rate of 5.35% for 7 Years");
            System.out.println("With a interest rate of 5.5% for 15 Years");
            System.out.println("With a interest rate of 5.75% for 30 Years");
            System.out.println ("Your monthly payment is " + twoDigits.format(payment));
      }
}
}


martinez1112Asked:
Who is Participating?
 
mukundha_expertConnect With a Mentor Commented:
Try this code..

import java.text.DecimalFormat;//This funtion allow to format the numeric output
public class MortgageCalculator
{
     public static void main(String arguments[])
     {

          int[] terms = { 7*12, 15*12, 30*12 };
          double[] rates = { 0.0535, 0.055, 0.0575 };

          for (int i=0; i<3; i++)
          {

          /*Program Variables*/
          double I = rates[i];//*Interest rate
          double P = 200000;//*Loan amount
          double J = I/12; //*Monthy interest
          int    N = terms[i];

          //calculate monthly payment
          double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
          double payment = P / discount; //*Rate Calculation

          DecimalFormat twoDigits = new DecimalFormat("$#,000.00");//Defines pattern for formatting numeric output


          /*Output*/
          System.out.println("\t Mortgage Calculator");
          System.out.println("For a $200,000.00 Mortgage");
          System.out.println("With a interest rate of "+ rates[i]*100 +"% for "+ terms[i]/12 +"Years");
          System.out.println ("Your monthly payment is " + twoDigits.format(payment));
     }
}
}
0
 
hoomanvCommented:
> with out using loops

so do each of the 3 calculations separately out of loop
0
 
martinez1112Author Commented:
yes, but I still need to use Array
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
hoomanvCommented:
is this OK ?

import java.text.DecimalFormat;

class MortgageCalculator {
      public static void main(String arguments[]) {
            String msg = "\t Mortgage Calculator\n" +
                              "For a $200,000.00 Mortgage\n" +
                              "With a interest rate of 5.35% for 7 Years\n" +
                              "With a interest rate of 5.5% for 15 Years\n" +
                              "With a interest rate of 5.75% for 30 Years\n" +
                              "Your monthly payment is ";
            
            int[] terms = { 7*12, 15*12, 30*12 };
            double[] rates = { 0.0535, 0.055, 0.0575 };
            DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
            
            for (int i=0; i<3; i++)
                  System.out.println(msg + twoDigits.format(calculatePayment(rates[i], terms[i])));
      }
      
      static double calculatePayment(double I, int N) {
            double P = 200000; //*Loan amount
            double J = I/12; //*Monthy interest

            //calculate monthly payment
            double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
            double payment = P / discount; //*Rate Calculation
            return payment;
      }
}
0
 
martinez1112Author Commented:
I'm getting the output in the same way as before.  the problem that I have in in the output that is showing

Mortgage Calculator
For a $200,000.00 Mortgage
With a interest rate of 5.35% for 7 Years
With a interest rate of 5.5% for 15 Years
With a interest rate of 5.75% for 30 Years
payment........


but I'm trying to have it some thing like this

Mortgage Calculator
For a $200,000.00 Mortgage
With a interest rate of 5.35% for 7 Years
payment......


Mortgage Calculator
For a $200,000.00 Mortgage
With a interest rate of 5.5% for 15 Years
payment......



Mortgage Calculator
For a $200,000.00 Mortgage
With a interest rate of 5.75% for 30 Years
payment......
0
 
hoomanvConnect With a Mentor Commented:
import java.text.DecimalFormat;

class MortgageCalculator {
      public static void main(String arguments[]) {            
            int[] terms = { 7*12, 15*12, 30*12 };
            double[] rates = { 0.0535, 0.055, 0.0575 };
            DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
            
            for (int i=0; i<3; i++) {
                  System.out.println("\t Mortgage Calculator\n" +
                  "For a $200,000.00 Mortgage\n" +
                  "With a interest rate of " + (rates[i] * 100) + " for " + (terms[i] / 12) + "% Years\n" +
                  "Your monthly payment is " +
                  twoDigits.format(calculatePayment(rates[i], terms[i])));
            }
      }
      
      static double calculatePayment(double I, int N) {
            double P = 200000; //*Loan amount
            double J = I/12; //*Monthy interest

            //calculate monthly payment
            double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
            double payment = P / discount; //*Rate Calculation
            return payment;
      }
}
0
 
basicinstinctConnect With a Mentor Commented:
Like this...?

import java.text.DecimalFormat;//This funtion allow to format the numeric output
class MortgageCalculator
{
     public static void main(String arguments[])
     {

          int[] terms = { 7*12, 15*12, 30*12 };
          double[] rates = { 0.0535, 0.055, 0.0575 };

          for (int i=0; i<terms.length; i++)
          {
                /*Program Variables*/
                double I = rates[i];//*Interest rate
                double P = 200000;//*Loan amount
                double J = I/12; //*Monthy interest
                int    N = terms[i];
      
                //calculate monthly payment
                double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
                double payment = P / discount; //*Rate Calculation
      
                DecimalFormat twoDigits = new DecimalFormat("$#,000.00");//Defines pattern for formatting numeric output
      
      
                /*Output*/
                System.out.println("Mortgage Calculator");
                System.out.println("For a $200,000.00 Mortgage");
                System.out.println("With a interest rate of " + (rates[i] * 100) + "% for " + (terms[i] / 12) + " Years");
                System.out.println ("Your monthly payment is " + twoDigits.format(payment));
                System.out.println();
          }
     }
}
0
 
hoomanvCommented:
apparently all the answers are the same :)

-- for java 1.5, without DecimalFormat by using System.out.printf()

class MortgageCalculator {
      public static void main(String arguments[]) {
            String msg = "\t Mortgage Calculator\n" +
                  "For a $200,000.00 Mortgage\n" +
                  "With a interest rate of %.2f%% for %d Years\n" +
                  "Your monthly payment is %,.2f \n\n";
            
            int[] terms = { 7*12, 15*12, 30*12 };
            double[] rates = { 0.0535, 0.055, 0.0575 };
            
            for (int i=0; i<3; i++)
                  System.out.printf(msg, rates[i] * 100, terms[i] / 12, calculatePayment(rates[i], terms[i]));
      }
      
      static double calculatePayment(double I, int N) {
            double P = 200000; //*Loan amount
            double J = I/12; //*Monthy interest

            //calculate monthly payment
            double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
            double payment = P / discount; //*Rate Calculation
            return payment;
      }
}
0
 
martinez1112Author Commented:
Thanks to all. I really apresiate your help

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.