Foundations
Variables
Naming Values
You're tracking your monthly budget. Instead of typing 2500.00 every time you
calculate savings, rent, and groceries, you store it in a variable called income.
Now when your income changes, you update one number and all calculations adjust.
Store and use a value
Give a value a name, then use that name in calculations.
public class Basic {
public static void main(String[] args) {
int x = 10;
int y = x + 5;
int z = x * x;
}
}
public class Basic {
public static void main(String[] args) {
int x = 5;
int y = x + 5;
int z = x * x;
}
}
public class Basic {
public static void main(String[] args) {
int x = 20;
int y = x + 5;
int z = x * x;
}
}
x ← 10, y ← 15, z ← 100
1public class Basic {2 public static void main(String[] args) {3 int x→ 10 = 10; //@x=5, 204 int y→ 15 = x10 + 5;5 int z→ 100 = x10 * x;6 }
x ← 5, y ← 10, z ← 25
1public class Basic {2 public static void main(String[] args) {3 int x→ 5 = 5;4 int y→ 10 = x5 + 5;5 int z→ 25 = x5 * x;6 }
x ← 20, y ← 25, z ← 400
1public class Basic {2 public static void main(String[] args) {3 int x→ 20 = 20;4 int y→ 25 = x20 + 5;5 int z→ 400 = x20 * x;6 }
A variable holds a value. Use it anywhere you'd use the value itself.
Building expressions step by step
Break a complex calculation into named pieces. Each piece has meaning.
public class Steps {
public static void main(String[] args) {
int width = 7;
int height = 5;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = 3;
int height = 5;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = 10;
int height = 5;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = 7;
int height = 4;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = 7;
int height = 8;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
width ← 7, height ← 5, area ← 35, perimeter ← 24
1public class Steps {2 public static void main(String[] args) {3 int width→ 7 = 7; //@width=3, 104 int height→ 5 = 5; //@height=4, 85 int area→ 35 = width7 * height5;6 int perimeter→ 24 = 2 * (width7 + height5);7 }
width ← 3, height ← 5, area ← 15, perimeter ← 16
1public class Steps {2 public static void main(String[] args) {3 int width→ 3 = 3;4 int height→ 5 = 5;5 int area→ 15 = width3 * height5;6 int perimeter→ 16 = 2 * (width3 + height5);7 }
width ← 10, height ← 5, area ← 50, perimeter ← 30
1public class Steps {2 public static void main(String[] args) {3 int width→ 10 = 10;4 int height→ 5 = 5;5 int area→ 50 = width10 * height5;6 int perimeter→ 30 = 2 * (width10 + height5);7 }
width ← 7, height ← 4, area ← 28, perimeter ← 22
1public class Steps {2 public static void main(String[] args) {3 int width→ 7 = 7;4 int height→ 4 = 4;5 int area→ 28 = width7 * height4;6 int perimeter→ 22 = 2 * (width7 + height4);7 }
width ← 7, height ← 8, area ← 56, perimeter ← 30
1public class Steps {2 public static void main(String[] args) {3 int width→ 7 = 7;4 int height→ 8 = 8;5 int area→ 56 = width7 * height8;6 int perimeter→ 30 = 2 * (width7 + height8);7 }
Reading area tells you more than reading 7 * 5. Variables make code self-documenting.
Temperature conversion (Fahrenheit to Celsius)
Convert 98.6°F (body temperature) to Celsius using the formula: C = (F - 32) × 5 / 9
public class Temperature {
public static void main(String[] args) {
double fahrenheit = 98.6;
double celsius = (fahrenheit - 32) * 5 / 9;
}
}
public class Temperature {
public static void main(String[] args) {
double fahrenheit = 32;
double celsius = (fahrenheit - 32) * 5 / 9;
}
}
public class Temperature {
public static void main(String[] args) {
double fahrenheit = 212;
double celsius = (fahrenheit - 32) * 5 / 9;
}
}
public class Temperature {
public static void main(String[] args) {
double fahrenheit = 0;
double celsius = (fahrenheit - 32) * 5 / 9;
}
}
fahrenheit ← 98.6, celsius ← 37.0
1public class Temperature {2 public static void main(String[] args) {3 double fahrenheit→ 98.6 = 98.6; //@fahrenheit=32, 212, 04 double celsius→ 37.0 = (fahrenheit98.6 - 32) * 5 / 9;5 }
fahrenheit ← 32.0, celsius ← 0.0
1public class Temperature {2 public static void main(String[] args) {3 double fahrenheit→ 32.0 = 32;4 double celsius→ 0.0 = (fahrenheit32.0 - 32) * 5 / 9;5 }
fahrenheit ← 212.0, celsius ← 100.0
1public class Temperature {2 public static void main(String[] args) {3 double fahrenheit→ 212.0 = 212;4 double celsius→ 100.0 = (fahrenheit212.0 - 32) * 5 / 9;5 }
fahrenheit ← 0.0, celsius ← -17.77777777777778
1public class Temperature {2 public static void main(String[] args) {3 double fahrenheit→ 0.0 = 0;4 double celsius→ -17.77777777777778 = (fahrenheit0.0 - 32) * 5 / 9;5 }
Variables make the formula readable: fahrenheit, celsius - you know what they mean.
Fibonacci sequence (without loops)
Each Fibonacci number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8...
public class Fibonacci {
public static void main(String[] args) {
int c0 = 0;
int c1 = 1;
int c2 = c0 + c1;
int c3 = c1 + c2;
int c4 = c2 + c3;
int c5 = c3 + c4;
int c6 = c4 + c5;
int c7 = c5 + c6;
}
}
c0 ← 0, c1 ← 1, c2 ← 1, c3 ← 2, c4 ← 3, c5 ← 5, c6 ← 8, c7 ← 13
1public class Fibonacci {2 public static void main(String[] args) {3 int c0→ 0 = 0;4 int c1→ 1 = 1;5 int c2→ 1 = c00 + c11;6 int c3→ 2 = c11 + c21;7 int c4→ 3 = c21 + c32;8 int c5→ 5 = c32 + c43;9 int c6→ 8 = c43 + c55;10 int c7→ 13 = c55 + c68;11 }
Watch how each variable depends on the previous ones. This pattern is called building up from base cases.
Circle calculations
Given a radius, calculate circumference and area.
public class Circle {
public static void main(String[] args) {
double pi = 3.14159;
double radius = 5;
double circumference = 2 * pi * radius;
double area = pi * radius * radius;
}
}
public class Circle {
public static void main(String[] args) {
double pi = 3.14159;
double radius = 1;
double circumference = 2 * pi * radius;
double area = pi * radius * radius;
}
}
public class Circle {
public static void main(String[] args) {
double pi = 3.14159;
double radius = 10;
double circumference = 2 * pi * radius;
double area = pi * radius * radius;
}
}
public class Circle {
public static void main(String[] args) {
double pi = 3.14159;
double radius = 2.5;
double circumference = 2 * pi * radius;
double area = pi * radius * radius;
}
}
pi ← 3.14159, radius ← 5.0, circumference ← 31.4159, area ← 78.53975
1public class Circle {2 public static void main(String[] args) {3 double pi→ 3.14159 = 3.14159;4 double radius→ 5.0 = 5; //@radius=1, 10, 2.55 6 double circumference→ 31.4159 = 2 * pi3.14159 * radius5.0;7 double area→ 78.53975 = pi3.14159 * radius5.0 * radius;8 }
pi ← 3.14159, radius ← 1.0, circumference ← 6.28318, area ← 3.14159
1public class Circle {2 public static void main(String[] args) {3 double pi→ 3.14159 = 3.14159;4 double radius→ 1.0 = 1;5 6 double circumference→ 6.28318 = 2 * pi3.14159 * radius1.0;7 double area→ 3.14159 = pi3.14159 * radius1.0 * radius;8 }
pi ← 3.14159, radius ← 10.0, circumference ← 62.8318, area ← 314.159
1public class Circle {2 public static void main(String[] args) {3 double pi→ 3.14159 = 3.14159;4 double radius→ 10.0 = 10;5 6 double circumference→ 62.8318 = 2 * pi3.14159 * radius10.0;7 double area→ 314.159 = pi3.14159 * radius10.0 * radius;8 }
pi ← 3.14159, radius ← 2.5, circumference ← 15.70795, area ← 19.6349375
1public class Circle {2 public static void main(String[] args) {3 double pi→ 3.14159 = 3.14159;4 double radius→ 2.5 = 2.5;5 6 double circumference→ 15.70795 = 2 * pi3.14159 * radius2.5;7 double area→ 19.6349375 = pi3.14159 * radius2.5 * radius;8 }
pi and radius as named values make the formulas clear.