•
•
•
•
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
The JOptionPane class is used to give the CompMortgage.java program a front-end GUI interface. The class is divided into numerous functions.
package computemortgage; import javax.swing.JOptionPane; import java.text.DecimalFormat; // The following program gives the previous computeMortgage program a front-end // GUI interface. I wrote the functions from bottom to top because I wasn't // sure if they needed to be defined before they are used (as is necessary // in C++.) Also, what about scope identifiers? (e.g. functions which are // members of a class are usually defined outside of the class declaration // in C++.) public class ComputeMortgage { // pre: string asking for a value is passed in // post: displays msgbox asking user for double and returns what user enters static double getDouble(String askFor) { return Double.parseDouble(JOptionPane.showInputDialog(null, askFor)); } // pre: string asking for a value is passed in // post: displays msgbox asking user for integer and returns what user enters static int getInt(String askFor) { return Integer.parseInt(JOptionPane.showInputDialog(null, askFor)); } // pre: interest rate, number of years for loan, and loan amount passed in // post: returns monthly loan payment // used as helper function for void ShowPayment(double, int, double) static double monthlyPayment(double rate, int years, double amount) { return amount*rate/(1 - (Math.pow(1/(1 + rate), years*12))); } // pre: monthly loan payment and number of years for loan passed in // post: returns total payment after loan + interest is payed off // used as helper function for void totalPayment(double, int) static double totalPayment(double monthly, int years) { return monthly*years*12; } // pre: interest rate, number of years for loan, and loan amount passed in // post: calculates and displays monthly and annual payment static void showPayment(double annualRate, int numYears, double loanAmount) { double m = monthlyPayment(annualRate, numYears, loanAmount); double a = totalPayment(m, numYears); JOptionPane.showMessageDialog(null, "Monthly Payment = $" + m); JOptionPane.showMessageDialog(null, "Total Payment = $" + a); } // pre: none // post: computes how much $$$ is required to pay off a loan public static void main(String[] args) { double annualRate = getDouble("Enter Annual Interest Rate")/100.0; int numYears = getInt("Enter number of Years"); double loanAmount = getDouble("Enter loan Amount"); showPayment(annualRate, numYears, loanAmount); } }
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
•
•
•
•
DaniWeb Marketplace (Sponsored Links)