Applet / Array help

Reply

Join Date: Feb 2006
Posts: 6
Reputation: dmgs11 is an unknown quantity at this point 
Solved Threads: 0
dmgs11 dmgs11 is offline Offline
Newbie Poster

Applet / Array help

 
0
  #1
Feb 19th, 2006
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

  1. /* 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. */
  2.  
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import javax.swing.border.*;
  6. import java.awt.*;
  7. import java.lang.*;
  8. import java.text.*;
  9. import java.util.*;
  10.  
  11. public class Mortgage extends javax.swing.JApplet implements ActionListener {
  12.  
  13. double[] annualInterests = {5.35,5.5,5.75};
  14. int[] numOfYears = {7,15,30};
  15.  
  16. double monthlyPayment, monthlyInterest, denominator, numOfMonths, getMortgageAmount;
  17. double principal;
  18. double interest;
  19. int term, i;
  20. float paymentdue;
  21.  
  22. JLabel title = new JLabel(" Mortgage Calculator ");
  23. JLabel title = new JLabel(" Please enter Principal amount and select term with rate ");
  24. JTextField appPrincipal=new JTextField(10);
  25. JTextField appTerm=new JTextField(2);
  26. JTextField appInterest=new JTextField(5);
  27.  
  28. JTextField appMonthlyPayment=new JTextField(10);
  29. JLabel ErrorMsg=new JLabel("");
  30.  
  31. Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
  32.  
  33. public void init()
  34. {
  35. Container con = getContentPane();
  36. con.setLayout(new BorderLayout());
  37.  
  38. JPanel TopLabel=new JPanel();
  39. TopLabel.setLayout(new FlowLayout());
  40. title.setFont(bigFont);
  41. con.add("North",title);
  42.  
  43. setBackground(Color.blue);
  44. setForeground(Color.yellow);
  45.  
  46. JPanel MainForm=new JPanel();
  47. MainForm.setLayout(new GridLayout(0,3));
  48. JLabel label1=new JLabel(" Term ",SwingConstants.RIGHT);
  49. MainForm.add(label1);
  50. JButton bttn = new JButton(" 7 Years at 5.35% ");
  51. bttn.addActionListener( new ActionListener() { }
  52. public void actionPerformed(ActionEvent evt) {
  53. // This method will respond to the user's click entry for the mortgage term & interest
  54. i = 0;
  55. MainForm.add(i);
  56.  
  57. }
  58. JButton bttn = new JButton(" 15 Years at 5.5% ");
  59. bttn.addActionListener( new ActionListener() {
  60. }
  61. public void actionPerformed(ActionEvent evt) {
  62. i = 1;
  63. MainForm.add(i);
  64. }
  65. JButton bttn = new JButton(" 30 Years at 5.75% ");
  66. bttn.addActionListener( new ActionListener() {
  67. }
  68. public void actionPerformed(ActionEvent evt) {
  69. i = 2;
  70. MainForm.add(i);
  71. }
  72.  
  73.  
  74.  
  75. JLabel label2=new JLabel(" Principle ",SwingConstants.RIGHT);
  76. MainForm.add(label2);
  77.  
  78. MainForm.add(appPrincipal);
  79.  
  80. JLabel label3=new JLabel(" Interest Rate ",SwingConstants.RIGHT);
  81. MainForm.add(label3);
  82.  
  83. MainForm.add(appInterest);
  84.  
  85. JLabel label4=new JLabel("Result ",SwingConstants.RIGHT);
  86. MainForm.add(label4);
  87.  
  88. MainForm.add(appMonthlyPayment);
  89.  
  90. con.add("West",MainForm);
  91.  
  92. JPanel ButtonPanel=new JPanel();
  93. ButtonPanel.setLayout(new BorderLayout());
  94. ButtonPanel.add("North",ErrorMsg);
  95. JButton Calculate=new JButton("Calculate");
  96.  
  97. Calculate.addActionListener(this);
  98.  
  99. ButtonPanel.add("Center",Calculate);
  100. con.add("South",ButtonPanel);
  101. appMonthlyPayment.setEditable(false);
  102.  
  103.  
  104. }
  105.  
  106. public void start()
  107. {
  108. repaint();
  109. }
  110.  
  111. public void actionPerformed(ActionEvent divide)
  112. {
  113.  
  114. DecimalFormat decimalPlaces=new DecimalFormat("0.00");
  115. appMonthlyPayment.setText("");
  116.  
  117. term = Integer.parseInt(appTerm.getText());
  118. interest = Double.parseDouble(appTerm.getText());
  119. principal = Double.parseDouble(appPrincipal.getText());
  120.  
  121. monthlyInterest = annualInterests[i]/(12*100);
  122. int totalNumOfMonths = numOfYears[i]*12;
  123.  
  124. int months= term*12;
  125. monthlyInterest = interest/(12 * 100);
  126. denominator = Math.pow(1 + monthlyInterest, -(term*12));
  127. denominator = 1 - denominator;
  128. monthlyPayment = principal * (monthlyInterest / denominator);
  129.  
  130. monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
  131. return monthlyPayment;
  132.  
  133. double getMonthlyPrincipal, monthlyPayment, remainingPrincipal;
  134.  
  135. return monthlyPayment - (remainingPrincipal * monthlyInterest);
  136.  
  137. appMonthlyPayment.setText("Payment = " + monthlyPayment);
  138.  
  139.  
  140. }
  141.  
  142. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Applet / Array help

 
0
  #2
Feb 19th, 2006
What's the question? What problem are you having? Is there an error?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 6
Reputation: dmgs11 is an unknown quantity at this point 
Solved Threads: 0
dmgs11 dmgs11 is offline Offline
Newbie Poster

Re: Applet / Array help

 
0
  #3
Feb 19th, 2006
\Mortgage.java:81: ')' expected
public void actionPerformed(ActionEvent evt) {
^
\Mortgage.java:90: ')' expected
public void actionPerformed(ActionEvent evt) {
^
\Mortgage.java:97: ')' expected
public void actionPerformed(ActionEvent evt) {
^
3 errors

Tool completed with exit code 1
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 6
Reputation: dmgs11 is an unknown quantity at this point 
Solved Threads: 0
dmgs11 dmgs11 is offline Offline
Newbie Poster

Re: Applet / Array help

 
0
  #4
Feb 19th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Applet / Array help

 
0
  #5
Feb 19th, 2006
Where's the closing parenthesis and semi-colon for the follow codes:
  1. bttn.addActionListener( new ActionListener() { }
  2. public void actionPerformed(ActionEvent evt) {
  3. // This method will respond to the user's click entry for the mortgage term & interest
  4. i = 0;
  5. MainForm.add(i);
  6.  
  7. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 6
Reputation: dmgs11 is an unknown quantity at this point 
Solved Threads: 0
dmgs11 dmgs11 is offline Offline
Newbie Poster

Re: Applet / Array help

 
0
  #6
Feb 19th, 2006
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import javax.swing.border.*;
  4. import java.awt.*;
  5. import java.lang.*;
  6. import java.text.*;
  7. import java.util.*;
  8.  
  9. public class Mortgage extends javax.swing.JApplet implements ActionListener {
  10.  
  11.  
  12. /*Create Arrays*/
  13. double[] annualInterests = {5.35,5.5,5.75}; //Interest Array
  14. int[] numOfYears = {7,15,30}; //Term Array
  15.  
  16.  
  17. // Declare variables
  18. double monthlyPayment, monthlyInterest, denominator, numOfMonths, getMortgageAmount;
  19. double principal;
  20. double interest;
  21. int term, i;
  22. float paymentdue; /*The variable used for user output for exact payment due information*/
  23.  
  24.  
  25. //Constructing Labels for GUI
  26. JLabel title = new JLabel(" Mortgage Calculator ");
  27. JLabel title = new JLabel(" Please enter Principal amount and select term with rate ");
  28. JTextField appPrincipal=new JTextField(10); // Principle Loan Amount
  29. JTextField appTerm=new JTextField(2); // Term in Years "00"
  30. JTextField appInterest=new JTextField(5); // Interest %
  31.  
  32. JTextField appMonthlyPayment=new JTextField(10);
  33. JLabel ErrorMsg=new JLabel("");
  34.  
  35. Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
  36.  
  37. //* Constructing Applet Container
  38. public void init()
  39. {
  40. Container con = getContentPane();
  41. con.setLayout(new BorderLayout());
  42.  
  43. JPanel TopLabel=new JPanel();
  44. TopLabel.setLayout(new FlowLayout());
  45. title.setFont(bigFont);
  46. con.add("North",title);
  47.  
  48. // Initialize the applet by setting it to use blue
  49. // and yellow as background and foreground colors.
  50. setBackground(Color.blue);
  51. setForeground(Color.yellow);
  52.  
  53. JPanel MainForm=new JPanel();
  54. MainForm.setLayout(new GridLayout(0,3));
  55. JLabel label1=new JLabel(" Term ",SwingConstants.RIGHT);
  56. MainForm.add(label1);
  57. JButton bttn = new JButton(" 7 Years at 5.35% ");
  58. bttn.addActionListener( new ActionListener()) { // Created an "action listener" for the button which is defined by this anonymous class.
  59. }
  60.  
  61. public void actionPerformed(ActionEvent evt) {
  62. // This method will respond to the user's click entry for the mortgage term & interest
  63. i = 0;
  64. MainForm.add(i);
  65. }
  66.  
  67. JButton bttn = new JButton(" 15 Years at 5.5% ");
  68. bttn.addActionListener( new ActionListener()) {
  69. }
  70.  
  71. public void actionPerformed(ActionEvent evt) {
  72. i = 1;
  73. MainForm.add(i);
  74. }
  75.  
  76. JButton bttn = new JButton(" 30 Years at 5.75% ");
  77. bttn.addActionListener( new ActionListener()) {
  78. }
  79.  
  80. public void actionPerformed(ActionEvent evt) {
  81. i = 2;
  82. MainForm.add(i);
  83. }
  84.  
  85.  
  86. JLabel label2=new JLabel(" Principle ",SwingConstants.RIGHT);
  87. MainForm.add(label2);
  88.  
  89. MainForm.add(appPrincipal);
  90.  
  91. JLabel label3=new JLabel(" Interest Rate ",SwingConstants.RIGHT);
  92. MainForm.add(label3);
  93.  
  94. MainForm.add(appInterest);
  95.  
  96. JLabel label4=new JLabel("Result ",SwingConstants.RIGHT);
  97. MainForm.add(label4);
  98.  
  99. MainForm.add(appMonthlyPayment);
  100.  
  101. con.add("West",MainForm);
  102.  
  103. JPanel ButtonPanel=new JPanel();
  104. ButtonPanel.setLayout(new BorderLayout());
  105. ButtonPanel.add("North",ErrorMsg);
  106. JButton Calculate=new JButton("Calculate");
  107.  
  108. //add an action listener
  109. Calculate.addActionListener(this);
  110.  
  111. ButtonPanel.add("Center",Calculate);
  112. con.add("South",ButtonPanel);
  113. appMonthlyPayment.setEditable(false);
  114.  
  115.  
  116. }
  117.  
  118. public void start()
  119. {
  120. repaint();
  121. }
  122.  
  123. //Ask For User Input And Divide Input
  124. public void actionPerformed(ActionEvent divide)
  125. {
  126.  
  127. DecimalFormat decimalPlaces=new DecimalFormat("0.00");
  128. appMonthlyPayment.setText("");
  129.  
  130. // The next 3 lines get the values from the text boxes.
  131. term = Integer.parseInt(appTerm.getText());
  132. interest = Double.parseDouble(appTerm.getText());
  133. principal = Double.parseDouble(appPrincipal.getText());
  134.  
  135.  
  136. //Calculate Monthly Interest
  137. monthlyInterest = annualInterests[i]/(12*100);
  138. int totalNumOfMonths = numOfYears[i]*12;
  139.  
  140. int months= term*12; //The variable used for user input for exact month information
  141. monthlyInterest = interest/(12 * 100); //calculate the monthly interst using simple interest.
  142. denominator = Math.pow(1 + monthlyInterest, -(term*12));
  143. denominator = 1 - denominator;
  144. monthlyPayment = principal * (monthlyInterest / denominator);
  145.  
  146.  
  147. // Monthly payment formula: M = P x (J/(1-(1+J)^-N));
  148. // M = P * ( J / (1 - (1 + J) ** -N));
  149. // source: <a rel="nofollow" class="t" href="http://www.hughchou.org/calc/formula.html" target="_blank">http://www.hughchou.org/calc/formula.html</a>
  150. monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
  151. return monthlyPayment;
  152.  
  153.  
  154.  
  155. //calculate the monthly interst using simple interest.
  156. double getMonthlyPrincipal, monthlyPayment, remainingPrincipal;
  157.  
  158. return monthlyPayment - (remainingPrincipal * monthlyInterest);
  159.  
  160.  
  161. // Finally, set the text field to show the result
  162. appMonthlyPayment.setText("Payment = " + monthlyPayment);
  163.  
  164. public void actionPerformed(ActionEvent e)
  165. {
  166. try
  167. { // Capture the Info the User Entered, remove 's and make it a double
  168.  
  169. MortgageInfo.setInrestRatePercent(Double.parseDouble(IntrestBox.getText().replaceAll(",","")));
  170. MortgageInfo.setloanAmount(Double.parseDouble(MortgageBox.getText().replaceAll(",","")));
  171. MortgageInfo.setloanYears(Double.parseDouble(TermBox.getText().replaceAll(",",""))); // Get & Format the Results
  172. double test=MortgageInfo.getpaymentAmount();
  173. DecimalFormat myFormatter = new DecimalFormat("$###,###.00");
  174. MonthlyPayment.setText("<html><font color=blue>"+myFormatter.format(test)+"</font></html>");
  175. }
  176. catch(Exception except)
  177. {
  178.  
  179. }
  180.  
  181. public static void main(String[] args)
  182. {
  183. JFrame.setDefaultLookAndFeelDecorated(true); // Make it pretty
  184. JFrame frame=new briano_week2(); // Create a new Frame
  185. frame.setSize(250,180); // Set the Size of the Frame
  186. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allow the User to Quit
  187. frame.setVisible(true);
  188. }
  189.  
  190. } //End Program
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Applet / Array help

 
0
  #7
Feb 19th, 2006
You just made it worse:

  1. bttn.addActionListener( new ActionListener()) { // Created an "action listener" for the button which is defined by this anonymous class.
  2. }

If you don't know how to add and use an actionlistener, then please read up on it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 6
Reputation: dmgs11 is an unknown quantity at this point 
Solved Threads: 0
dmgs11 dmgs11 is offline Offline
Newbie Poster

Re: Applet / Array help

 
0
  #8
Feb 19th, 2006
Thanks for the HELP!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC