Scope is the region where a name is available. A local variable declared in main belongs to that method’s block; a helper cannot read it just because both methods are in the same file. Parameters make needed values explicit. Inside a loop or branch, a newly declared local name exists only within that block.
If a helper needs a value from main, pass it as an argument instead of hoping the helper can see main’s variables. Prefer a method that receives input and returns a result. Independent calls are then easy to test and explain. Day 5 introduces fields, which store data belonging to objects.
Make a small check repeatable
A check compares what a method actually returns with an expected value. For now, we will print a label and a boolean comparison. True means this one check passed; false means the implementation or expectation needs investigation. A compiler can catch a missing return or wrong type, but it cannot decide whether your shipping threshold or arithmetic rule matches the brief.
Choose cases that distinguish plausible mistakes. If a method doubles a number, testing only zero would also pass an implementation that always returns zero. Use zero, a nonzero value and, if allowed by the contract, a negative value. Keep the expected value independent of the implementation being checked.
public class Main {
public static void main(String[] args) {
System.out.println("zero: " + (twice(0) == 0));
System.out.println("positive: " + (twice(7) == 14));
System.out.println("negative: " + (twice(-3) == -6));
}
static int twice(int value) {
return value * 2;
}
}Check your prediction
zero: true
positive: true
negative: true Changing return value*2 to return value will leave the zero check true but fail the other checks. This is why several deliberately chosen inputs give stronger evidence than repeatedly running one input.
public class Main {
public static void main(String[] args) {
System.out.println(addOne(5));
System.out.println(addOne(5));
}
static int addOne(int start) {
int result = start + 1;
return result;
}
}Check your prediction
6
6 Each call has its own start and result variables. Nothing accumulates between these calls. We will deliberately store object state later, after you can distinguish that behavior from a local calculation.
public class Main {
public static void main(String[] args) {
if (true) {
int minutes = 25;
}
System.out.println(minutes);
}
}Read the diagnostic
minutes was declared inside the if block, so the later println cannot see it. If the value is needed afterward, declare it in the surrounding block and assign it on every required path.
Your turn
Check a clamp method
Define cap(value, maximum) for nonnegative ints. Return maximum if value exceeds it; otherwise return value. Print labeled checks for below (5,10→5), equal (10,10→10), above (12,10→10) and zero (0,10→0). All four lines should end in true.
public class Main {
public static void main(String[] args) {
// Write independent comparisons for the four cases.
}
// Define cap with an int return type.
}Hint 1: the idea
Write the two possible outcomes before writing the checks. Each expectation should be a known literal for a chosen input.
Hint 2: a first step
The normal path returns the original value. A comparison branch handles only values above the maximum.
Show one solution
public class Main {
public static void main(String[] args) {
System.out.println("below: " + (cap(5, 10) == 5));
System.out.println("equal: " + (cap(10, 10) == 10));
System.out.println("above: " + (cap(12, 10) == 10));
System.out.println("zero: " + (cap(0, 10) == 0));
}
static int cap(int value, int maximum) {
if (value > maximum) {
return maximum;
}
return value;
}
}below: true
equal: true
above: true
zero: true Change it and explain
Deliberately return maximum for every input. Which checks fail? Restore the method, then deliberately change one expected value and confirm that a correct method can fail a mistaken test. Check the brief before deciding which side to repair.
Keep these checks next to your work until you understand the behavior. On Day 6, you will make failing checks stop a test program automatically, instead of requiring someone to inspect every printed true or false.
Completion is saved in this browser only. It does not measure mastery.