CS 161 Homework 2 - Banking
Due Wed Sep 18 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
- To practice defining and calling methods in Java
- To practice working with variables and mathematical operations
- To explore how methods can be used to encapsulate code and make writing programs easier.
Necessary Files
You will be creating your own program from scratch for this assignment---select "Project > New Project" from the BlueJ menu to create a new project.
You will need to download a copy of the README.txt (right-click on the link and select "Save Link As" to download the file) to fill out and turn in along with your assignment.
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
-
The first thing you'll need is a class in which to put your methods (since all code goes in classes in Java). Create a new class called
Banking
. Be sure and delete all the sample code except the class definition: Inside this class you will be creating new methods for calculating interest and calculating change. These methods are detailed below.-
Important Note: these methods won't necessary rely on an object to work--we don't need a "Banker" object to calculate interest, the same way we don't use a "Mathematician" object to add numbers or calculate cosine. For this reason we will make the methods in the Banking class static, meaning that they are methods called on the class not the object. We make a method static by including the
static
keyword in the method signature, for example:public static void myMethod() { ... }
Yes, this is the same static word that is used in the main method (which is why we can call that method on a class, rather than making an object)!
-
Important Note: these methods won't necessary rely on an object to work--we don't need a "Banker" object to calculate interest, the same way we don't use a "Mathematician" object to add numbers or calculate cosine. For this reason we will make the methods in the Banking class static, meaning that they are methods called on the class not the object. We make a method static by including the
- You should also make a new
BankingTester
class (see below for details). Do this early!
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.
-
In the
Banking
class, Write a method calledfirstQuarterEarnings
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?
- Be careful to convert your interest rate from a percentage to a decimal.
- Remember to make the method static! You can test your method by calling it on the class in BlueJ, or by using your tester class (see below).
-
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
Banking
class, write a method calledcalculateLongTermInterest
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
Update: fixed the reported values in the above example. 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
WhereF
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, andT
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 withMath.pow(3,4)
. - Remember to use parantheses to group terms and make sure that you're multiplying or dividing the right terms!
- And remember to make this method static as well!
-
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
Calculating Change
The second piece of functionality you'll create for your Banking
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!)
-
In the
Banking
class, write a method calledvalueOfChange
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, te above example should not give $1.8199999999999998 as the value).- Fixing this is actually somewhat tricky, so I'll give you 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'. 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 along
(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!
- Fixing this is actually somewhat tricky, so I'll give you 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
-
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 static 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 theMath.pow()
orMath.round()
methods. For example:int result = myMethod(); //this stores whatever value myMethod() returned!
I highly recommend that you 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.
Tester Class
You should also create a new BankingTester
class that demonstrates and tests the methods you wrote. You should do this early, before you write other methods!
-
This class should have in it a
main
method, which you use to call the methods in yourBanking
class. For example, if there is a methodmyMethod()
in yourBanking
class, you can call it with:Banking.myMethod();
In order to do this,myMethod()
must be static (see above). -
Your main method should include demonstrations of all 4 of your
Banking
methods. This makes it very easy to test that things are working using lots of cases. -
In your main method, be sure and use Sysout calls to print out WHAT is being tested. For example:
Testing firstQuarterEarnings() with $1000 at 6% 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 Testing firstQuarterEarnings() with $1 at 1% ...
-
For your
calculateLongTermInterest()
method, be sure and include a test that shows whether the Rule of 72 holds up! - Similarly, this is a nice way to easily test lots of different amounts of coins. Just check that the output makes sense!
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):
- Clear screen at method calls will clear old output each time you run the program. This can make debugging a lot easier, since you won't get lost on which output is from which program version
- Unlimited buffering will let your terminal show as many printed lines as your program contains--it lets yous scroll back to the top without having older printouts disappear!
Extensions
If you're feeling adventurous, you might write a separate methods that rounds dollar amounts to the correct penny, and then call that method in your other classes so that everything is formatted correctly.
Submitting Your Assignment
- Be sure and test your program thoroughly to make sure it works!
- Also add in some comments about your code, explaining how it works.
- Remember to download out the README.txt file and to place it in your project directory. Once you've downloaded it you can open it in BlueJ by double-clicking on the icon.
-
Upload the entire Banking project directory (including both the
Banking.java
andBankingTester.java
files, as well as the README.txt) to the Hwk2 submission folder on the hedwig server. Make sure you upload your work to the correct folder!. - This assignment is due at midnight on Wed, Sep 18.
Grading
This assignment will be graded on approximately the following criteria:
- A
Banking
class with static methods [5%] - A working
firstQuarterEarnings
method [15%] - A working
calculateLongTermInterest
method [20%] - A working
valueOfChange
method [20%] - A working
consolidateChange
method [20%] - A
BankingTester
class with a main method that demonstrates the 4 methods [10%] - Your code is properly formatted (indentation, etc), has comments, and is otherwise readable [5%]
- You have completed the included README.txt file [5%]