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 = ;
int y = x + 5;
int z = x * x;
}
}
public class Basic {
public static void main(String[] args) {
int x = ;
int y = x + 5;
int z = x * x;
}
}
public class Basic {
public static void main(String[] args) {
int x = ;
int y = x + 5;
int z = x * x;
}
}
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 = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
public class Steps {
public static void main(String[] args) {
int width = ;
int height = ;
int area = width * height;
int perimeter = 2 * (width + height);
}
}
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 = ;
double celsius = (fahrenheit - 32) * 5 / 9;
}
}
public class Temperature {
public static void main(String[] args) {
double fahrenheit = ;
double celsius = (fahrenheit - 32) * 5 / 9;
}
}
public class Temperature {
public static void main(String[] args) {
double fahrenheit = ;
double celsius = (fahrenheit - 32) * 5 / 9;
}
}
public class Temperature {
public static void main(String[] args) {
double fahrenheit = ;
double celsius = (fahrenheit - 32) * 5 / 9;
}
}
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;
}
}
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 = ;
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 = ;
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 = ;
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 = ;
double circumference = 2 * pi * radius;
double area = pi * radius * radius;
}
}
pi and radius as named values make the formulas clear.