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.

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

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

System.out.println("Profit = " + profit);
System.out.println("Brazil = " + 0.6 * profit);
System.out.println("Thailand = " + 0.3 * profit);
System.out.println("Parents = " + 0.1 * profit);
This might appear more complex, due to words like "double", 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 "double" mean?

"double" 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:

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

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

Since all the variables are double 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.

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

System.out.print("Born in " + born);
double x = 3;
double y = x*x-2*x+3;

System.out.println("x = " + x);
System.out.println("y = " + y);
double price = 7.50;
double tax = price * 0.16;
double total = price + tax;

System.out.println("total = " + total);
double money = 123;
double half = money / 2;
double third = money / 3;
double quarter= money / 4;

System.out.println(money);
System.out.println(half);
System.out.println(third);
System.out.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;

System.out.println(total);
double euros = 150;
double yen = euros / 90;

System.out.println(Euros = + euros);
System.out.println(Yen = + yen);
double hours = 8;
double minutes = hours * 60;
double seconds = minutes * 60;

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

System.out.println("radius = " + radius);
System.out.println("area = " + area);