Tip:
Highlight text to annotate it
X
I’m going to go over the program for calculating running totals
What should happen when you run the program, is that it will ask you
how many rooms you would like painted. I’m going to put in two, and I’m going to
give it a height of ten, width of ten,
length of ten, then it is going to ask me for the height of the second room:
height of ten, width of ten, and length of ten
I know for two rooms the total square footage should be 800, so this is correct.
I am going to show you how I have set this up.
I’ve started by creating a bunch of variables. I have a double,
for the total number of rooms. The reason I am using doubles, instead of an integer
is because when I multiply later in the program, alice won't
let me combine doing calculations with integers and
doubles. In most programs you can do that as long as you are multiplying by
an integer, but you are storing it in a double. Alice does not allow this
because it can cause errors. So we are going to start with
total rooms, and we will ask the user for that value. Then we are going to create
double, current room, which we will set to 0. A double for the room width.
A double for the room height, a double for the room length.
A double for the room square footage. We will calculate the room square footage at the end of each
loop. Double PRICE, that is in all caps because that is a constant
variable, it will not change while the program is running.
I’m setting it to 25 cents per square foot.
I have my total square feet, which will be each room
square footage added together, and then my total price which will be my total square footage times PRICE
My while statement is the most important
part of the program, it is going to keep running as long as the
current room is less than the total rooms. This will work as long as
I put in a number that is greater than 0. For example, if I put in 1
the first time it will run, because initially we start with 0.
Here is where we change the counter. We are changing current room to equal current room plus 1.
That way we can use that value for current room when we are asking
our user for the values. We will get the height
from the user and tell them that it is for room one, or whatever current room it is.
we will get the width, and we will get the length. Then we will
calculate our square footage, which is the room length times the room height
then the answer to that is multiplied by 2. (notice the parenthesis.)
Then we will calculate the room width by the room height, also multiplied by 2.
and they are added together. That gives us the square footage
for all four walls in a room.
our total square footage will equal itself plus
the room square footage that we just calculated.
At the end, we will calculate the total price by taking that total
square footage and multiplying it by price, and then we will give that back
to the user. So again, we tell it how many times
the room loop is going to run.
So we are going to do 3, the room height will be ten
we will say that it is an 8
by ten foot room, the height of the second room will also be 10
and we are going to make it a 12 by 12 foot room.
The height of the third room is ten feet, and the
room is a ten by 14 foot room.
that gives you your total square footage, and your total price. Again, with the looping
program, it will keep going through the loop, until the condition is met.