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 mistakes with operators are small: = in place of ==, a missing bracket, or int division when you wanted decimals. Each one has a quick fix, and each fix links back to the lesson that explains it.
= in place of ==
Mistake: boolean isFull = (marks = 100);. Java says incompatible types: int cannot be converted to boolean. Fix: two equal signs ask, marks == 100.
Mistake: city == "Pune" for text. It compiles but can say false for the same letters. Fix: city.equals("Pune").
Lost decimals and / by zero
Mistake: double avg = (78 + 85) / 2; gives 81.0, because int / int drops the decimals before the value is stored. Fix: divide by 2.0. On an int, marks += 2.7 drops them too.
Mistake: total / people when people is 0 stops the program with ArithmeticException: / by zero. Fix: check first, people != 0 && total / people > 100.
Review: Arithmetic operators (opens in a new tab), Assignment and compound assignment (opens in a new tab) and Logical operators (opens in a new tab).
Missing brackets
Mistake: "Total: " + 40 + 60 prints Total: 4060. Fix: "Total: " + (40 + 60). An average needs (a + b + c) / 3.
Mistake: "Passed: " + marks >= 35 does not compile. Fix: "Passed: " + (marks >= 35).
Example 1 · Common mistakes, fixed
public class Fixed { public static void main(String[] args) { int marks = 62; boolean isFull = marks == 100; System.out.println("Full marks: " + isFull); System.out.println("Passed: " + (marks >= 35)); double average = (450 + 461) / 2.0; System.out.println("Average: " + average); int age = 20; System.out.println("Adult: " + (age >= 18 && age <= 60)); }}Full marks: false Passed: true Average: 455.5 Adult: true
What this code does
==compares, and brackets keep each comparison apart from the text.- Dividing by
2.0keeps the.5; the range check uses&&.
Range checks and ++
Mistake: 18 <= age <= 60 does not compile. Fix: age >= 18 && age <= 60.
Mistake: expecting b = a++ to store the new value. It stores the old one; b = ++a stores the new one.
Example 2 · Postfix in one line
public class Tokens { public static void main(String[] args) { int token = 5; int served = token++; System.out.println("Served: " + served); System.out.println("Next token: " + token); }}Served: 5 Next token: 6
What this code does
token++gives the old value 5 toserved, thentokenbecomes 6.- This is the postfix rule: use first, then add 1.
When in doubt, put a++; on its own line.
Review: Logical operators (opens in a new tab) and Increment and decrement (opens in a new tab).
Spot the mistake
Each round below has exactly one wrong line. Tap it, then read why it is wrong and see the fix.
Find the bug
Find the wrong line
Select the line that is wrong.
This should print Full marks: true.
Example 3 · A mini shopping bill
public class MiniBill { public static void main(String[] args) { int price = 120; int qty = 3; int total = price * qty; total -= 50; boolean freeDelivery = total >= 299; String delivery = freeDelivery ? "Free delivery" : "Delivery Rs 40"; System.out.println("Total: Rs " + total); System.out.println(delivery); }}Total: Rs 310 Free delivery
What this code does
*finds 360, and the coupon-= 50brings it to 310.- A comparison gives
freeDelivery, and?:picks the message.
Finish up
Mark complete & continue
Next: Printing with println and print