Chapter 1 · Foundations
Variables and arithmetic
Step 1 of 22 · Concept
A variable is a named box
Lesson 2 showed main's frame with boxes in it. A variable is one of those boxes, with a name you choose.
int age = 15;
| part | means |
|---|---|
int | the type: what the box can hold |
age | the name of the box |
= 15 | the value put in the box |
In this lesson you will fill boxes, change them and calculate with them.
Step 2 of 22 · Concept
Four types of box
| type | holds | example |
|---|---|---|
int | whole numbers | int age = 15; |
double | numbers with a fraction | double price = 2.5; |
boolean | true or false | boolean adult = false; |
String | text, in double quotes | String name = "Ada"; |
A String's box holds a reference to the text on the heap, drawn as an arrow (lesson 2).
Step 3 of 22 · Quiz
Which type fits?
A box must hold the price 2.5. Which line works?
Step 4 of 22 · Concept
Java does arithmetic
Java calculates with these five signs:
| write | means | 17 ? 5 gives |
|---|---|---|
a + b | add | 22 |
a - b | subtract | 12 |
a * b | multiply | 85 |
a / b | divide | 3 |
a % b | the remainder of a / b | 2 |
17 / 5 is 3, not 3.4: two ints give an int. With a double on either side, the answer is a double:
3 * 2.5 is 7.5. The step Whole numbers divide without the fraction comes back to this.
Step 5 of 22 · Concept
+ joins text
- With two numbers,
+adds. - With text on either side,
+joins: the other side becomes text, and the two are stuck together.
Step 6 of 22 · Quiz
What will it print?
Type exactly what these lines print.
int width = 7;
int height = 6;
int area = width * height;
System.out.println("Area: " + area);
Step 7 of 22 · Demo
See it run
Run it and compare with your answer.
Step 8 of 22 · Try it
Try it: add first
Run it: it prints Fruit: 23, not Fruit: 5. Change the last line so that it prints:
Fruit: 5
Brackets go first, as in maths: (apples + pears) adds the two numbers before any text joins them.
Step 9 of 22 · Concept
= stores a value
In Java, = does not mean "equals". It assigns: it stores a value in a box.
age = age + 1;
If age holds 17:
- Java works out the right side first, with the value in the box now:
17 + 1is18. - Then it puts
18in the boxage. The old17is gone.
Adding to a box is so common that it has a short form: age += 10; means age = age + 10;. With text, +=
joins: name += "!"; stores name + "!" in name.
Step 10 of 22 · Concept
A comparison gives true or false
age >= 18 asks a question: is age 18 or more? The answer is true or false, a boolean. A boolean box
can keep it:
boolean adult = age >= 18;
| question | means | 18 ? 18 gives |
|---|---|---|
a >= b | more than or equal | true |
a > b | more than | false |
Step 11 of 22 · Trace
Watch the boxes
Press Next to run one line at a time. Watch the boxes age and adult.
int age = 17;
boolean adult = age >= 18;
age = age + 1;
System.out.println(adult);
adult = age >= 18;
System.out.println(adult);- Lines 1 and 2 ran:
ageholds17.17 >= 18isfalse, soadultholdsfalse. 17 + 1is18, and18replaced the17inage. Nothing else changed.- Line 4 printed
false.adultstill holdsfalse: a value, not the question that made it. - Line 5 asks again:
18 >= 18istrue. Nowadultchanges. - The second
printlnprintstrue.
Step 12 of 22 · Quiz
Which box changes?
What does total hold after these three lines?
int price = 4;
int total = price * 3;
price = 5;
Step 13 of 22 · Demo
The whole program
The boxes from the trace, and += at the end. Run it.
Step 14 of 22 · Exercise
Your turn: a receipt
Write a receipt for pencils.
- Declare
item("pencil"),count(3) andprice(2.5). - Print the total,
count * price, and whether delivery is free: a total of10or more. - Add
2tocountwith+=, then print both lines again.
The output must be exactly:
3 x pencil = 7.5
Free delivery: false
5 x pencil = 12.5
Free delivery: true
Pick a type for each box: text is a String, 3 an int, 2.5 a double. count * price is a double too.
Keep the answers in boxes: double total = count * price; and boolean freeDelivery = total >= 10;.
After count += 2;, work total and freeDelivery out again. They do not update by themselves.
count += 2;
total = count * price;
freeDelivery = total >= 10;
Step 15 of 22 · Concept
An int takes 4 bytes, a double 8
Each box in a frame takes some of memory's numbered bytes (lesson 2):
| type | bytes | holds |
|---|---|---|
int | 4 | whole numbers, up to about 2 billion either way |
double | 8 | numbers with a fraction, exact to about 16 digits |
A double keeps its digits and where the point goes, so it needs more room.
Step 16 of 22 · Concept
Whole numbers divide without the fraction
- When both sides of
/areint, the answer is anint: the fraction is dropped.7 / 2is3. - When either side is a
double, the answer is adoubleand keeps its fraction.7 / 2.0is3.5. 7 % 2is1: what is left over after7 / 2.
Step 17 of 22 · Quiz
Predict the division
Type exactly what these three lines print.
System.out.println(7 / 2);
System.out.println(7 / 2.0);
System.out.println(7 % 2);
Step 18 of 22 · Demo
See it run
Run it and compare with your answer.
Step 19 of 22 · Exercise
Your turn: the average
This program should print the average of three test scores, Average: 6.666666666666667. It prints
Average: 6.0. Fix the division.
a + b + c is 20, an int, and 3 is an int too. So the division drops the fraction.
Make one side of / a double: adding .0 to a number is enough.
Step 20 of 22 · Check yourself
Check yourself: a game score
Ada plays a game of 4 levels. Her points start at 0, and she scores 40, then 35 more.
She wins with 70 points or more.
Keep her name, her points, the number of levels and whether she won in variables. Print exactly:
Ada: 40 points
Ada: 75 points
Per level: 18.75
Winner: true
Follow the comments in order. The per-level line divides, so think about which box needs a fraction.
Step 21 of 22 · Challenge · optional
Challenge: swap two boxes
Optional. It earns the lesson's third star.
Swap the values of a and b, without typing 3 or 8 again. The program must print:
a = 8, b = 3
Try a = b; then b = a; and run it. Both are 8: once a holds 8, its 3 is gone.
Save the 3 in a third box before you change a: int keep = a;. Then b can take it from keep.
Step 22 of 22 · Recap
Recap
- A variable is a named box in the frame. Its type says what it holds.
=stores a value, not a formula. Work it out again when its inputs change.- A comparison such as
age >= 18givestrueorfalse. +adds numbers and joins text.%gives the remainder.- Two
ints divide without the fraction. Adoubleon either side keeps it.
Next: lesson 4 reads what javac says when a program is wrong.
Review
4 questions from this lesson, in a new order each time. Answer from memory: 3 of 4 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
public class Area {
public static void main(String[] args) {
int width = 7;
int height = 6;
int area = width * height;
System.out.println("Area: " + area);
}
}
public class Fruit {
public static void main(String[] args) {
int apples = 2;
int pears = 3;
System.out.println("Fruit: " + apples + pears);
}
}
public class Birthday {
public static void main(String[] args) {
int age = 17;
boolean adult = age >= 18;
System.out.println("Age " + age + ", adult: " + adult);
age = age + 1;
adult = age >= 18;
System.out.println("Age " + age + ", adult: " + adult);
age += 10;
System.out.println("Ten years later: " + age);
}
}
public class Receipt {
public static void main(String[] args) {
// declare the item, the count and the price
// print the total and whether delivery is free
// add 2 to the count, then print both lines again
}
}
public class Divide {
public static void main(String[] args) {
System.out.println(7 / 2);
System.out.println(7 / 2.0);
System.out.println(7 % 2);
}
}
public class Average {
public static void main(String[] args) {
int a = 4;
int b = 7;
int c = 9;
double average = (a + b + c) / 3;
System.out.println("Average: " + average);
}
}
public class Game {
public static void main(String[] args) {
// her name, her points (0 to start), the levels
// she scores 40 points: print the first line
// she scores 35 more: print the second line
// the points per level, and whether she won
}
}
public class Swap {
public static void main(String[] args) {
int a = 3;
int b = 8;
// swap the values of a and b
System.out.println("a = " + a + ", b = " + b);
}
}