Hi guys,
So my program does run in Eclipse, however, there are a few fixes that are still needed and I’m looking for simple suggestions/ideas. I’m saying ‘simple’ because I’m still learning plus I don’t want to delve into anything, just yet, my class hasn’t covered.
QUESTION 1: I realize my decimals needed to be round up for the outputs regarding money. From what I’ve been reading using double or flat is considered a ‘sin’. I think I could fix it by using the primitive type int instead, but the assignment was pretty clear on using the primitive type double. What do you guys think?
QUESTION 2: Two of my instance variables are giving the following message? Any suggestions?
“The field Amortization.intPerPd is never read locally”
“The field Amortization.numPmts is never read locally”
QUESTION 3: I know the assignment says the three methods below should be, “called every time getPmtAmt() or displayResults() is called”. So far I’ve only added it to public double getPmtAmt() method, but not the public void displayResults() method. Any suggestions for this?
private double calcNumPmts()
private double calcPmtAmt()
public double getPmtAmt()
If I’m not asking the right questions, I apologize. I’m still trying to ‘grasp’ the concept of programming.
Including the following:
1. Assignment
2. Amortization.java
3. AmortizationClient.java (works fine)
Thank you.
This is the assignment. You are to write a Java program that calculates the amount of a loan payment. The user will enter a loan amount, the number of payments per year, the number of years, and the interest rate. The program will then output the amount of the loan payments. You will use the formula: i PMT = PV --------------- 1 - (1 + i)-n Where PMT = payment amount PV = amount of loan (present value) i = rate per period (annual interest rate divided by payments per year) n = number of payments (number of years times payments per year) You will create a class called Amortization that contains several methods and instance variables and another class called AmortizationClient that uses the Amortization class. The instance variable that you will use will be: private double loanAmt; private double annIntRate; private double numYears; private double pmtsPerYear; private double intPerPd; private double numPmts; private double pmtAmt; The methods that class Amortization will contain are prototyped below: public Amortization (); public Amortization (double aLoanAmt, double anAnnIntRate); public Amortization (double aLoanAmt, double anAnnIntRate, double aNumYears, double aPmtsPerYear); public void explainProgram(); public void getLoanParameters(); public double getLoanAmt(); public void setLoanAmt(double aLoanAmt); public double getAnnIntRate(); public void setAnnIntRate(double anAnnIntRate); public double getNumYears(); public void setNumYears(double aNumYears); public double getPmtsPerYear(); public void setPmtsPerYear(double aPmtsPerYear); private double calcIntPerPd(); private double calcNumPmts(); private double calcPmtAmt(); public double getPmtAmt(); public void displayResults(); Method explainProgram() will explain the purpose of the program. Method getLoanParameters() will ask the user to input all four loan parameters. Methods calcIntPerPd(), calcNumPmts(), and calcPmtAmt() calculate the utility instance variables. These should be called every time getPmtAmt() or displayResults() is called. displayResults() displays the four loan parameters the user inputs and the payment amount. The dialog will appear as follows (italicized text in text boxes): This program will ask the user to input four loan parameters and will then display the payment amount. (Be more verbose than this in your program.) Please enter the loan amount: 5000.00 Please enter the number of payments per year: 12 Please enter the number of years: 10 Please enter the interest rate (in percent): 7 The loan amount is $5000.00 The number of payments per year is 12 The number of years is 10 The interest rate is 7% The amount of the loan payments will be $58.05.
import java.util.Scanner; public class Amortization { private double loanAmt; //INPUT 1 private double annIntRate; //INPUT 4 private double numYears; //INPUT 3 private double pmtsPerYear; //INPUT 2 private double intPerPd; //interest per period (interest in decimals / 12 months) private double numPmts; //total number of payments (12 months * X years = numPmts) private double pmtAmt; //Payment AMount (Loan formula) public Amortization () { set(0, 0, 0, 0); } public Amortization (double aLoanAmt, double aAnnIntRate) { set(aLoanAmt, aAnnIntRate, 0, 0); } public Amortization(double aLoanAmt, double aAnnIntRate, double aNumYears, double aPmtsPerYear) { set(aLoanAmt, aAnnIntRate, aNumYears, aPmtsPerYear); } public void explainProgram() //Explain purpose of the program { System.out.println("This program calculates the amount of a loan payment."); } public void getLoanParameters() //will ask the user to input all four loan parameters { Scanner keyboard = new Scanner(System.in); System.out.println("Please enter the loan amount: "); loanAmt = keyboard.nextDouble(); System.out.println("Please enter the number of payments per year: "); pmtsPerYear = keyboard.nextDouble(); System.out.println("Please enter the number of years: "); numYears = keyboard.nextDouble(); System.out.println("Please enter the interest rate (in percent): "); annIntRate = keyboard.nextDouble(); } public double getLoanAmt() { return loanAmt; } public void setLoanAmt(double aLoanAmt) { set(aLoanAmt, annIntRate, numYears, pmtsPerYear); } public double getAnnIntRate() { return annIntRate; } public void setAnnIntRate(double aAnnIntRate) { set(loanAmt, aAnnIntRate, numYears, pmtsPerYear); } public double getNumYears() { return numYears; } public void setNumYears(double aNumYears) { set(loanAmt, annIntRate, aNumYears, pmtsPerYear); } public double getPmtsPerYear() { return pmtsPerYear; } public void setPmtsPerYear(double aPmtsPerYear) { set(loanAmt, annIntRate, numYears, aPmtsPerYear); } private void set(double aLoanAmt, double aAnnIntRate, double aNumYears, double aPmtsPerYear) { loanAmt = aLoanAmt; annIntRate = aAnnIntRate; numYears = aNumYears; pmtsPerYear = aPmtsPerYear; } private double calcIntPerPd() //calculate the utility instance variables, //called every time getPmtAmt() or displayResults() is called //interest per period (interest rate / 100 / 12 months) { return intPerPd = annIntRate / 100 / 12; } private double calcNumPmts() //calculate the utility instance variables //called every time getPmtAmt() or displayResults() is called //total number of payments (12 months * X years = numPmts) { return numPmts = numYears * pmtsPerYear; } private double calcPmtAmt() //calculate the utility instance variables //called every time getPmtAmt() or displayResults() is called //Payment AMount (Loan formula) { return pmtAmt = (calcIntPerPd() * loanAmt) / (1 - Math.pow((1 + calcIntPerPd()),(-1 * calcNumPmts()))); } public double getPmtAmt() { calcIntPerPd(); calcNumPmts(); calcPmtAmt(); return pmtAmt; } public void displayResults() //displays the four loan parameters the user inputs //and the payment amount. { System.out.println("The loan amount is: $" + loanAmt); System.out.println("The number of payments per year is: " + pmtsPerYear); System.out.println("The number of years is: " + numYears); System.out.println("The interest rate is: " + annIntRate + "%"); System.out.println("The amount of the loan payments will be: $" + getPmtAmt()); } }
public class AmortizationClient { public static void main(String[] args) { Amortization calculate = new Amortization(); calculate.explainProgram(); calculate.getLoanParameters(); calculate.displayResults(); } }