| | |
Applet / Array help
![]() |
•
•
Join Date: Feb 2006
Posts: 6
Reputation:
Solved Threads: 0
I am looking to create a mortgage applet with an array for the 3 amounts. There is something I am missing here. Any help can be of use
Java Syntax (Toggle Plain Text)
/* GOAL 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. */ import javax.swing.*; import java.awt.event.*; import javax.swing.border.*; import java.awt.*; import java.lang.*; import java.text.*; import java.util.*; public class Mortgage extends javax.swing.JApplet implements ActionListener { double[] annualInterests = {5.35,5.5,5.75}; int[] numOfYears = {7,15,30}; double monthlyPayment, monthlyInterest, denominator, numOfMonths, getMortgageAmount; double principal; double interest; int term, i; float paymentdue; JLabel title = new JLabel(" Mortgage Calculator "); JLabel title = new JLabel(" Please enter Principal amount and select term with rate "); JTextField appPrincipal=new JTextField(10); JTextField appTerm=new JTextField(2); JTextField appInterest=new JTextField(5); JTextField appMonthlyPayment=new JTextField(10); JLabel ErrorMsg=new JLabel(""); Font bigFont = new Font("Helvetica", Font.ITALIC, 24); public void init() { Container con = getContentPane(); con.setLayout(new BorderLayout()); JPanel TopLabel=new JPanel(); TopLabel.setLayout(new FlowLayout()); title.setFont(bigFont); con.add("North",title); setBackground(Color.blue); setForeground(Color.yellow); JPanel MainForm=new JPanel(); MainForm.setLayout(new GridLayout(0,3)); JLabel label1=new JLabel(" Term ",SwingConstants.RIGHT); MainForm.add(label1); JButton bttn = new JButton(" 7 Years at 5.35% "); bttn.addActionListener( new ActionListener() { } public void actionPerformed(ActionEvent evt) { // This method will respond to the user's click entry for the mortgage term & interest i = 0; MainForm.add(i); } JButton bttn = new JButton(" 15 Years at 5.5% "); bttn.addActionListener( new ActionListener() { } public void actionPerformed(ActionEvent evt) { i = 1; MainForm.add(i); } JButton bttn = new JButton(" 30 Years at 5.75% "); bttn.addActionListener( new ActionListener() { } public void actionPerformed(ActionEvent evt) { i = 2; MainForm.add(i); } JLabel label2=new JLabel(" Principle ",SwingConstants.RIGHT); MainForm.add(label2); MainForm.add(appPrincipal); JLabel label3=new JLabel(" Interest Rate ",SwingConstants.RIGHT); MainForm.add(label3); MainForm.add(appInterest); JLabel label4=new JLabel("Result ",SwingConstants.RIGHT); MainForm.add(label4); MainForm.add(appMonthlyPayment); con.add("West",MainForm); JPanel ButtonPanel=new JPanel(); ButtonPanel.setLayout(new BorderLayout()); ButtonPanel.add("North",ErrorMsg); JButton Calculate=new JButton("Calculate"); Calculate.addActionListener(this); ButtonPanel.add("Center",Calculate); con.add("South",ButtonPanel); appMonthlyPayment.setEditable(false); } public void start() { repaint(); } public void actionPerformed(ActionEvent divide) { DecimalFormat decimalPlaces=new DecimalFormat("0.00"); appMonthlyPayment.setText(""); term = Integer.parseInt(appTerm.getText()); interest = Double.parseDouble(appTerm.getText()); principal = Double.parseDouble(appPrincipal.getText()); monthlyInterest = annualInterests[i]/(12*100); int totalNumOfMonths = numOfYears[i]*12; int months= term*12; monthlyInterest = interest/(12 * 100); denominator = Math.pow(1 + monthlyInterest, -(term*12)); denominator = 1 - denominator; monthlyPayment = principal * (monthlyInterest / denominator); monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1)))))); return monthlyPayment; double getMonthlyPrincipal, monthlyPayment, remainingPrincipal; return monthlyPayment - (remainingPrincipal * monthlyInterest); appMonthlyPayment.setText("Payment = " + monthlyPayment); } }
•
•
Join Date: Feb 2006
Posts: 6
Reputation:
Solved Threads: 0
PLEASE DIS-REGARD THE PRIOR MESSAGE...
\Mortgage.java:79: ';' expected
bttn.addActionListener( new ActionListener()) { ^
\Mortgage.java:88: ';' expected
bttn.addActionListener( new ActionListener()) {
^
\Mortgage.java:95: ';' expected
bttn.addActionListener( new ActionListener()) {
^
3 errors
Tool completed with exit code 1
\Mortgage.java:79: ';' expected
bttn.addActionListener( new ActionListener()) { ^
\Mortgage.java:88: ';' expected
bttn.addActionListener( new ActionListener()) {
^
\Mortgage.java:95: ';' expected
bttn.addActionListener( new ActionListener()) {
^
3 errors
Tool completed with exit code 1
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Where's the closing parenthesis and semi-colon for the follow codes:
Java Syntax (Toggle Plain Text)
bttn.addActionListener( new ActionListener() { } public void actionPerformed(ActionEvent evt) { // This method will respond to the user's click entry for the mortgage term & interest i = 0; MainForm.add(i); }
•
•
Join Date: Feb 2006
Posts: 6
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.event.*; import javax.swing.border.*; import java.awt.*; import java.lang.*; import java.text.*; import java.util.*; public class Mortgage extends javax.swing.JApplet implements ActionListener { /*Create Arrays*/ double[] annualInterests = {5.35,5.5,5.75}; //Interest Array int[] numOfYears = {7,15,30}; //Term Array // Declare variables double monthlyPayment, monthlyInterest, denominator, numOfMonths, getMortgageAmount; double principal; double interest; int term, i; float paymentdue; /*The variable used for user output for exact payment due information*/ //Constructing Labels for GUI JLabel title = new JLabel(" Mortgage Calculator "); JLabel title = new JLabel(" Please enter Principal amount and select term with rate "); JTextField appPrincipal=new JTextField(10); // Principle Loan Amount JTextField appTerm=new JTextField(2); // Term in Years "00" JTextField appInterest=new JTextField(5); // Interest % JTextField appMonthlyPayment=new JTextField(10); JLabel ErrorMsg=new JLabel(""); Font bigFont = new Font("Helvetica", Font.ITALIC, 24); //* Constructing Applet Container public void init() { Container con = getContentPane(); con.setLayout(new BorderLayout()); JPanel TopLabel=new JPanel(); TopLabel.setLayout(new FlowLayout()); title.setFont(bigFont); con.add("North",title); // Initialize the applet by setting it to use blue // and yellow as background and foreground colors. setBackground(Color.blue); setForeground(Color.yellow); JPanel MainForm=new JPanel(); MainForm.setLayout(new GridLayout(0,3)); JLabel label1=new JLabel(" Term ",SwingConstants.RIGHT); MainForm.add(label1); JButton bttn = new JButton(" 7 Years at 5.35% "); bttn.addActionListener( new ActionListener()) { // Created an "action listener" for the button which is defined by this anonymous class. } public void actionPerformed(ActionEvent evt) { // This method will respond to the user's click entry for the mortgage term & interest i = 0; MainForm.add(i); } JButton bttn = new JButton(" 15 Years at 5.5% "); bttn.addActionListener( new ActionListener()) { } public void actionPerformed(ActionEvent evt) { i = 1; MainForm.add(i); } JButton bttn = new JButton(" 30 Years at 5.75% "); bttn.addActionListener( new ActionListener()) { } public void actionPerformed(ActionEvent evt) { i = 2; MainForm.add(i); } JLabel label2=new JLabel(" Principle ",SwingConstants.RIGHT); MainForm.add(label2); MainForm.add(appPrincipal); JLabel label3=new JLabel(" Interest Rate ",SwingConstants.RIGHT); MainForm.add(label3); MainForm.add(appInterest); JLabel label4=new JLabel("Result ",SwingConstants.RIGHT); MainForm.add(label4); MainForm.add(appMonthlyPayment); con.add("West",MainForm); JPanel ButtonPanel=new JPanel(); ButtonPanel.setLayout(new BorderLayout()); ButtonPanel.add("North",ErrorMsg); JButton Calculate=new JButton("Calculate"); //add an action listener Calculate.addActionListener(this); ButtonPanel.add("Center",Calculate); con.add("South",ButtonPanel); appMonthlyPayment.setEditable(false); } public void start() { repaint(); } //Ask For User Input And Divide Input public void actionPerformed(ActionEvent divide) { DecimalFormat decimalPlaces=new DecimalFormat("0.00"); appMonthlyPayment.setText(""); // The next 3 lines get the values from the text boxes. term = Integer.parseInt(appTerm.getText()); interest = Double.parseDouble(appTerm.getText()); principal = Double.parseDouble(appPrincipal.getText()); //Calculate Monthly Interest monthlyInterest = annualInterests[i]/(12*100); int totalNumOfMonths = numOfYears[i]*12; int months= term*12; //The variable used for user input for exact month information monthlyInterest = interest/(12 * 100); //calculate the monthly interst using simple interest. denominator = Math.pow(1 + monthlyInterest, -(term*12)); denominator = 1 - denominator; monthlyPayment = principal * (monthlyInterest / denominator); // Monthly payment formula: M = P x (J/(1-(1+J)^-N)); // M = P * ( J / (1 - (1 + J) ** -N)); // source: <a rel="nofollow" class="t" href="http://www.hughchou.org/calc/formula.html" target="_blank">http://www.hughchou.org/calc/formula.html</a> monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1)))))); return monthlyPayment; //calculate the monthly interst using simple interest. double getMonthlyPrincipal, monthlyPayment, remainingPrincipal; return monthlyPayment - (remainingPrincipal * monthlyInterest); // Finally, set the text field to show the result appMonthlyPayment.setText("Payment = " + monthlyPayment); public void actionPerformed(ActionEvent e) { try { // Capture the Info the User Entered, remove 's and make it a double MortgageInfo.setInrestRatePercent(Double.parseDouble(IntrestBox.getText().replaceAll(",",""))); MortgageInfo.setloanAmount(Double.parseDouble(MortgageBox.getText().replaceAll(",",""))); MortgageInfo.setloanYears(Double.parseDouble(TermBox.getText().replaceAll(",",""))); // Get & Format the Results double test=MortgageInfo.getpaymentAmount(); DecimalFormat myFormatter = new DecimalFormat("$###,###.00"); MonthlyPayment.setText("<html><font color=blue>"+myFormatter.format(test)+"</font></html>"); } catch(Exception except) { } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); // Make it pretty JFrame frame=new briano_week2(); // Create a new Frame frame.setSize(250,180); // Set the Size of the Frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allow the User to Quit frame.setVisible(true); } } //End Program
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
You just made it worse:
If you don't know how to add and use an actionlistener, then please read up on it.
Java Syntax (Toggle Plain Text)
bttn.addActionListener( new ActionListener()) { // Created an "action listener" for the button which is defined by this anonymous class. }
If you don't know how to add and use an actionlistener, then please read up on it.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: I need some help with modem and parallel port
- Next Thread: Array issue
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






