Calculating with Numbers
Basic mathematical operations
Java uses +, -, *, and / with the usual precedence: * and / before + and -.
Simple calculations
int first = 2;
int second = 4;
int sum = first + second; // Addition: 6
int difference = second - first; // Subtraction: 2
int product = first * second; // Multiplication: 8
int quotient = second / first; // Division: 2
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);Precedence and parentheses
Parentheses run first. Without them, multiplication and division happen before addition and subtraction.
Order of operations
int withParentheses = (1 + 1) + 3 * (2 + 5);
System.out.println(withParentheses); // prints 23
int withoutParentheses = 1 + 1 + 3 * 2 + 5;
System.out.println(withoutParentheses); // prints 13
// Step by step:
// (1 + 1) = 2, (2 + 5) = 7, 3 * 7 = 21, 2 + 21 = 23
// vs
// 3 * 2 = 6, then 1 + 1 + 6 + 5 = 13Key ideas
- Expression: Values and operators that produce a single value.
- Statement: A full instruction, usually ending with
;. - Precedence:
*and/bind tighter than+and-. - Parentheses: Force a different evaluation order.
Calculating and printing
int robotSpeed = 25;
int time = 4;
int distance = robotSpeed * time;
// Combine text and calculations
System.out.println("Robot speed: " + robotSpeed + " mph");
System.out.println("Distance traveled: " + distance + " miles");
System.out.println("Average: " + (robotSpeed + time) / 2);
// String concatenation precedence
System.out.println("Result: " + (2 + 3)); // prints "Result: 5"
System.out.println("Wrong: " + 2 + 3); // prints "Wrong: 23"With +, Java evaluates left to right. Once one operand is a String, the rest are converted to strings and appended. Parentheses around (2 + 3) do the math first.
Division and data types
If both operands are integers, / truncates toward zero. If either operand is floating-point, you get a decimal result.
Integer vs floating-point division
// Integer division - truncates decimal part
int intResult = 7 / 2;
System.out.println(intResult); // prints 3
// Floating-point division
double floatResult1 = 7.0 / 2; // 3.5
double floatResult2 = 7 / 2.0; // 3.5
double floatResult3 = 7.0 / 2.0; // 3.5
// Type casting for precise division
int a = 7;
int b = 2;
double precise = (double) a / b; // 3.5
double wrong = (double) (a / b); // 3.0 (division done first!)Division habits
Good Practices:
- Use
doublefor calculations requiring decimal precision - Cast to
(double)before division for exact results - Be aware of integer division truncation
- Use parentheses to control evaluation order
Avoid:
- Expecting decimals from integer division
- Casting after division:
(double)(a/b) - Dividing by zero (causes runtime error)
- Forgetting precedence in mixed operations
Practical robot calculations
public class RobotCalculations {
public static void main(String[] args) {
// Robot navigation calculations
double startX = 10.5;
double startY = 15.0;
double endX = 25.3;
double endY = 30.7;
// Distance formula: sqrt((x2-x1)² + (y2-y1)²)
double deltaX = endX - startX;
double deltaY = endY - startY;
double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
System.out.println("Robot must travel: " + distance + " units");
// Battery calculation
double batteryStart = 100.0;
double powerConsumption = 2.5; // percent per minute
int timeMinutes = 25;
double batteryUsed = powerConsumption * timeMinutes;
double batteryRemaining = batteryStart - batteryUsed;
System.out.println("Battery remaining: " + batteryRemaining + "%");
}
}Assignment copies a value
a = bcopies the value frombintoa;bis unchanged.- Later changes to
bdo not changea. - Assignment runs once when that line executes.
Variable assignment example
int robotCount = 5;
int backupCount = robotCount; // Copy 5 to backupCount
System.out.println("Original: " + robotCount); // 5
System.out.println("Backup: " + backupCount); // 5
// Change original
robotCount = 10;
System.out.println("After change:");
System.out.println("Original: " + robotCount); // 10
System.out.println("Backup: " + backupCount); // Still 5!
// backupCount did NOT automatically changeTry It Yourself:
Exercise 1: Calculation Practice
- Calculate the area of a rectangular robot base (length × width)
- Compute average speed from distance and time with proper division
- Create a battery percentage calculator with remaining time
- Demonstrate the difference between integer and floating-point division
Practice with mathematical operations and calculations: