This chapter is a refresher of the Java you met in Class 9, so the new Class 10 topics build on solid ground. Java is an object-oriented language: programs are built from objects created from classes. (Java code here is for you to read and run in BlueJ/an IDE — it can't run inside the browser.)
1OOP, objects and classes
Object-Oriented Programming (OOP) models a program as a set of objects that hold data and can act on it. An object is a real-world thing (a student, a car); a class is the blueprint that describes what such objects have and can do.
class Student { // the blueprint (class)
String name; // data the object holds
int marks;
}
Student s = new Student(); // s is an object of class Student- Java is object-oriented: programs are built from objects created from classes.
- An object is a specific thing; a class is the blueprint describing such things.
- One class can create many objects using the new keyword.
2Data types, variables and operators
A variable stores a value; its data type says what kind. Java's common primitive types:
| Type | Holds | Example |
|---|---|---|
int | Whole numbers | int age = 15; |
double | Decimal numbers | double pi = 3.14; |
char | One character | char g = 'A'; |
boolean | true / false | boolean ok = true; |
Operators combine values:
- Arithmetic:
+ - * / %(% is remainder) - Relational:
< > <= >= == != - Logical:
&&(and),||(or),!(not)
- Common primitive types: int (whole), double (decimal), char (one character), boolean (true/false).
- Arithmetic operators: + - * / and % (remainder).
- Relational (<, >, ==, !=) compare; logical (&&, ||, !) combine true/false values.
3Input and Math library methods
To read input from the user, Java uses the Scanner class:
import java.util.Scanner; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // read a whole number String s = sc.next(); // read a word
Math methods
The Math class provides ready-made calculations:
| Method | Returns |
|---|---|
Math.sqrt(x) | Square root |
Math.pow(a, b) | a to the power b |
Math.abs(x) | Absolute (positive) value |
Math.max(a, b) / Math.min(a, b) | Larger / smaller |
- Scanner (from java.util) reads input: nextInt() for a number, next() for a word.
- The Math class has ready calculations: sqrt, pow, abs, max, min.
- Math methods are called on the class name, e.g. Math.sqrt(25).
4Decisions and loops
Conditional statements choose what to do:
if (marks >= 33)
System.out.println("Pass");
else
System.out.println("Fail");
switch (grade) {
case 'A': System.out.println("Great"); break;
default: System.out.println("Keep going");
}Loops repeat actions:
for (int i = 1; i <= 5; i++) // runs 5 times
System.out.println(i);
int i = 1;
while (i <= 5) { i++; } // entry-controlled
do { i++; } while (i <= 5); // exit-controlled (runs at least once)- if/else and switch choose between actions based on a condition.
- for, while and do-while repeat code; do-while runs at least once.
- A nested loop is a loop inside another loop — used for grids and patterns.
★ Practical: warm-up programs
Write (and run in BlueJ) short programs that:
- Read two numbers with Scanner and print their sum and product.
- Print whether a number entered is even or odd using if/else and %.
- Print the square root and the square of a number using Math methods.
- Print the numbers 1 to 10 using a for loop.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.