Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Desperatley need applet / arrat help

843807Feb 25 2006
I can not get my code to run w/ o errors and I am beyond stumped. Any advice would be useful in getting this code to run and compile error free. Also, I am missing a menu items for adding the term and rate.

errors:
.java:124: array required, but double found
totalNumOfMonths[i] = numOfYears*12;
^
.java:135: array required, but double found
monthlyPayment[i]= (double)(principal*(monthlyInterest[i] / (1-(Math.pow((1+monthlyInterest[i]),(totalNumOfMonths[i]*-1))))));
^
.java:139: operator <= cannot be applied to double,double[]
for (int count=1; mortgageBalance<=monthlyPayment; count++) //Loop for balance amounts
^
.java:142: operator * cannot be applied to double,double[]
double interestPaid= principal * monthlyInterest;
^
.java:143: operator - cannot be applied to double[],double
double principalPaid= monthlyPayment - interestPaid;
^
5 errors

Tool completed with exit code 1
/****************************************************************************************************
 * Danielle Safonte
 * Modified on February 22, 2006
 * Week 4 Mortgage Calculator Applet	Version 7.1
 *
 * Task
 * Modify the mortgage program to allow the user to select which way they want to calculate a
 * mortgage: by input of the amount of the mortgage, the term of the mortgage, and the interest
 * rate of the mortgage payment (as was done in the program for Week Two) or by input of the amount
 * of a mortgage and then select from a menu of mortgage loans: 7 year at 5.35%, 15 year at 5.5 %,
 * and 30 year at 5.75% (as was done in the program for Week Three). In either case, display the
 * mortgage payment amount. Then, list the loan balance and interest paid for each payment over the
 * term of the loan. Allow the user to loop back and enter a new amount and make a new selection, or
 * quit. Include data validation and error handling in your program. Insert comments in the program
 * to document the program.
 *
 ****************************************************************************************************/

//Import Libraries
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.lang.*;
import java.text.*;
import java.util.*;

/**
 *
 * @author Danielle Safonte
 */
public class DSafonteWeek4 extends javax.swing.JApplet implements ActionListener {			//Start Main

    /*Create Arrays*/
    double[] annualInterest  = {5.35,5.50,5.75}; //Interest Array
    int[] numOfYears    = {7,15,30};			 //Term Array

    // Declare variables
    double numOfMonths, getMortgageAmount, principal, interest, totalNumOfMonths;
    int i;
    double monthlyPayment[]; /*The variable used for user output for exact payment due information*/
    double monthlyInterest[];

    //Constructing Labels for GUI
    JLabel title = new JLabel("  Mortgage Calculator - D. Safonte  ");
    JLabel title2 = new JLabel("  Please enter Principal amount and select term with rate ");
    JTextField appPrincipal=new JTextField(10);  												// Principle Loan Amount
    JLabel appMonthlyPayment=new JLabel();
    JLabel ErrorMsg=new JLabel("");

    Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

    //Constructing Applet Container
    public void init() {												//Opens Applet Container
        Container con = getContentPane();
        con.setLayout(new BorderLayout());
        JPanel TopLabel=new JPanel();
        TopLabel.setLayout(new FlowLayout());
        title.setFont(bigFont);
        con.add("North",title);
        JPanel MainForm=new JPanel();
        MainForm.setLayout(new GridLayout(0,3));
        JLabel label1=new JLabel(" Term & Interest Rate ",SwingConstants.RIGHT);
        MainForm.add(label1);
        JLabel label2=new JLabel(" Principle ",SwingConstants.LEFT);
        MainForm.add(label2);
        MainForm.add(appPrincipal);
        JLabel label3=new JLabel(" Monthly Payment ",SwingConstants.RIGHT);
        MainForm.add(label3);
        MainForm.add(appMonthlyPayment);
        con.add("West",MainForm);
        JButton clearBtn=new JButton("Clear");
        clearBtn.addActionListener(this);
        JPanel ButtonPanel=new JPanel();
        ButtonPanel.setLayout(new BorderLayout());
        ButtonPanel.add("North",ErrorMsg);
        JButton exitBtn = new JButton("Exit");
        exitBtn.addActionListener(this);
        JButton Calculate=new JButton("Calculate");
        Calculate.addActionListener(this);
        ButtonPanel.add("Center",Calculate);
        con.add("South",ButtonPanel);

        JButton Mortg1 =new JButton(" 7 year term at 5.35% interest ");			    // 7 year term @ 5.35%
        Mortg1.addActionListener( new ActionListener(){
            public void actionPerformed(ActionEvent e) {      						// This method will respond to the user's click entry for the mortgage term & interest
                i=1;
            }
        });
        JButton Mortg2 =new JButton(" 15 year term at 5.50% interest ");			// 15 year term @ 5.50%
        Mortg2.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                i=2;
            }
        });
        JButton Mortg3 =new JButton(" 30 year term at 5.75% interest ");			// 30 year term @ 5.75%
        Mortg3.addActionListener( new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                i=3;
            }
        });

    } // Closes Applet Container

    public void start() {
        repaint();
    }

    //Mortgage Calculator
    public void actionPerformed(ActionEvent Mortgage) {					           //Starts Mortgage Calculator
        DecimalFormat decimalPlaces=new DecimalFormat("0.00");
        appMonthlyPayment.setText("");

        // The next 3 lines get the values from the text boxes. - The only user entry will be principal for this version
        // term = Integer.parseInt(appTerm.getText());
        // interest = Double.parseDouble(appTerm.getText());
        principal = Double.parseDouble(appPrincipal.getText());

	 for (int i=0; i<3; i++) {       											//Start loop for calculating the monthly interest.
	       monthlyInterest[i] = annualInterest/(12*100);			//Calculate Monthly Interest
totalNumOfMonths[i] = numOfYears[i]*12;
int months = numOfYears[i]*12; //The variable used for user input for exact month information
double denominator= Math.pow(1 + monthlyInterest[i], -(numOfYears[i]*12));
denominator = 1 - denominator;
monthlyPayment[i]= principal * (monthlyInterest[i]/ denominator);
} //End for interest loop

int numOfMonths[];
int counter = 0; //Create a counter for the remainding payments.
if (counter<= totalNumOfMonths){ //Start of loop to count down payments.
counter=counter +1;
monthlyPayment[i]= (double)(principal*(monthlyInterest[i] / (1-(Math.pow((1+monthlyInterest[i]),(totalNumOfMonths[i]*-1))))));

appMonthlyPayment.setText("Payment = " + monthlyPayment); //Finally, set the text field to show the result
double mortgageBalance;
for (int count=1; mortgageBalance<=monthlyPayment; count++) //Loop for balance amounts
{
double getMortgageAmount;
double interestPaid= principal * monthlyInterest;
double principalPaid= monthlyPayment - interestPaid;
mortgageBalance = principal - principalPaid;
getMortgageAmount=mortgageBalance;

}//End for Loop
}
} //Ends Mortgage Calculator

} //End Main Program
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2006
Added on Feb 25 2006
0 comments
129 views