1 Reply Latest reply: Mar 28, 2013 1:18 AM by Kayaman RSS

    Stuck on issue with Date and Loan class for monthly payment calculation

    999594
      Hello: I am stuck a bit on some code. I believe that I have the first class, the Date class, done correctly and I believe that I have most of the second class done correctly but am unsure of a part in the loan class requirements. Here is what the program should do:

      Design a User-Defined class called Date.java with three attributes day, month and year. Implement an appropriate constructor, set and get methods – getMonth(), setMonth(), getYear(), setYear(), getDay(), and setDay().


      Next, design a User-Defined class called Loan.java, an object of which represents the real world Loan entity. Each loan object should store the following information:
      a. Annual Interest Rate
      b. Number of Years
      c. Loan Amount
      d. Loan Date (You would be specifying Loan Date of the type Date class you created earlier)

      Here are the different operations that could be done on any Loan object.
      a. Operation that returns the annual interest rate
      b. Operation that returns the number of years
      c. Operation that returns the loan amount
      d. Operation that returns the Loan Date as a String value providing the date information.
      e. Operation that sets a new annual interest rate
      f. Operation that sets a new number of years
      g. Operation that sets a new loan amount
      h. Operation that calculates and returns the monthly payment
      (Calculate the monthly payment using the following formulas:
      o Monthly payment = (mIR * M) / (1 – (1 / (1 + mIR)(12*nOY) ))
      where:
      o mIR = monthly interest rate (annual interest rate / 12)
      o nOY = number of years
      o M = Loan amount


      i. Operation that calculates the total payment amount over the life of the loan
      j. Operation that returns the overpayment amount


      I am a bit unsure on part d on how to call the loan date from the date class

      If I did part e annual interest rate correctly.

      and part h - I'm not sure where to implement this in to my code structure so that I can simply call it from the client

      Here is the code that I have for the Date and Loan classes, thank you for the guidance:
      public class Date
      {
      //declare instance variables
      
      private int day;
      private String month;
      private int year;
      
      //constructor
      public Date ( int day, String month, int year )
      {
      this.day = day;
      this.month = month;
      this.year = year;
      }
      
      /** constructor:
       * sets initial values of instance variables
       */
      public Date()
      {
      day = 0;
      month = "unknown";
      year = 0;
      }
      //method sets the day
      public void setDay ( int day )
      {
      this.day = day;
      }
      //method sets the month
      public void setMonth ( String month )
      {
      this.month = month;
      }
      //method sets the year
      public void setYear ( int year )
      {
      this.year = year;
      }
      //method returns the day
      public int getDay()
      {
      return day;
      }
      //method returns the month
      public String getMonth()
      {
      return month;
      }
      //method returns the year
      public int getYear()
      {
      return year;
      }
      
      
      }
      
      
      public class Loan
      {
         //declare variables
          private double annualInterestRate;
          private int numberOfYears;
          private double loanAmount;
          private String loanDate;
          
          //constructor
          public Loan ( double annualInterestRate, int numberOfYears, double loanAmount, String loanDate )
          {
              this.annualInterestRate = annualInterestRate;
              this.numberOfYears = numberOfYears;
              this.loanAmount = loanAmount;
              this.loanDate = loanDate;
          }
          
          /**constructor:
           * sets initial values of variables
           */   
          public Loan()
          {
              annualInterestRate = 0.0;
              numberOfYears = 0;
              loanAmount = 0.0;
              loanDate = "unknown";
          }
          
          //method sets the annual interest rate
          public void setAnnualInterestRate ( double annualInterestRate )
          {
              this.annualInterestRate = annualInterestRate;
          }
          //method sets the number of years
          public void setNumberOfYears ( int numberOfYears )
          {
              this.numberOfYears = numberOfYears;
          }
          //method sets the loan amount
          public void setLoanAmount ( double loanAmount )
          {
              this.loanAmount = loanAmount;
          }
          //method returns the annual interest rate
          public double getAnnualInterestRate()
          {
              return annualInterestRate;
          }
          //method returns the number of years
          public int getNumberOfYears()
          {
              return numberOfYears;
          }
          //method returns the loan amount
          public double getLoanAmount()
          {
              return loanAmount;
          }
          
      }
       
        • 1. Re: Stuck on issue with Date and Loan class for monthly payment calculation
          Kayaman
          996591 wrote:
          I am a bit unsure on part d on how to call the loan date from the date class
          Your Loan class doesn't have the Date class at the moment, you have it as a String. Change that.
          If I did part e annual interest rate correctly.
          You didn't post the code.
          and part h - I'm not sure where to implement this in to my code structure so that I can simply call it from the client
          Since it deals with the Loan object's data, the most natural place would be in the Loan class, don't you think?