BCTC - CIT-149 - Java I - Piggy Bank
So, a friend of mine is going about learning programming at a local college. This is something I generally support and therefore something that I'd be happy to help with in any fashion. I also asked if I could be given the coursework / homework, so that I could see the progression that it goes and do the work myself; so that I may be better able to understand where the course is and what is expected from the work.
So, the first piece of work that I was given is called Piggy Bank. As expected from the first piece of homework that’s actually to do with programming, it’s a simple task that can certainly seem daunting at first.
So, the task is as follows;
Penny has been saving her birthday money for a “long time.” She opens her piggy bank to count her money. She has $5 bills, $1 bills, quarters, dimes,nickels, and pennies. You decide to write a program to help Penny count her money (and to practice your programming skills). Your program will ask Penny to enter how many $5 bills, $1 bills, quarters, dimes, nickels, and pennies she has and then it will calculate and display the total.
1. Use floating-point constants 5.00, 1.00, .25, .10, .05, and .01.
2. Use integer variables for the number of $5 bills, the number of $1 bills, the number of quarters, the number of dimes, the number of nickels, and the number of pennies that Penny has.
3. Use a floating-point variable for Penny’s total.
4. Format your input nicely so that it is VERY easy for Penny to enter her counts (one at a time). Do not have Penny enter all at once with commas between.
5. Format your output nicely so that Penny can easily read how much she has. Use something similar to:
Penny, you have a total of 15.32
6. Document your program well. Place a comment block at the beginning of your code that includes: Program name, Author, Date, Purpose of program
In my opinion, syntax is not something important to remember, instead it is important to understand concepts, logic and how to properly break down a task into smaller tasks in order to properly write out and structure the code into something meaningful.
The task set forth is about a number of different topics;
Now, I am actually acutely aware that I don’t actually know anything about what is in Chapter 1 of the textbook that they’re studying from. So, I might go into a bit more detail than they should already know (Output formatting, for example).
The first thing to do in any given task is to break down the problem into individual steps; into specific “units” of work that can be implemented individually. A task as a whole can be daunting, but understanding and focusing on smaller bits of code will make things easier and allow the mind to comprehend what work that is to do on that task.
A computer program / application is, at its core, a series of statements. Computers are, effectively, exceedingly stupid; they do exactly what they’re told line by line. Especially in beginner programs, the flow of an application is very simple and this allows for us to break things down step by step.
So, we need to break down this task into steps:
Define 6 Constants to represent Dollar Value of denominations
Create a Scanner to read from System.in
Create a variable to track the running total
For each of the denominations;
read numeric input representing how many of the current denomination Peggy has
multiply the input with the denomination dollar value
add that value to a running total
Display the running total to the screen
In theory, we have five steps for our program.
1. Define 6 Constants to represent Dollar Value of denominations
A constant in Java is declared like a variable is, but on the main class and is a final static. This means that when you have declared the constant and assigned its value, it will always have that value.
private final static float FIVE_DOLLAR_BILL = 5.00;
This type of constant is what can be referred to as a Named Constant. A Named Constant is a constant that represents a set value in a program; so instead of using a literal value (5.00) in potentially multiple places in the program, a single constant is used which can easily be changed.
For example, we may use FIVE_DOLLAR_BILL multiple times through a program. At sometime in the future, for some strange reason, the value of a FIVE_DOLLAR_BILL might change from 5.00 to 5.05. By just using the Named Constant, we only have to change this once instead of any number of times where we’ve hardcoded the 5.00 value.
This part also brings us to the used of the technical term “Floating Point Types”. A Floating Point Type is a numeric value that can have decimal points (1.00, 1.01, 2.034322002, 3.00), which are opposed to an Integer Type, which represents a number that cannot have a decimal point (1, 2, 3, 4...). An Integer Type is a variable of type int or long, while to represent a Floating Point Type we use a float or double.
Currency is generally represented (at this stage) as a float or a long, as to capture the pennies. That is why we represent our dollar values as a float.
2. Create a Scanner to read from System.in
The Scanner is the way that we work with an InputStream. InputStream and OutputStream are two classes that are more complicated than this point requires, however at the base they are very simple:
An InputStream is a way of reading from a data source
An OutputStream is a way of writing to a data source
For all we care right now, our InputStream is System.in. System.in is an InputStream that we can use to wait for an input from the user on the command line.
Our OutputStream is called System.out. System.out writes to the command line.
Scanner scnr = new Scanner(System.in);
The Scanner class has a bunch of different methods to it, but mostly the one that’s important to know right now is scnr.nextInt(); This will wait for the user to input an integer and will return that input.
The Scanner that we have created will be our main use for reading any and all input.
3. Create a variable to track the running total
Variables are very important in all programs. Variables are how we track all manner of things, so in order to track the running total, we need ourselves a variable.
We know what the variable is for; so we have to determine what type it is (is it an int or is it a float?). Since we know we’re going to be tracking a monetary value which will definitely have decimal points, we can safely assume that we’ll be using a float. Especially as the spec says to use a Floating-Point Type, which as we figured out before is a float or a double.
It is very important to give variables descriptive (yet pithy) names in order to allow others to understand what exactly those variables are, as it is not always clear. While we could name our variable “foo”, “bar”, o”x”, “genericNumberOfChickenese” or something of that like, it would probably be better named with something simple, like “total”.
4. For Each Denomination...
At this point is the biggest part of the program. We have six different known denominations of money, for each one, we need to know how many Peggy has and use that knowledge to calculate the “value” of all that money, and add that to our running total.
Saying it in a run-on sentence like that makes it sound complicated. Breaking it down helps;
Read numeric input using our scanner. This is our quantity of that denomination.
Multiply the quantity by the value to get the denomination dollar value
Add the denomination dollar value to the running total
Essentially, we want to do the same thing six times, once for each denomination. This means that we’re going to be doing some repeated code, so we only really need to figure out just what we’re doing once.
Our first step (Display a Prompt) is about using System.out in order print to the console a nice and neat prompt that asks Peggy for input into the system. The main way we do this is through System.out.println(...) and System.out.print(...). The main difference between the two is that println will append a new line character to the end of the output, while print will not, allowing for further output or input to be on the same line.
The next step (Read numeric input using our scanner) utilises the scnr variable previously made in order to read in an integer. This integer represents the quantity of the current denomination that Peggy has. So, for example, if she had 4 five dollar notes, then she would input 4 and the scnr.nextInt() call would return the number (int) 4.
Now we have the quantity, we can do the next step (Multiply the quantity by the value) in order to order to determine the denomination dollar value, so we can continue onto the next step. This is a simple multiplication that we do between an Integer (quantity) and a Floating-Point Type (the Named Constant that represents the denomination). The most important thing to remember that whenever we multiply anything by a Floating-Point Type, we always get a Floating-Point Type.
So, a float multiplied by an int would always return a float.
The final step (Add the denomination dollar value to the running total) is a simple addition; our denomination dollar value is a float (as it is the product (multiplication) of an int (quantity) and a float (Named Constant that represents the denomination)).
As an example of this process:
Print the prompt “Enter how many five dollars you have: “
Read the next integer input into an int variable (quantity). This is input as 4.
Multiply quantity by FIVE_DOLLAR_VALUE (a float value of 5.00). This total is 20.00 and stored in a float variable called dollarValue.
Add dollarValue to the total value.
This process is then repeated for each other other denominations. At the end, total will contain the total of all denomination values.
5. Display the running total to the screen
I am not entirely sure if the textbook that is used goes over how to use the System.out method “printf”, which is a very useful tool for formatting strings and printing them out to the console.
Using printf will allow us to fix some problems with multiplying Floating-Point Type numbers. This can be seen by using String concatenation in order to output the total.
System.out.println(”Penny, you have saved $” + total);
This will do something that will look strange; it’ll output the total as having a strange decimal point value. For example, passing in 1 to all of the denominations will output the following:
Penny, you have a saved $6.4100003
This is because multiplying Floating-Point Types is actually a bit strange. So, instead, we can use the System.out.printf in order to format our string a bit better and also to limit our float.
System.out.printf allows us to format (the f stands for “format”) strings with variables. It’s very useful and interesting, but I suspect that it might be a bit out of the scope of this article.
System.out.printf("Penny, you have saved $%.2f", total);
The most interesting part of this string, of course, is “%.2f”. Running this program and seeing the output should get you the following;
Penny, you have saved $6.41
printf has number of Format Specifiers, though that might not actually what they’re called. These Specifiers start with % and end with a letter to declare what type of variable. Anything between the two is a flag. The terminology is not important.
“f” means that the variable is a float. “.2″ means that the float should be formatted to only two decimal places.
“%.2f” essentially means to replace the string with the first variable given (total) as a float to two decimal points.
MagicAnyway, that’s the break down of the program. Hopefully, this should be helpful and breaks things down to an understandable level.
Finally, here is my version of the program. Just for a reference.