Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Amortization Loan, works, but need suggestions please.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Amortization Loan, works, but need suggestions please.

    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();
     
     
     
    	}
    }


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Amortization Loan, works, but need suggestions please.

    Hi TheWhopper858,

    You will get a The field "[variableName] is never read locally" warning when an attributeis not being read by the class that contains it. Good practice is to always access class attributes via get/set methods. This is for many reasons including readability, security, maintainability, auditing, etc. If you create a get method of each of the attributes, this warning should go away.

    Your code is looking great, especially since you claim to still be getting a grasp on the concepts. Don't be afraid to make variable/method names a little bit longer to make them easier to read and understand, though in this case it looks like you have some guidelines that you need to follow.
    When writing a method/statement, try keep grouped togeather and place comments (Great that you have them!) above, or somewhere which will not break the flow of the code. e.g
            /*
                displays the four loan parameters the user inputs and the payment amount.
            */
    	public void displayResults()
            {
                 // Do stuff
    	{

    This doesn't add any value functionality, but when you, or someone else, comes back to read your code, it makes it a lot easier to follow.

    The final thing which jumped out at me is the blanket set method. I would stay away from using one method to set every variable, if you need to set all of them, e.g in the constructor, just call them sequentially.

    		setLoanAmt(xxx);
    		setAnnIntRate(xxx);
    		setNumYears(xxx);
    		setPmtsPerYear(xxx);

    Keep up the good work!

Similar Threads

  1. Loan Payments
    By clarafeb1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2011, 03:36 AM
  2. Suggestions how to do this?
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: September 2nd, 2010, 12:18 PM
  3. savings and loan society
    By 5723 in forum Java Theory & Questions
    Replies: 9
    Last Post: November 14th, 2009, 12:01 AM