Skip to lesson

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

IgnoredReturn.javaJava
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;    }}
Output
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

AverageBug.javaJava
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;    }}
Output
Wrong: 75.0
Right: 75.33333333333333

What this code does

  • 226 / 3 divides two int values, so it gives 75; only then does the double return type turn it into 75.0.
  • The return type does not change how the division inside works. Dividing by 3.0 keeps 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:

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 6

Print 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

PrintAndReturn.javaJava
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;    }}
Output
5
5
5

What this code does

  • addLoud prints 5 itself, then main prints the returned 5 again: two lines for one answer.
  • add only returns, so main decides 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()

public class TraceTwice {
public static void main(String[] args) {
int a = 3;
int b = twice(a) + 1;
System.out.println(a + " " + b);
System.out.println(twice(b));
}
static int twice(int n) {
n = n * 2;
return n;
}
}

Variables

a
3

Output

(nothing printed yet)

Line 3a starts at 3.

Step 1 of 11

Finish up

Mark complete & continue

Next: Must-do problems: Methods