Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 754891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:03:59+00:00 2026-05-14T15:03:59+00:00

I have a Loan class that in its printPayment method, it prints the amortization

  • 0

I have a Loan class that in its printPayment method, it prints the amortization table of a loan for a hw assignment. We are also to implement a print first payment method, and a print last payment method. Since my calculation is done in the printPayment method, I didn’t know how I could get the value in the first or last iteration of the loop and print that amount out.

One way I can think of is to write a new method that might return that value, but I wasn’t sure if there was a better way. Here is my code:

public abstract class Loan
{   
    public void setClient(Person client)
    {
        this.client = client;
    }

    public Person getClient()
    {
        return client;
    }

    public void setLoanId()
    {
        loanId = nextId;
        nextId++;
    }

    public int getLoanId()
    {
        return loanId;
    }

    public void setInterestRate(double interestRate)
    {
        this.interestRate = interestRate;
    }

    public double getInterestRate()
    {
        return interestRate;
    }

    public void setLoanLength(int loanLength)
    {
        this.loanLength = loanLength;
    }

    public int getLoanLength()
    {
        return loanLength;
    }

    public void setLoanAmount(double loanAmount)
    {
        this.loanAmount = loanAmount;
    }

    public double getLoanAmount()
    {
        return loanAmount;
    }

    public void printPayments()
    {
        double monthlyInterest;
        double monthlyPrincipalPaid;
        double newPrincipal;
        int paymentNumber = 1;
        double monthlyInterestRate = interestRate / 1200;
        double monthlyPayment = loanAmount * (monthlyInterestRate) / 
                                (1 - Math.pow((1 + monthlyInterestRate),( -1 * loanLength)));

        System.out.println("Payment Number | Interest | Principal | Loan Balance");     

        // amortization table
        while (loanAmount >= 0) {
            monthlyInterest = loanAmount * monthlyInterestRate;
            monthlyPrincipalPaid = monthlyPayment - monthlyInterest;
            newPrincipal = loanAmount - monthlyPrincipalPaid;
            loanAmount = newPrincipal;


            System.out.printf("%d, %.2f, %.2f, %.2f", paymentNumber++, monthlyInterest, monthlyPrincipalPaid, loanAmount);
        }
    }
    /*
    //method to print first payment
    public double getFirstPayment()
    {
    }

    method to print last payment
    public double getLastPayment()
    {
    }*/

    private Person client;
    private int loanId;
    private double interestRate;
    private int loanLength;
    private double loanAmount;
    private static int nextId = 1;

}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:03:59+00:00Added an answer on May 14, 2026 at 3:03 pm

    You’ve already identified that the printPayments(), printFirstPayment() and printLastPayment() methods have common logic. You generally want to minimize duplication of such code and the two common ways to do this are:

    1. Implement all but one of the methods in terms of one of them; or

    2. Implement all the methods in terms of a private method.

    So, for example:

    public void printPayments() {
      for (Payment : getPayments()) {
        printPayment(payment);
      }
    }
    
    public void printFirstPayment() {
      printPayment(getPayments().get(0));
    }
    
    public void printLastPayment() {
      List<Payment> payments = getPayments();
      printPayment(payments.get(payments.size()-1));
    }
    
    private void printPayment(Payment payment) {
      ...
    }
    
    private List<Payment> getPayments() {
      ...
    }
    

    Now this is homework so you may not have come across the syntax List<Payment> yet. If not, it’s generics. There are other ways to do this: using a non-generic Collection or using arrays for example.

    The points I wanted to illustrate here is that:

    1. The logic for creating the payments and displaying them has been separated;

    2. A single method getPayments() does the calculations and returns a List of Payment objects. Payment is a new object in this mock up;

    3. All three methods are implemented in terms of getPayments() and printPayment().

    So I hope this leads you in the right direction. The concept here I guess is functional composition, composing your functions in terms of other functions and making your internal functions granular enough to be grouped together usefully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables Loan(Parent Table) and Receipt(Child table)what i want to do is
I Have a database called 'lms' with two tables loan and value, table loan
I have a table called tbl_loans which has the field loan_number . Loan numbers
Here's the c# code that I have: private double get806Fees (Loan loan) { Loan.Fee.Items
I have one class that declares an enumeration type as: public enum HOME_LOAN_TERMS {FIFTEEN_YEAR,
I have one class called Person that basically looks like: public class Person {
I have servlet that does the following: public class LoginServlet extends HttpServlet { protected
I have an application that allows, using an abstract class, people to write their
I have a simple class that imports another class from another jar. Everything compiles
Needed some help with my logic again. Assuming the Loan class exists, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.