Last Updated: 2017-09-18 Mon 17:27

CSCI 1103 Project 1: Input Money / Output Money

CODE DISTRIBUTION: p1-code.zip

CHANGELOG:

1 Introduction

This project will provide an opportunity for students to exercise newly acquired input and output skills. The problems below build on experience gained from lab and in-class exercises to "solve" some limited though semi-realistic problems. Students will need to know about printing messages to the screen, prompting and retrieving input from a user, how to use external function calls, and the difference between integer division and remainder operators.

2 Download Lab Code and Setup

As in Lab01, download the code pack linked at the top of the page. Unzip this which will create a folder and create your files in that folder.

File State Notes
TextIO.java Provided Allows easy input calls like TextIO.getInt()
LoanCalcTests.java Testing Tests for problem 1
MakeChangeTests.java Testing Tests for problem 2
P1Tests.java Testing Runs all project tests from command line
junit-1103.jar Testing For testing, don't try to edit this one
LoanCalc.java Create Create this file for Problem 1
MakeChange.java Create Create this file for Problem 2

3 Problem 1: Loans and Interest

Loans are an inevitable part of life: often one just doesn't have enough money for large purchase such as a house, car, or college education. Many modern loans work as follows:

  • A lender (bank) will give a large quantity of money to the borrower (student)
  • The loan is to paid back over some number of years such as 5 called the loan period.
  • Each month during the loan period, the borrower pays the lender back some amount of money; for 5 years it would 12 payments per year for a total of 60 payments. The number of monthly payments is fixed up front when the lender and borrower agree to the loan terms.
  • The complication comes from the fact lenders want interest on their loan, additional money charged for the service of lending money. An interest rate might be low, such as 2%, or high such as 20%.
  • Usually the yearly interest rate is stated but what is actually used in interest computations. A 12% yearly interest rate would mean a 1% rate each month while an 18% yearly rate would mean 1.5% per month. (Some might think this is done just to confuse borrowers and they might be right)
  • Each month, before the borrower pays, the amount owed increases some based on the monthly interest rate. If the annual interest rate is 18%, the monthly rate is
    18/12 = 1.5% 
    

    A current loan of $200 would increase by

    200 * 1.5% = 200 * 0.015 = $3.00
    

    meaning before the borrower pays anything, $203.00 are owed.

The goal of this project is write a short program to compute the monthly payment for a loan: the minimum amount of money that must be paid every month in order to pay off a loan over the loan term.

A smart cookie worked out a formula long ago that relates loan parameters to the monthly payment. Note that the below is a proper mathematical equation but we will be using the right hand side to figure out the left side.

loaneq.png

Figure 1: MONTHLY LOAN PAYMENT FORMULA

  • P is the monthly payment, the number we are interested in calculating based on the other quantities.
  • i is the monthly interest rate as a decimal, which is the yearly interest rate divided by 12, then divided by 100 to get a decimal rather than a percent.
  • A is the loan amount, the initial amount give to the borrower by the lender.
  • N is the total number of monthly payments, which is the loan term in years multiplied by 12

You may wish to refer to some reading material if you find the ideas of loans confusing or are struggling with the above formula.

3.1 Basic Approach

Create a file named LoanCalc.java which will contain a main() method where the action happens. The program should do the following.

  1. Prompt the user for the three necessary pieces of information
    • The loan amount
    • The yearly interest rate as a percentage
    • The loan term in years

    Use output messages identical to those in the demo to ensure that you are compatible with the tests.

  2. Compute the monthly interest rate by dividing the yearly interest rate by 12 and then by 100 to get the decimal value
  3. Compute the number of monthly payments by multiply the years of the loan term by 12
  4. Compute the numerator of the right side of the MONTHLY LOAN PAYMENT FORMULA above
  5. Compute the denominator of the formula
  6. Compute the monthly payment by dividing the numerator by the denominator
  7. Print out the monthly payment
  8. Print out the total cost of the loan which is the monthly payment multiplied by the number of monthly payments
  9. Compute the total interest on the loan which is the total cost minus the loan amount

A few notes

  • While the description of the code above is fairly detailed, there may be a few steps that require several lines of code such as the first one. Always break complex tasks into smaller, simpler steps to stay sane.
  • In step 5, the denominator requires raising a number to a power. This will be discussed in lecture but is best accomplished with a call to Math.pow() as demonstrated below.
    double twoToFive = Math.pow(2.0, 5.0);
    double x = 3.1;
    double y = 6.8;
    double xToY = Math.pow(x, y);
    
  • A common mistake is to forget that the interest rate read from the user is a percentage that needs to be converted to a decimal/fraction number. This is done just by dividing by 100 as indicated in step 2.
  • The values printed in this program should appear nicely as dollars with two digits of cents; see the examples below. This will require the use of the printf() method as discussed in lecture.

3.2 Demo

The LoanCalc program in demonstrated below. Use the samples to more easily generate the required output messages. Lines that start with numbers are the typed by the user.

> javac LoanCalc.java

> java LoanCalc
Enter Loan Amount:
225000.00
Enter Yearly Interest Rate (percent):
7.8
Enter Years for Loan:
30
Monthly Payment is $1619.71
Total Cost for loan is $583095.11
Total Interest for loan is $358095.11

> java LoanCalc
Enter Loan Amount:
1000
Enter Yearly Interest Rate (percent):
10
Enter Years for Loan:
1
Monthly Payment is $87.92
Total Cost for loan is $1054.99
Total Interest for loan is $54.99

3.3 Problem 1 Manual Inspection Criteria (30%)   grading

The criteria below will be examined by graders to account for part of your grade.

Wgt Criteria
5% Make use of the TextIO class to get input from the user
5% Include some comments about how the complex equation for loan payment is calculated
5% Use some intermediate variables for values like the monthly interest rate to improve clarity
5% Use descriptive variable names in camelCase rather than 1 letter names like i
5% Use the printf() function to easily format output nicely with 2 digits of decimal accuracy
5% Make use of the Math library functions to do exponentiation
30% Total weight for these criteria

4 Problem 2: Change We Can Spend

An old problem in retail is to "make change" after a customer pays. A cashier must give back some quantity such as $1.67 but wants to do so with as few bills and coins as possible. We will limit our consideration to the following kinds of bills/coins:

Bill/Coin Cents
Dollar bills 100
Quarters 25
Dimes 10
Nickels 5
Pennies 1

There are a variety of coin combinations which could be used to total 167 cents ($1.67) such as

  • 16 dimes and 7 pennies = 23 bills/coins
  • 6 quarters, 3 nickels, 2 pennies = 11 bills/coins
  • etc.

Most folks with experience will quickly realize though that the smallest number of bills/coins can be gotten via the following process.

Operation Coins Cents Left from 167
Give as many dollars as possible 1 100 67 cents left
Give as many quarters as possible 2 50 17 cents left
Give as many dimes as possible 1 10 7 cents left
Give as many nickels as possible 1 5 2 cents left
Give remainder in pennies 2 2 0 cents left
Total coins 7 167 Total cents

The trick is to start with large value bills/coins and use as many as possible then move on to the next lower value. Note also that this process can exploit quotients and remainders as follows:

  • 167 / 100 = 1 remainder 67
  • 67 / 25 = 2 remainder 17
  • etc.

It is an interesting property of the US money system that this process works and if the treasury and chosen different values for coins it might NOT work. For the moment, this process can be exploited to make a simple change calculator.

4.1 Basic Approach

In the file MakeChange.java write a main() method which.

  1. Prompts the user for the number of cents required, an integer
  2. Prints out the number of each type of bill/coin required to make the change. This should the minimum possible which is determined by the process described above.

There are several ways one can go about this problem all rely on the principle that large bills/coins are given first and then deducted from the running total. The required approach is to use the modulo operator % to get remainders after dividing out large-ish coins. Recall that integer division with / returns a quotient while modulo with % returns a remainder. This enables something like the following.

int centsLeft = 367;
int dollars = centsLeft / 100;  // 3 dollars
centsLeft   = centsLeft % 100;  // 67 cents left

4.2 Demo

> javac MakeChange.java

> java MakeChange
Enter required change in pennies (ex: 167): 
167
Dollars: 1
Quarter: 2
Dimes  : 1
Nickels: 1
Pennies: 2

> java MakeChange
Enter required change in pennies (ex: 167): 
499
Dollars: 4
Quarter: 3
Dimes  : 2
Nickels: 0
Pennies: 4

> java MakeChange
Enter required change in pennies (ex: 167): 
308
Dollars: 3
Quarter: 0
Dimes  : 0
Nickels: 1
Pennies: 3

4.3 Problem 2 Manual Inspection Criteria (20%)   grading

The criteria below will be examined by graders to account for part of your grade.

Wgt Criteria
5% Make use of the TextIO class to get input from the user
5% Make use of the modulo (%) operator to calculate remainders
5% Use some intermediate variables for values like the number of dimes
5% Use descriptive variable names in camelCase rather than 1 letter names like i
20% Total weight for these criteria

5 Automatic Testing (50%)   grading

5.1 Running Tests

Run the tests associated with the project and ensure all of them pass. This can be done in DrJava by pressing the Test button when test files are open.

On the command line you can run all tests with the following commands.

On Unix/Mac platforms, use a terminal to run the following commands in the folder where your code exists.

> javac -cp .:junit-1103.jar P1Tests.java   #compile
> java -cp .:junit-1103.jar P1Tests         #run tests

On windows, use cmd.exe and change to the correct directory. Run the commands below where which use a semicolon (;) rather than a colon (:).

> javac -cp .;junit-1103.jar P1Tests.java   #compile
> java -cp .;junit-1103.jar P1Tests         #run tests

Correctly passing all tests will yield output similar to the following.

JUnit version 4.12
..........
Time: 0.024

OK (10 tests)

Failing some tests will yield long messages which hint at where there might be a problem. Failures always end with the number of tests passed/failed as in the following.

FAILURES!!!
Tests run: 10,  Failures: 5

5.2 Test Grading Policies

Some policies to keep in mind with respect to how automatic tests affect grades.

  • Credit on this section is proportional to the number of tests passed.
  • If there were 30 total tests for the project…
  • Passing 30 / 30 tests gets 30 / 30 * 50 = 50.00
  • Passing 15 / 30 tests gets 15 / 30 * 50 = 25.00
  • Passing 4 / 30 tests gets 4 / 30 * 50 = 6.67
  • A 5% penalty may be deducted from this portion if code does not compile initially but a grader is able to quickly fix the problem.
  • Passing no tests because code does not compile or does not work properly gets 0 credit.

6 Zip and Submit

6.1 Submit to Canvas

Once your are confident your code is working, you are ready to submit. Ensure your folder has all of the required files. Create a zip archive of your lab folder and submit it to blackboard.

On Canvas:

  • Click on the Assignments section
  • Click on the appropriate link for this lab
  • Scroll down to "Attach a File"
  • Click "Browse My Computer"
  • Select you Zip file and press OK

6.2 Late Policies

You may wish to review the policy on late project submission which will cost you late tokens to submit late or credit if you run out of tokens. No projects will be accepted more than 48 hours after the deadline.

http://www-users.cs.umn.edu/~kauffman/1103/syllabus.html#late-projects


Author: Chris Kauffman (kauffman@umn.edu)
Date: 2017-09-18 Mon 17:27