Common mistakes and practice
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Most method bugs are one of a few kinds: an answer that never comes back, an argument that does not fit, or a method expected to change your variable. Spot them quickly, then put the whole module to work.
Mistakes and fixes at a glance
The hardest method bugs compile and run, but print the wrong answer. The most common is an ignored return value: the method works out the answer, and nobody keeps it.
Example 1 · An ignored return value
public class IgnoredReturn { public static void main(String[] args) { square(5); System.out.println("Nothing printed above this line"); int s = square(5); System.out.println("Square: " + s); }
static int square(int n) { return n * n; }}Nothing printed above this line Square: 25
What this code does
square(5);on its own line returns 25, but nobody stores or prints it, so the answer is lost.int s = square(5);keeps the answer, and the next line can print it.
Example 2 · Integer division inside a method
public class AverageBug { public static void main(String[] args) { System.out.println("Wrong: " + avgWrong(70, 75, 81)); System.out.println("Right: " + avgRight(70, 75, 81)); }
static double avgWrong(int a, int b, int c) { return (a + b + c) / 3; }
static double avgRight(int a, int b, int c) { return (a + b + c) / 3.0; }}Wrong: 75.0 Right: 75.33333333333333
What this code does
226 / 3divides twointvalues, so it gives 75; only then does thedoublereturn type turn it into 75.0.- The return type does not change how the division inside works. Dividing by
3.0keeps the decimals, as in Division: int or double? (opens in a new tab).
Review: Return a value from a method (opens in a new tab). The other common mistakes, each with what goes wrong and the fix:
missing return statement→ one path has noreturn→ return on every path, as in the same lesson.cannot be applied to given types→ wrong number or types of arguments → match the header. Review: Parameters and arguments (opens in a new tab)cannot find symbolfor a variable → it belongs to another method → pass it as an argument. Review: Local variables and scope (opens in a new tab)- A variable did not change after the call → the method got a copy →
x = change(x);. Review: Pass-by-value (opens in a new tab) already defined→ two versions differ only in return type → change the parameters. Review: Method overloading (opens in a new tab)
Spot the bug
Each method below looks almost right but does the wrong thing. Read what it should do, tap the wrong line, then check the fix.
Before you tap, ask three questions: does every call match the header, does every path return, and is every returned value kept?
Find the bug
Find the method bug
Select the line that is wrong.
This should print 590, the price with 18% GST.
Round 1 of 6Print or return?
addLoud prints its answer and also returns it, so the answer shows twice. A method that returns and leaves the printing to the caller can be used anywhere: printed, added to GST or compared.
Example 3 · Printing and returning: the answer shows twice
public class PrintAndReturn { public static void main(String[] args) { System.out.println(addLoud(2, 3)); System.out.println(add(2, 3)); }
static int addLoud(int a, int b) { System.out.println(a + b); return a + b; }
static int add(int a, int b) { return a + b; }}5 5 5
What this code does
addLoudprints 5 itself, thenmainprints the returned 5 again: two lines for one answer.addonly returns, somaindecides to print it, and 5 appears once.
A good habit for every program: methods work things out and return them; main reads the input and prints the output.
When in doubt, trace it
A program with several methods prints a wrong number, and reading it again does not help. Trace it: follow each call into its method, write down every variable, and note what comes back.
Trace the code
Trace two calls to twice()
Variables
- a
- 3
Output
(nothing printed yet)Line 3a starts at 3.
Finish up
Mark complete & continue
Next: Must-do problems: Methods