Code issue

Hi all,

I am having a problem with my code for my class. I'm new to Java programing. So, I am having difficulty trying to figure out why my program will not compile.

Here is the assignment:

Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. Please insert comments in the program to document the program.

Here is my code:

/*

Title:Mortgage Calculator

Programmer: Mark Jackson

Date:Januaray 21, 2007

Filename:mortgageCalculation.java

Class:POS / 407

Instructor: Raouf Benhadj

Purpose:Write a program with a GUI. Have it calculate and display

mortgage payment amount from user input. Allow for user to

clear and input new data.

*/

import java.io.*;

import java.awt.*;

import javax.swing.BoxLayout;

import javax.swing.JFrame;

import java.text.DecimalFormat;

public class mortgageCalculator extends JFrame

{

final static DecimalFormat decimal = new DecimalFormat("$0.00");

// create new form

public mortgageCalculator()

{

initComponents();

}

private void initComponets()

{

jTextField1 = new JTextField();

jTextField2 = new JTextField();

jTextField3 = new JTextField();

jLabel1= new JLabel();

jLabel2= new JLabel();

jLabel3= new JLabel();

jLabel4= new JLabel();

jLabel5= new JLabel();

jButton1= new JButton();

jButton2= new JButton();

setDefaultCloseOperation(windowConstrants.EXIT_ON_CLOSE);

// make button1, the Calculate button clickable

jButton1.setText("Calculate");

jButton1.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(java.awt.event.ActionEvent evt)

{

jButton1ActionPerformed(evt);

}

});

jLabel1.setText("0.00");

jLabel2.setText("Monthly Payment");

jLabel3.setText("Principal");

jLabel4.setText("Term");

jLabel5.setText("Interest");

// make button2, the Clear button clickable

jButton2.setText("Clear");

jButton2.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent evt)

{

jButton2ActionPerformed(evt);

}

});

// setting the layout

JPanel mortgagePanel = new JPanel(new GridLayout(3,2));

mortgagePanel.add(jLabel3);

mortgagePanel.add(jTextField1);

mortgagePanel.add(jLabel5);

mortgagePanel.add(jTextField2);

mortgagePanel.add(jLabel4);

mortgagePanel.add(jTextField3);

mortgagePanel.add(jlabel1);

mortgagePanel.add(jLabel2);

// setting button layout

JPanel buttonPanel = JPanel();

buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));

buttonPanel.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));

buttonPanel.add(Box.createHorizontalGlue());

buttonPanel.add(jButton1);

buttonPanel.add(jButton2);

}

}

// setting the function on the Clear button

private void jButton2ActionPerformed(ActionEvent evt)

{

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

jLabel1.setText("0.00");

}

// setting the function of the Calculate Button

private void jButton1ActionPerformed(ActionEvent evt)

{

double field1 = Double.parseDouble(jTextField1.getText());

double field2 = Double.parseDouble(jTextField.getText());

int field3= Integer.parseInt(jTextField3.getText());

double sum= calcPayment(field1, field2, field3);

String formatted = decimal.format(sum);

jLabel1.setText(formatted);

}

public static void main(String args[])

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

new mortgageCalculator().setVisible(true);

}

})

}

// calculation

public double calcPayment(double interestRate, double principal, int years)

{

int months = years * 12;

double annualInterest= interestRate / 100;

double periodicInterest = annualInterest / 12;

double interestBalance = principal * periodicInterest;

double monthlyPayment= interestBalance / (1-(1/Math.pow((1 + periodicInterest), months)));

double principalBalance = monthlyPayment - interestBalance;

double loanBalance= principal - principalBalance;

return monthlyPayment;

}

Any help would be greatly appreciated.

Thanks.

[4772 byte] By [Kededrina] at [2007-11-26 15:53:34]
# 1
Give us the compiler errors, along with what lines they are occuring on.
CaptainMorgan08a at 2007-7-8 22:14:03 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hi all,

>

> I am having a problem with my code for my class. I'm

> new to Java programing. So, I am having difficulty

> trying to figure out why my program will not compile.

You're kidding, right?

This is so far from legal Java syntax it's ridiculous. I brought this into IntelliJ and watched every line marked in red with syntax errors.

Incredible. That's all I can say.

%

duffymoa at 2007-7-8 22:14:03 > top of Java-index,Java Essentials,Java Programming...
# 3

I only quickly scanner through your code, but it looks like your main problem is that you did not declare any of the objects.

JButton jButton; //declared here

jButton = new JButton(); //initialized here

CaptainMorgan08a at 2007-7-8 22:14:03 > top of Java-index,Java Essentials,Java Programming...
# 4

This compiles, but it still needs work. I don't believe this is what you had in mind for the runtime behavior. - %

/*

Title: Mortgage Calculator

Programmer: Mark Jackson

Date: Januaray 21, 2007

Filename: mortgageCalculation.java

Class: POS / 407

Instructor: Raouf Benhadj

Purpose: Write a program with a GUI. Have it calculate and display

mortgage payment amount from user input. Allow for user to

clear and input new data.

*/

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.WindowConstants;

import java.awt.EventQueue;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.DecimalFormat;

public class mortgageCalculator extends JFrame

{

final static DecimalFormat decimal = new DecimalFormat("$0.00");

private JTextField jTextField1;

private JTextField jTextField2;

private JTextField jTextField3;

private JLabel jLabel1;

private JLabel jLabel2;

private JLabel jLabel3;

private JLabel jLabel4;

private JLabel jLabel5;

private JButton jButton1;

private JButton jButton2;

// create new form

public mortgageCalculator()

{

jTextField1 = new JTextField();

jTextField2 = new JTextField();

jTextField3 = new JTextField();

jLabel1 = new JLabel();

jLabel2 = new JLabel();

jLabel3 = new JLabel();

jLabel4 = new JLabel();

jLabel5 = new JLabel();

jButton1 = new JButton();

jButton2 = new JButton();

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

// make button1, the Calculate button clickable

jButton1.setText("Calculate");

jButton1.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(java.awt.event.ActionEvent evt)

{

jButton1ActionPerformed(evt);

}

});

jLabel1.setText("0.00");

jLabel2.setText("Monthly Payment");

jLabel3.setText("Principal");

jLabel4.setText("Term");

jLabel5.setText("Interest");

// make button2, the Clear button clickable

jButton2.setText("Clear");

jButton2.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent evt)

{

jButton2ActionPerformed(evt);

}

});

// setting the layout

JPanel mortgagePanel = new JPanel(new GridLayout(3, 2));

mortgagePanel.add(jLabel3);

mortgagePanel.add(jTextField1);

mortgagePanel.add(jLabel5);

mortgagePanel.add(jTextField2);

mortgagePanel.add(jLabel4);

mortgagePanel.add(jTextField3);

mortgagePanel.add(jLabel1);

mortgagePanel.add(jLabel2);

// setting button layout

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));

buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));

buttonPanel.add(Box.createHorizontalGlue());

buttonPanel.add(jButton1);

buttonPanel.add(jButton2);

}

// setting the function on the Clear button

private void jButton2ActionPerformed(ActionEvent evt)

{

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

jLabel1.setText("0.00");

}

// setting the function of the Calculate Button

private void jButton1ActionPerformed(ActionEvent evt)

{

double field1 = Double.parseDouble(jTextField1.getText());

double field2 = Double.parseDouble(jTextField2.getText());

int field3 = Integer.parseInt(jTextField3.getText());

double sum = calcPayment(field1, field2, field3);

String formatted = decimal.format(sum);

jLabel1.setText(formatted);

}

public static void main(String args[])

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

new mortgageCalculator().setVisible(true);

}

});

}

// calculation

public double calcPayment(double interestRate, double principal, int years)

{

int months = years * 12;

double annualInterest = interestRate / 100;

double periodicInterest = annualInterest / 12;

double interestBalance = principal * periodicInterest;

double monthlyPayment = interestBalance / (1 - (1 / Math.pow((1 + periodicInterest), months)));

double principalBalance = monthlyPayment - interestBalance;

double loanBalance = principal - principalBalance;

return monthlyPayment;

}

}

duffymoa at 2007-7-8 22:14:03 > top of Java-index,Java Essentials,Java Programming...
# 5
jButton = new JButton("some text");Would be even better.
floundera at 2007-7-8 22:14:03 > top of Java-index,Java Essentials,Java Programming...