CodeBlanks Logo Icon CodeBlanks

Summary - Operators

Combine arithmetic, comparison and logical operators to write a grade calculator.

Summary - Operators

In this group you learned Java operators:

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, <, >, <=, >=
  • Logical: &&, ||, !
  • Bitwise: &, |, ^, ~, <<, >>
  • Assignment/increment: =, +=, ++, --

Now combine them and write a grade calculator.

Practice Compute a grade

Write a Main class with a main method. Assign 60 to int midterm and 80 to int finalGrade. Assign (midterm + finalGrade) / 2 to int average. Print average with println.

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        int midterm = 60;
        int finalGrade = 80;
        int average = (midterm + finalGrade) / 2;
        System.out.println(average);
    }
}

Expected output

70

Tips

A few tips from this group:

  • Integer division gives an integer; use double for decimals.
  • == compares, = assigns; do not mix them.
  • Use && and || for logic, & and | for bitwise.
  • ++x increments first, x++ increments after.
  • Use bitwise operators only when you really need them.

These basics are needed for conditionals and loops.

Practice Check status with logical and

Write a Main class with a main method. Assign 25 to int age. Assign age >= 18 && age <= 65 to boolean inRange. Print inRange with println.

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        int age = 25;
        boolean inRange = age >= 18 && age <= 65;
        System.out.println(inRange);
    }
}

Expected output

true

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match the operator families

Term Meaning
+, -, *, / Arithmetic
==, != Comparison
&&, || Logical
&, | Bitwise

2 Complete the comparison operator

Question
boolean passed = average ____ 50;
Answer
boolean passed = average >= 50;

Explanation: average >= 50 means passed.

All lessons in Beginner