Bake Sale

  Download Source Code


Variables

A variable is a letter (or name) that represents a value.  The number 576.50 above is the income
received from selling baked goods.  We could use the letter X to represent this number.
But the letter X isn't really the clearest choice - we could use I or INC to represent INCOME = 576.50.
Now rather than writing 576.50 over and over again, we could just write INC, or just I .
This has several advantages:

Here is a better version of the program, written using variables.

float i = 576.50;
float s = 87.50;
float c = 55.95;
float o = 92.00;

float profit = i-(s+c+o);

println("Profit = " + profit);
println("Brazil = " + 0.6 * profit);
println("Thailand = " + 0.3 * profit);
println("Parents = " + 0.1 * profit);
This might appear more complex, due to words like "float", and because it's actually longer.
But if one of the numbers changes, there is only ONE change necessary in the program.
For example, if we recount the money and it's actually 596.50, it's very easy to change here.

Good Style

Those single-letter variables are easy to type, but they are not so easy to read.
The letters "i" and "o" are especially bad, because they look too much like 1 (one) and 0 (zero).
In general:

Consistency is also helpful.  If you are going to use whole words for variables - e.g. profit -
then do that all the time. 

What does "float" mean?

"float" is a variable type.  This forces the numbers stored in the variables to be treated as decimals
rather than integers.  As a result, you can write the following and get a correct answer:

float  money = 700;
float people = 8 ;
float share = money / people;

println("Each person gets " + share + " Euros"); // ==> 87.50

Since all the variables are float types, the division will be done using decimals and give a correct answer.
It doesn't matter that there are no decimals in 700 or 8.  The word float FORCES these to be treated as decimals.


Practice

READ each program below.  PREDICT what you think it will print.  Then RUN it and check your answer.

float age = 15;
float year = 2012;
float born = year-age;

print("Born in " + born);
float x = 3;
float y = x*x-2*x+3;

println("x = " + x);
println("y = " + y);
float price = 7.50;
float tax = price * 0.16;
float total = price + tax;

println("total = " + total);
float money = 123;
float half = money / 2;
float third = money / 3;
float quarter= money / 4;

println(money);
println(half);
println(third);
println(quarter);

Each of the following programs contains errors.  Fix the errors and make each program run correctly.

price = 14.95;
count = 10;
total = price*count;

println(total);



float euros = 150;
float yen = euros / 90;

println(Euros = + euros);
println(Yen = + yen);
float hours = 8;
float minutes = hours * 60;
float seconds = minutes * 60;

println("Hours = " + hours);
println("Minutes = " + minutes);
println("Seconds = " + seconds);
float radius = 7;
float pi = 22/7;
float area = pi*r*r;

println("radius = " + radius);
println("area = " + area);