ITEC 120
Bills and Coins

Objectives

After successfully completing this lab you will be able to develop programs that use constants, variables and expressions.

Assignment

Develop a program named Money that determines how an amount of money can be represented using bills and coins.

To solve this problem, you will use constants, variables, and expressions.

Variables

A variable is a name for a memory location where you will store some piece of information you will use in your program. You make up the name. It's important to make up meaningful names for your variables - it makes your code easy to read, understand, and debug. The value you store there may change during the program's execution. That's why it is called a variable - it's value can vary.

To use a variable in your program, you must first declare it. A variable declaration simply states the variable's name and variable's type - which indicates what type of information is stored at that memory location. The value of a variable can change, but its type cannot.

Here are some variable declarations:

String name;
int pennies;
double accountBalance;

For this program, we are going to use the type int (integer) to store numbers of dollars, bills, coins, and so forth.

By convention, Java programmers name all variables using lowercase letters. If a variable name is made up of more than one word, all words except the first word will start with a capital letter, as in the example accountBalance above. This is called camel case. You cannot put spaces in a variable name.

The variables name, pennies, and accountBalance do not have values until they are initialized. To initialize a variable, an assignment statement is used. In Java, the assignment operator is the equals sign = . This can be confusing at first, because assignment does not mean the same thing as = means in mathematics.

Assignment means:
evaluate the expression on the right hand side and put the result in the variable on the left hand side.

Here are some assignment statements:

name = "Ada Lovelace";
pennies = 24 + 37;
accountBalance = initialBalance + deposits - withdrawels;


In the accountBalance example, notice the expression is made up of other variable names. In order for this line of code to compile, those three variables would need to have been declared as numerical types, and they would have to already have a value. The expression cannot be evaluated unless those variables have values.

The following is also a valid assignment statement, assuming pennies has a value when this line of code is reached:

pennies = pennies - 2;

This assignment statement takes whatever value was stored in pennies, takes 2 away, and puts that value back into pennies. Thus, pennies is two less than it was.

Now, back to the problem we are going to solve today:


Declare variables needed for calculating bills from a dollar amount

You are going to write a program that takes a dollar amount, and figures out how many twenties, tens, fives, and ones would be needed to total to that amount.

Here's an example output of this program:

57 dollars is:
     2 twenty dollar bills
     1 ten dollar bills
     1 five dollar bills
     2 one dollar bills

While it would be possible to simply write println statements to print the information above, that is not a useful program. You are going to write a program which calculates the number of bills given any dollar amount. To do this:

 

Constants

Constants are used to make code easier to read, understand, debug, and maintain. They are like variables, except that their value cannot change. Constants must be initialized when they are delcared, and their value will not change during the course of a program. Here are some example constant declarations:

final int EGGS_PER_DOZEN = 12;
final int INCHES_PER_FOOT = 12;
final String TITLE = "Moby Dick";

The Java keyword final is what makes these constants. The constants are getting their "final" value when they are declared. By convention, Java programmers name constants using all caps. If the constant name is made up of more than one word, an underscore is used to separate the words.

Some of you might think, "Wouldn't it be easier just to type 12 in a program, rather than EGGS_PER_DOZEN?" Maybe so, but, suppose you saw the number 12 in a program. What does that 12 mean? Maybe it means eggs in a dozen, or maybe it means inches in a foot, or maybe it means months in a year, or something else completely. A constant allows you to give a meaningful name to a value.

Furthermore, if you need to change a value of a constant, you can change it once, where the constant is declared, and not everywhere it appears in your code.

For this problem, there is a good opportunity to use some constants;

 

Now for the calculations

You've got the variables and constants you need. Now you need to write the expressions that do the calculating. You'll find these two operators handy:

/ and %

To divide in Java, you use the / operator. Like +, / can mean two different things depending on the type of it's operands.

If you divide an int by an int, something called integer division happens. The result is the number of whole times the bottom number goes into the top number. It does not round, it simply throws away the fractional part.

To get the part that got "thrown away", you can use the % operator, sometimes called modulus, or mod, or remainder.

Now, write the expressions to calculate how many tweny dollar bills, ten dollar bills, five dollar bills, and one dollar bills are needed to make up a given dollar amount.

Print out your results. Compile and run your code. Get that much working. Try changing the dollar amount, compile and run again and see if your results are correct.

 

Calculate Coins too

Once you have that much working, add a variable to hold a number of cents (make it an int).

Add the variables, constants, and assignment statements to calculate the number of quarters, dimes, nickels, and pennies that would be needed to make that amount. You can do this in a similar fashion to the first part. A sample run of the program with both parts working might look like this:

57 dollars and 49 cents is:
     2 twenty dollar bills
     1 ten dollar bills
     1 five dollar bills
     2 one dollar bills
     1 quarters
     2 dimes
     0 nickels
     4 pennies

 

A note about grammar

In the English language, we would not say "1 ten dollar bills". It is possible to write a program which would output "1 ten dollar bill" instead, but that would require a conditional statement, which we haven't learned yet. So don't worry about grammar on this lab, but once we cover conditionals, we'll handle this sort of thing.

 

Submit Your Assignment

Submit your program, Money.java to the Lab Submission Folder on D2L.

Your program will be graded on the proper use of variables and constants as well as correct and nicely formatted output.

When you submit code to a dropbox, we download, compile, run, and read your code. We often give you feedback, which you can see in your D2L account. Be sure to read and pay attention to this feedback. It is extremely valuable to you.

Often, labs are graded leniently because this is your first exposure to the material. But if you are missing any concepts in the lab, you can be sure you will lose points on that concept on the next quiz or test. Furthermore, because everything we learn in this class depends on a good understanding of everything we have covered previously, you can find yourself falling behind very quickly. It is your responsibility to use the feedback we give you to figure out what you are missing, and learn it as soon as possible.