Summary - Operators
- Java course
- Beginner
- Lesson 29/116
- 5 min
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
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
doublefor decimals. ==compares,=assigns; do not mix them.- Use
&&and||for logic,&and|for bitwise. ++xincrements 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
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
boolean passed = average ____ 50;
boolean passed = average >= 50;
Explanation: average >= 50 means passed.