package mortgage;//Mortgage.java: Encappsulate mortgage information
public class Mortgage {
private double annualInterestRate; private int numOfYears; private double loanAmount; /** Default constructor */
public Mortgage() {
this(7.5, 30, 100000);
} /**
 * Construct a mortgage with specified annual interest rate,number of years
 * and laon amount
 */
public Mortgage(double annualInterestRate, int numOfYears, double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numOfYears = numOfYears;
this.loanAmount = loanAmount;
} /** Return annualInterestRate */
public double getAnnualInterestRate() {
return annualInterestRate;
} /** Set a annualInterestRate */
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
} /** Return numOfYears */
public int getNumOfYears() {
return numOfYears;
} /** Set a new numOfYears */
public void setNumOfYears(int numOfYears) {
this.numOfYears = numOfYears;
} /** Return loanAmount */
public double getLoanAmount() {
return loanAmount;
} /** Set a new loanAmount */
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
} /** Find monthly payment */
public double monthlyPayment() {
double monthlyInterestRate = annualInterestRate / 12;
return loanAmount
* monthlyInterestRate
/ (1 - (Math
.pow(1 / (1 + monthlyInterestRate), numOfYears * 12)));
} /** Find total payment */
public double totalPayment() {
return monthlyPayment() * numOfYears * 12;
}
}
这是Test程序package mortgage;import javax.swing.JOptionPane;public class TestMortgageClass { /** Main method */
public static void main(String[] args) { // Enter yearly interest rate
String annualInterestRateString = JOptionPane.showInputDialog(null,
"Enter yearly interest rate,for example 8.25:", "Input",
JOptionPane.QUESTION_MESSAGE); // Convert string into double
double annualInterestRate = Double
.parseDouble(annualInterestRateString); // Enter number of years
String numOfYearsString = JOptionPane.showInputDialog(null,
"Enter number of years,\nfor example 8:", "Input",
JOptionPane.QUESTION_MESSAGE); // Convert string to int
int numOfYears = Integer.parseInt(numOfYearsString); // Enter loan amount
String loanAmountString = JOptionPane.showInputDialog(null,
"Enter loan amount,\nfor example 120000.95:", "Input",
JOptionPane.QUESTION_MESSAGE); // Convert string to double
double loanAmount = Double.parseDouble(loanAmountString); // Create Mortgage object
Mortgage mortgage = new Mortgage(annualInterestRate, numOfYears,
loanAmount); // Format to keep two
double monthlyPayment = (int) (mortgage.monthlyPayment() * 100) / 100.0; double totalPayment = (int) (mortgage.totalPayment() * 100) / 100.0; // Display result
StringBuffer output = new StringBuffer();
output.append("The monthly payment is " + monthlyPayment
+ "\nThe total payment is " + totalPayment); JOptionPane.showMessageDialog(null, output, "Output",
JOptionPane.INFORMATION_MESSAGE); System.exit(0); }}
Mortgage里面的所有set和get方法在测试程序里面都没什么用啊.....为什么还要这些set和get方法呢??后者说是为了别的Test程序用的...