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
- 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 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.
-
The methods in the
BankerTester
are allstatic
, which means that they are methods called on the class not an object. You can call them by simply right-clicking on the orange BankerTester class; you don't need to instantiate a newBankerTester
object.-
static
methods are useful when we don't need to discuss methods as being performed on or by a particular thing (object).
-
- The Tester has methods that show the expected output of the Banker's methods, as well as the actual output. You can check that your methods work by visually comparing if the two outputs are the same--if they are, then everything worked as expected. If not, then there is a logical bug that needs to be addressed.
- Note that this form of testing (called unit testing) only checks that a method works for a particular set of parameters. It's possible that something works for those parameters, but other values will cause the problem to break. You should test your program with a variety of different parameters, making sure that the output you expect is the output you get!.
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 that can perform the required functionality (e.g., where you can put your methods). Create a new class called
Banker
to represent objects that do 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.
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
Banker
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?
-
You should expect interest rate as a decimal
(a
double
). E.g., 5% interest should be entered as0.05
.
-
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 calledlongTermInterest
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
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 parentheses to group terms and make sure that you're multiplying or dividing the right terms!
-
Luckily, there is an easy formula for calculating compound interest over time:
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!)
-
In the
Banker
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, 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 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!
- 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
- 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 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
-
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 anint
or adouble
). You will also need to make the last line of the method use thereturn
keyword:public int myMethod() { return 10; //or some other value }
Math.pow()
orMath.round()
methods. For example:int result = myMethod(); //this stores whatever value myMethod() returned!
- 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):
- 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 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.
-
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!
- Add in some comments about your code, explaining how it works! Adding method comments are a good place to start.
-
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).
-
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!
- This assignment is due at midnight on Fri, Feb 06.
Grading
This assignment will be graded out of 20 points:
- [1pt] You have create a new Banker class with appropriate methods
- [1pt] The firstQuarterEarnings() method has an appropriate signature
- [1pt] The firstQuarterEarnings() method calculates interest correctly
- [1pt] The firstQuarterEarnings() method properly outputs its results
- [1pt] The longTermInterest() method has an appropriate signature
- [2pt] The longTermInterest() method correctly calculates compound interest
- [1pt] The longTermInterest() method properly outputs its results
- [1pt] The valueOfChange() method has an appropriate signature, including a return value
- [1pt] The valueOfChange() method method correctly calculates change value
- [1pt] The valueOfChange() method properly formats and rounds the final result
- [1pt] The valueOfChange() method both prints out and returns the resulting value of change
- [1pt] The consolidateChange() method has an appropriate signature
- [1pt] The consolidateChange() method re-uses your previous method(s)
- [2pt] The consolidateChange() method calculates the correct change amount
- [1pt] The consolidateChange() method properly outputs its results
- [1pt] Your code includes some comments, including a descriptive class comment at the top
- [1pt] Your code is properly formatted (indentation, etc) and methods/variables are properly named
- [1pt] You have completed the included README.txt file