CS 161 Homework 2 - Banking

Due Fri Feb 06 at 11:59pm

Overview

Computer science (and Java in particular) has a long history of use in business and finance. So for this homework, you will be coding a money program. You will implement a number of methods to help with common banking operations. You will be writing these methods from scratch--but don't panic, there will be lots of help to keep you on track!

This assignment should be completed individually.

Objectives

Necessary Files

You will need to download the Hwk2.zip file, which contains the BlueJ project for you to complete. Remember to unzip the contents from the file!. You should save the unzipped project to your Alexandria drive for safe-keeping.

While you will need to create a new class for this program, I have included one class for you: BankerTester contains methods used to test the Banker class you will be creating.

Assignment Details

Be sure and read the instructions through carefully before you begin working. Knowing the overall goal will help you work on the individual pieces

Calculating Compound Interest

Imagine that you open a bank account that accrues compound interest---that is, an account where the interest earns is itself earning interest. Interest is compounded monthly: so if you have $100 in the account and earn 6% a year in interest, then 0.5% (1/12th) of the current balance will be added to the balance each month.

  1. In the Banker class, Write a method called firstQuarterEarnings that, given an initial investment and annual interest rate, calculates and prints out the earnings during the first three months. Your method should have output similar to the following:
    Initial balance: $1000.0
    Annual interest: 6.0%
    Balance after first month: $1005.0
    Balance after second month: $1010.025
    Balance after third month: $1015.075125
    Note that you do not need to worry about formatting the numbers or rounding the pennies (though you can as an extension, see below). However, you should be sure to include the dollar signs, percentage sign, and put proper spacing in your print-out!
    • Think about what parameters this method should take, and what data type those parameters should be. How can you use (and reuse!) the variables in your method?
    • You should expect interest rate as a decimal (a double). E.g., 5% interest should be entered as 0.05.
  2. That's great, but what we'd really like is to see how much money the account will make in the long term. In the Banker class, write a method called longTermInterest that, given an initial investment, an annual interest rate, and a number of years, calculates and prints out the interest earned and the final value of the account. Your method should have output similar to the following:
    Initial balance: $1000.0
    Annual interest: 6.0%
    Interest earned in 5 years: $348.8501525493075
    Total value after 5 years: $1348.8501525493075
    Again, you don't need to format the numbers or round the pennies.
    • Luckily, there is an easy formula for calculating compound interest over time:
      F = P * (1+ R/N)N*T
      Where F is the final account balance, P is the initial deposit, R is the annual interest rate, N is the number of times per year that interest is compounded, and T is the number of years we're earning interest.
    • Note that this equation has exponential growth--you'll need to use an exponent to calculate the result. Luckily, exponents are easy to use in Java! The Math.pow() method lets you raise one number to the power of another: for example, you can calculate 34 with Math.pow(3,4).
    • Remember to use parentheses to group terms and make sure that you're multiplying or dividing the right terms!

Calculating Change

The second piece of functionality you'll create for your Banker class is to calculate the value of lots of coins. This is to help tellers deal with people who want to deposite multiple years worth of loose change (it happens!)

  1. In the Banker class, write a method called valueOfChange that takes in the numbers of different US coins (so how many quarters, dimes, nickels, and pennies) and prints out total value in dollars of those coins. Your method should have output similar to the following:
    # of quarters: 5
    # of dimes: 4
    # of nickels: 3
    # of pennies: 2
    Value of change: $1.82
    For this method, you will need to use correct formatting and rounding in the final amount. In other words, the above example should not give $1.8199999999999998 as the value. (You do not, however, need to force it to include a trailing 0---a result of $1.0 instead of $1.00 is acceptable).
    • Fixing this is actually somewhat tricky, so I'll outline the algorithm here. The basic idea is that we want to round off those extra decimal points--to round to the nearest 100th place. The Math class does include a .round() method, but it only rounds to the nearest whole number. So how can we use this tool? Try to think through this on your own before reading on!
      • The key is to realize that what we want to do is round to the nearest penny--so we need to convert our value that is in 'dollars' to a value that is in 'pennies'--making a new variable! This should be easy to do (how do you count how many pennies are in a dollar value?) Then you can use Math.round() on that new value.
      • But there's another tricky bit: if you use Math.round() on a double, Java will give you the result as a long (which is like an int, but can hold a bigger number). So you'll need to convert that into an int. You can do this by casting, using (int). For example:
        int x = (int)Math.round(0.999);
      • Last step! Now that you have an int representing the number of pennies, you should easily be able to convert that back to dollars to print it out. Watch out for integer division!
    • This is one of the hardest parts of the assignment, if you get stuck, please check in and I'm happy to help you out!
  2. Finally, write a separate method called consolidateChange that takes in the numbers of coins (just like the previous method) and prints out the simplest number of bills and coins needed to make that amount. This will let our intrepid bank teller easily produce the correct change for someone's coin collection. Your method should have output similar to the following:
    Coins given:
    # of quarters: 5
    # of dimes: 4
    # of nickels: 3
    # of pennies: 2
    Value of change: $1.82
    Coins returned: 
      0 fives
      1 ones
      3 quarters
      0 dimes
      1 nickels
      2 pennies
    I recommend you use things like white-space to make your output more readable!
    • You will likely find yourself wanting to copy code or a method that you've already written. But instead of copy and pasting code, you should re-use it (yes I know that copying is like re-using, but that's not what we mean in CS. We'll talk about code re-use more later). What you want to do is to be able to use the value that was calculated by a previous method inside another method. You can do this by returning the value that you calculated.
    • In order to have a method return a value, you will need to change the method signature so that instead of returning void, it returns a particular data type (such as an int or a double). You will also need to make the last line of the method use the return keyword:
      public int myMethod()
      {
        return 10; //or some other value
      }
      Then when you call that method, you can store its result in a variable, just like you do with the Math.pow() or Math.round() methods. For example:
      int result = myMethod(); //this stores whatever value myMethod() returned!
      You should modify your previous method in order to re-use it; this is excellent practice for the future!
    • Helpful advice: thinking through how to calculate the correct change isn't trivial. Think about how you might do it on paper (without the computer), or even with a stack of coins! Hint: try thinking in terms of pennies.

Development Notes

A quick note: your output should all appear in the Terminal window in BlueJ. If the terminal window isn't showing, you can see it by selecting "View > Show Terminal" from the BlueJ menu. Once you've got the terminal up, I highly recommend you select the following settings (make sure the terminal "has focus" and is the front window, and select the Options menu):

Extensions

If you're feeling adventurous, you might write a separate method that rounds dollar amounts to the correct penny, and then call that method in your other classes so that everything is formatted correctly. No extra credit for this, but it will make your life more satisfying.

Submitting Your Assignment

There are a couple of details to keep in mind before you submit your assignment.

  1. Double-check that your methods all work and give the correct output. Use the BankerTester to help you check!
    • If your program doesn't compile, I can't give you credit for it!
  2. Add in some comments about your code, explaining how it works! Adding method comments are a good place to start.
  3. You will also need to fill out the README.txt file. You can open this file in BlueJ by double-clicking on the white paper icon in the upper left corner of the class window. You should place the answer to each question below the question box, replacing the "<Replace this with your response!>". Remember to save your changes (select "Class > Save" from the menu).

  4. You should compress the entire Hwk2 folder into a single zip file (like you did for Lab A), and then submit it to the Hwk2 Submission page on Moodle.
    • Be sure an upload the ENTIRE project folder--that is what includes all your work!
  5. This assignment is due at midnight on Fri, Feb 06.

Grading

This assignment will be graded out of 20 points: