CodeIEB
Theory Notes/๐Ÿ—„๏ธ Topic 4: Data & Information Management, Solution Development/10.4.5
10.4.5Grade 10

Basic Methods, Functions & Objects

Your first real steps into using pre-built functionality (Math functions) and Object-Oriented Programming (instantiating objects from existing classes).

Common mathematical functions and their typical use in Java (via the Math class):

PurposeJava example
Minimum/maximum of two valuesMath.min(a, b), Math.max(a, b)
Power/exponentMath.pow(base, exponent)
Absolute valueMath.abs(x)
Square rootMath.sqrt(x)
Rounding to whole numberMath.round(x)
Random numberMath.random() โ€” returns a double between 0.0 and 1.0

Integer arithmetic โ€” calculating a dividend and remainder together, e.g. splitting seconds into minutes and remaining seconds:

Example

int totalSeconds = 125; int minutes = totalSeconds / 60; // dividend: 2 int remainingSeconds = totalSeconds % 60; // remainder: 5

Working with objects from existing classes:

Instantiate
Create a specific object (instance) from a class, e.g. `Scanner input = new Scanner(System.in);` creates a Scanner object.
Constructor
A special method, matching the class name, called automatically when an object is created โ€” used to set up the object's initial state.
Typed method / function
A method that returns a value of a specific type, e.g. a method that returns an int or a String.
Void method / procedure
A method that performs an action but does not return any value.

๐Ÿ’ก Exam Tip

A method call that returns a value can be used directly in an expression, e.g. `int result = Math.max(x, y) + 5;` โ€” a void method cannot, since it has no value to plug into the expression.