CS/SWE 332 Assignment 1
Due Wednesday, September 5, 2018


Goal: Basic familiarity with Java; Introduction to Contract Model.

Basic Assignment:
Consider a method that calculates the number of months needed to pay off a loan of a given size at a fixed annual interest rate and a fixed monthly payment. For instance, a $100,000 loan at an 8% annual rate would take 166 months to discharge at a monthly payment of $1,000, and 141 months to discharge at a montly payment of $1,100. (In both of these cases, the final payment is smaller than the others; I rounded 165.34 up to 166 and 140.20 up to 141.) Continuing the example, the loan would never be paid off at a monthly payment of $100, since the principal would grow rather than shrink.
Define a Java class called Loan. In that class, write a method that satisfies the following specification:

   public static int months (int principal, double annualRate, int monthlyPayment)
   
   Requires: principal, annualRate, and monthlyPayment all positive 
            and payment is sufficiently large to drive the principal to zero.
   Effects:  return the number of months required to pay off the principal

Note that the precondition is quite strong, which makes implementing the method easy. We will discuss specific methods of addressing this precondition in coming weeks. You should use double precision arithmetic internally, but the final result is an integer, not a floating point value. The key step in your calculation is to change the principal on each iteration with the following formula (which amounts to monthly compounding):

    newPrincipal = oldPrincipal * (1 + monthlyInterestRate) - payment;
The variable names here are explanatory, not required. You may want to use different variables, which is fine.

To make sure you understand the point about preconditions, your code is required to be minimal. Specifically, if it possible to delete parts of your implementation and still have it satisfy the requirements, you'll earn less than full credit.

Grading Criteria: