Arrays and Mortgage Calculator Issues

Good Afternoon,

Here is my code to calculated a loan amount among different percentage rates and terms.  
import java.io.IOException;

public class GutierrezMortgages {

    //Method to Calculate Mortgage using the principal amount, the interest, and the term
    static  int[] loanTerm = {360,180,84}; //*30 years, 15 years, 7 years
    static  double loanInterest[] = {.0575, .0550, .0535}; //*5.75% interest rate(ir), 5.50% (ir), 5.35% (ir)
    static  double loanAmount = 200000; //*$200,000 Loan

    public static double calculateMortgage (double loanAmount, double loanInterest, int loanTerm) {
       double myMortgage = (loanAmount*loanInterest/12)/(1-Math.pow(1+loanInterest/12, -loanTerm));
       return myMortgage;
    }

    //Method to display the calculated mortgage
    public static void displayTheMortgages()  {
       System.out.println();
       System.out.printf("\t\tLoan Amount: $%.2f\n", loanAmount);
       
       for (int i = 0; i < loanTerm.length; i++) {
       System.out.printf("\t\tTerm: %d Years", loanTerm [i] / 12);
       System.out.println();
       System.out.printf("\t\tInterest Rate: %.2f%%", loanInterest[i] * 100);
       System.out.println();
       System.out.printf("\t\tThe monthly payment amount is $%.2f", calculateMortgage (loanAmount, loanInterest[i], loanTerm[i]));
       System.out.println();
       }
    }

    public static void main(String[] args){
       displayTheMortgages();
    }
}

Open in new window


This executes what I want but the problem is that I need to place this variables into
the main () method with argument of type arrays and pass the arrays from main because our instructor does not want these variables in another method other than the main () method.

I am still new to Java and my mind is numb with all this code.  Any help would be great!

PapaDragonXAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Try
import java.io.IOException;


public class GutierrezMortgages {
    //Method to Calculate Mortgage using the principal amount, the interest, and the term
    public static double calculateMortgage(double loanAmount,
        double loanInterest, int loanTerm) {
        double myMortgage = ((loanAmount * loanInterest) / 12) / (1 -
            Math.pow(1 + (loanInterest / 12), -loanTerm));

        return myMortgage;
    }

    //Method to display the calculated mortgage
    public static void displayTheMortgages(int[] loanTerm,
        double[] loanInterest, double loanAmount) {
        System.out.println();
        System.out.printf("\t\tLoan Amount: $%.2f\n", loanAmount);

        for (int i = 0; i < loanTerm.length; i++) {
            System.out.printf("\t\tTerm: %d Years", loanTerm[i] / 12);
            System.out.println();
            System.out.printf("\t\tInterest Rate: %.2f%%", loanInterest[i] * 100);
            System.out.println();
            System.out.printf("\t\tThe monthly payment amount is $%.2f",
                calculateMortgage(loanAmount, loanInterest[i], loanTerm[i]));
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[] loanTerm = { 360, 180, 84 }; //*30 years, 15 years, 7 years
        double[] loanInterest = { .0575, .0550, .0535 }; //*5.75% interest rate(ir), 5.50% (ir), 5.35% (ir)
        double loanAmount = 200000; //*$200,000 Loan
        displayTheMortgages(loanTerm, loanInterest, loanAmount);
    }
}

Open in new window

0
 
PapaDragonXAuthor Commented:
The variables that I need to place in the main() method are:

  //Method to Calculate Mortgage using the principal amount, the interest, and the term
    static  int[] loanTerm = {360,180,84}; //*30 years, 15 years, 7 years
    static  double loanInterest[] = {.0575, .0550, .0535}; //*5.75% interest rate(ir), 5.50% (ir), 5.35% (ir)
    static  double loanAmount = 200000; //*$200,000 Loan 

Open in new window


Here is my revised code that has 9 errors:
/*Joshua Gutierrez
	PRG 420
	Week 4
	Individual Assignment
	3/27/3011
*/
package joshua;

import java.io.IOException;

public class GutierrezMortgages {

    
    public static double calculateMortgage (double loanAmount, double loanInterest, double loanTerm) {
       double myMortgage = (loanAmount*loanInterest/12)/(1-Math.pow(1+loanInterest/12, -loanTerm));
       return myMortgage;
    }

    //Method to display the calculated mortgage
    public void displayTheMortgages(double myCalculatedMortgage)  {
       System.out.println();
       System.out.printf("\t\tLoan Amount: $%.2f\n", loanAmount);
       
       for (int i = 0; i < loanTerm.length; i++) {
       System.out.printf("\t\tTerm: %d Years", loanTerm [i] / 12);
       System.out.println();
       System.out.printf("\t\tInterest Rate: %.2f%%", loanInterest[i] * 100);
       System.out.println();
       System.out.printf("\t\tThe monthly payment amount is $%.2f", calculateMortgage (loanAmount, loanInterest[i], loanTerm[i]));
       System.out.println();
       }
    }

    public static void main(String[] args){

		//Method to Calculate Mortgage using the principal amount, the interest, and the term
                  double loanTerm[] = {360,180,84}; //*30 years, 15 years, 7 years
                  double loanInterest[] = {.0575, .0550, .0535}; //*5.75% interest rate(ir), 5.50% (ir), 5.35% (ir)
                  double loanAmount = 200000; //*$200,000 Loan


	
                 // Create an instance of the class

		GutierrezMortgages myInstance = new GutierrezMortgages ();

                // Use my the class instance and the dot notation to call the method that calculates the mortgage

		double myMortgage = myInstance.calculateMortgage (loanAmount, loanInterest, loanTerm);

                // Display the calculated mortgage

		myInstance.displayTheMortgage(myMortgage);
       
    }
}  

Open in new window

0
 
PapaDragonXAuthor Commented:
Awesome,

I totally messed up the revision and noticed that all I had to do was define the arguments within the "displayTheMortgages[loanTerm, loanInterest, loanAmount" and moving the brackets in the main() method.  Thanks for lending another set of eyes to this!
0
 
PapaDragonXAuthor Commented:
Outstanding!  Thanks for clarifying!
0
 
CEHJCommented:
:)
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.