User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 427,398 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,206 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Sep 20th, 2004
Views: 2,940
The JOptionPane class is used to give the CompMortgage.java program a front-end GUI interface. The class is divided into numerous functions.
java Syntax | 4 stars
  1. package computemortgage;
  2. import javax.swing.JOptionPane;
  3. import java.text.DecimalFormat;
  4. // The following program gives the previous computeMortgage program a front-end
  5. // GUI interface. I wrote the functions from bottom to top because I wasn't
  6. // sure if they needed to be defined before they are used (as is necessary
  7. // in C++.) Also, what about scope identifiers? (e.g. functions which are
  8. // members of a class are usually defined outside of the class declaration
  9. // in C++.)
  10. public class ComputeMortgage
  11. {
  12. // pre: string asking for a value is passed in
  13. // post: displays msgbox asking user for double and returns what user enters
  14. static double getDouble(String askFor)
  15. { return Double.parseDouble(JOptionPane.showInputDialog(null, askFor)); }
  16. // pre: string asking for a value is passed in
  17. // post: displays msgbox asking user for integer and returns what user enters
  18. static int getInt(String askFor)
  19. { return Integer.parseInt(JOptionPane.showInputDialog(null, askFor)); }
  20. // pre: interest rate, number of years for loan, and loan amount passed in
  21. // post: returns monthly loan payment
  22. // used as helper function for void ShowPayment(double, int, double)
  23. static double monthlyPayment(double rate, int years, double amount)
  24. { return amount*rate/(1 - (Math.pow(1/(1 + rate), years*12))); }
  25. // pre: monthly loan payment and number of years for loan passed in
  26. // post: returns total payment after loan + interest is payed off
  27. // used as helper function for void totalPayment(double, int)
  28. static double totalPayment(double monthly, int years)
  29. { return monthly*years*12; }
  30. // pre: interest rate, number of years for loan, and loan amount passed in
  31. // post: calculates and displays monthly and annual payment
  32. static void showPayment(double annualRate, int numYears, double loanAmount)
  33. {
  34. double m = monthlyPayment(annualRate, numYears, loanAmount);
  35. double a = totalPayment(m, numYears);
  36. JOptionPane.showMessageDialog(null, "Monthly Payment = $" + m);
  37. JOptionPane.showMessageDialog(null, "Total Payment = $" + a);
  38. }
  39. // pre: none
  40. // post: computes how much $$$ is required to pay off a loan
  41. public static void main(String[] args)
  42. {
  43. double annualRate = getDouble("Enter Annual Interest Rate")/100.0;
  44. int numYears = getInt("Enter number of Years");
  45. double loanAmount = getDouble("Enter loan Amount");
  46. showPayment(annualRate, numYears, loanAmount);
  47. }
  48. }
Comments (Newest First)
diamonddog | Newbie Poster | May 2nd, 2005
Thank you so much for this sample code. I am a spatial learner and do well with examples. Thank you again
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 4:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC