Methods, APIs & The Math Class

Learn how to read a method, call it correctly, and use Java's built-in tools with confidence.

đŸĨ¤ Ordering Machine

Click the choices and see how one order becomes a method call.

Button
Parameter 1: Size
Parameter 2: Sugar
Result
Ready to Order
Press Place Order.
Current Order
Milk Tea
Medium
Less Sugar

🧾 Three Steps in the Machine

The real-world pattern comes first: choose, customize, receive.

☕ From Machine to Java

In Java, the pattern is still tool, inputs, and result.

1

Pick a Tool

In Java, that becomes the method name.
2

Pass In Values

Your choices become the input values.
3

Get a Result

The method runs and gives something back.

Method Call

drink = orderDrink("Milk Tea", "Large", "Less Sugar")

Tool Name

orderDrink names the tool.

Input Values

The three strings are the input values.

Returned Result

The variable drink stores the returned result.

🧰 What Is a Method?

A method is a named tool that does a job or gives a result.

Do Something

Some methods perform a job when you call them.

Return Something

Other methods return a value you can use.

Use the Name

Use a method by calling its name.

Abstraction

Focus on what it does before how it works.

Action Example

showMessage();

Value Example

applyTax(12);

Stored Result

int books = countBooks(3);

📖 How Do We Use It?

When something is unfamiliar, its instructions tell us what it does and how to use it.

🔎 Inside a Method Signature

Read each part of a method before using it.

int Return TypeValue returned findMax Method NameAction to call Parameter List (int first, Parameter 1Type: intName: first int second) Parameter 2Type: intName: second

🧾 Reading Signatures

Use the same parts to read several method signatures.

MethodGuide.java
1

Find the Name

Look for the tool name first.
2

Count Parameters

Count how many inputs it needs.
3

Parameter Types

Check which input types it expects.
4

Return Type

Check what type it gives back.

🔍 Signature Quiz (1/7)

Identify the method name, parameter count and types, and return type.

SignatureQuiz.java

🔍 Signature Quiz (2/7)

Identify the method name, parameter count and types, and return type.

SignatureQuiz.java

🔍 Signature Quiz (3/7)

Identify the method name, parameter count and types, and return type.

SignatureQuiz.java

🔍 Signature Quiz (4/7)

Identify the method name, parameter count and types, and return type.

SignatureQuiz.java

🔍 Signature Quiz (5/7)

Identify the method name, parameter count and types, and return type.

SignatureQuiz.java

🔍 Signature Quiz (6/7)

Identify the method name, parameter count and types, and return type.

SignatureQuiz.java

🔍 Signature Quiz (7/7)

Identify the method name, parameter count and types, and return type.

SignatureQuiz.java

🤝 The Method Contract

Javadoc promises what must be true before and after a call.

Contract.java
🔒

Precondition

The before promise: what the caller must supply.

đŸŽ¯

Postcondition

The after promise: what the method guarantees to return.

📝

Javadoc Tags

@param and @return record the contract.

📞 Calling a Method

Use the method name and place argument values inside the parentheses.

Method Signature

int findMax(int first, int second)
Keep the name ¡ replace parameters with argument values

Method Call

findMax(8, 13);

Method Name

Call findMax.

Arguments

Pass 8 and 13 to the method.

Parentheses

Place the arguments inside ( ).

â†Šī¸ Void and Returned Values

A return type determines where a method call can be used.

ReturnTypeCalls.java

void Method

A void method performs an action but does not return a value.

Call it as a statement; do not store it or place it inside an expression.

Non-void Method

A non-void method returns a value with the type named in its header.

Store the value in a compatible variable or use it in an expression.

🧩 Arguments Must Match

Check the number, order, and compatible types before making a call.

ArgumentMatching.java

Number

Supply the same number of arguments as parameters.

Order

Match each argument to the parameter in the same position.

Type

Each argument must have a compatible type.

🔍 Call Matching Quiz (1/7)

Does the call match the parameter list?

CallMatching.java

🔍 Call Matching Quiz (2/7)

Does the call match the parameter list?

CallMatching.java

🔍 Call Matching Quiz (3/7)

Does the call match the parameter list?

CallMatching.java

🔍 Call Matching Quiz (4/7)

Does the call match the parameter list?

CallMatching.java

🔍 Call Matching Quiz (5/7)

Does the call match the parameter list?

CallMatching.java

🔍 Call Matching Quiz (6/7)

Does the call match the parameter list?

CallMatching.java

🔍 Call Matching Quiz (7/7)

Does the call match the parameter list?

CallMatching.java

🧃 How Overloading Feels

One familiar name can lead to different actions based on the inputs.

đŸ§Ŧ Overloaded Methods

The same method name can support different parameter lists.

OverloadedMethods.java

Same Name

Overloaded methods must share the same method name.

Different Parameters

The ordered parameter type lists must differ; parameter names do not matter.

Return Type Is Ignored

Changing only the return type does not create an overload.

🔍 Overloading Quiz (1/6)

Do these two method signatures create valid overloaded methods?

OverloadingQuiz.java

🔍 Overloading Quiz (2/6)

Do these two method signatures create valid overloaded methods?

OverloadingQuiz.java

🔍 Overloading Quiz (3/6)

Do these two method signatures create valid overloaded methods?

OverloadingQuiz.java

🔍 Overloading Quiz (4/6)

Do these two method signatures create valid overloaded methods?

OverloadingQuiz.java

🔍 Overloading Quiz (5/6)

Do these two method signatures create valid overloaded methods?

OverloadingQuiz.java

🔍 Overloading Quiz (6/6)

Do these two method signatures create valid overloaded methods?

OverloadingQuiz.java

📚 API & Libraries

Do not guess. Read the tools first.

Library

A library is a collection of ready-made classes.

API

An API explains how to use those classes correctly.

Appendix Habit

Read the available documentation before you guess a method call.

When a method is unfamiliar, check the Java Quick Reference ↗ before guessing how to call it.

đŸ“Ļ Inside a Java Library

Packages organize classes, and classes group data with behavior.

Package

A package groups related classes inside a library.

Class

A class defines a reference type that can be used to create objects.

Attributes

Attributes are the data associated with a class and stored in variables.

Behaviors

Behaviors describe what objects can do and are defined by methods.

Existing classes and libraries let us create useful objects without building every tool from the beginning.

🧰 Java's Built-In Tools

Java already gives you many ready-made tools.

Math

Use Math for calculations.
abs()
pow()
sqrt()

String

Use String methods for text.
length()
substring()
equals()

Scanner

Use Scanner for keyboard input.
nextInt()
nextDouble()
nextLine()

đŸˇī¸ Class Methods

These methods belong to the class, so we call them with the class name and a dot.

ClassMethods.java

Class Tool

A class method belongs to the class, not one object.

Uses static

A class method header contains the keyword static.

Name + Dot

Call it with the class name, a dot, and the method name.

🧮 The Math Class

Java gives you ready-made methods for common calculations

int abs(int a)

Returns the absolute value.

Math.abs(-5) -> 5

double abs(double a)

Returns the absolute value.

Math.abs(-2.75) -> 2.75

double pow(double a, double b)

Raises a number to a power.

Math.pow(2, 3) -> 8.0

double sqrt(double a)

Returns the square root.

Math.sqrt(25) -> 5.0

double random()

Returns a value where 0.0 ≤ value < 1.0.

Math.random() -> a new value each run

More Math tools

Other methods exist too.

max() min() ceil() floor() round()

â†Šī¸ Math Return Values

Each Math call gives back a value, and that value has a type.

ReturnValues.java

🔍 Math Quiz (1/8)

What value is stored in the variable?

MathQuiz.java

🔍 Math Quiz (2/8)

What value is stored in the variable?

MathQuiz.java

🔍 Math Quiz (3/8)

What value is stored in the variable?

MathQuiz.java

🔍 Math Quiz (4/8)

What value is stored in the variable?

MathQuiz.java

🔍 Math Quiz (5/8)

What value is stored in the variable?

MathQuiz.java

🔍 Math Quiz (6/8)

What value is stored in the variable?

MathQuiz.java

🔍 Math Quiz (7/8)

What value is stored in the variable?

MathQuiz.java

🔍 Math Quiz (8/8)

What value is stored in the variable?

MathQuiz.java

🎲 How a Range Works

Generate a random number from 1 to 6.

0
1
2
3
4
5
6
7
8
9
10
[0.0, 1.0)
Step 1
Start with Math.random()

Java starts with a random decimal between 0.0 and 1.0.

Expression
Math.random()

🔍 Random Range Quiz (1/6)

Which integers could the variable store?

RandomRangeQuiz.java

🔍 Random Range Quiz (2/6)

Which integers could the variable store?

RandomRangeQuiz.java

🔍 Random Range Quiz (3/6)

Which integers could the variable store?

RandomRangeQuiz.java

🔍 Random Range Quiz (4/6)

Which integers could the variable store?

RandomRangeQuiz.java

🔍 Random Range Quiz (5/6)

Which integers could the variable store?

RandomRangeQuiz.java

🔍 Random Range Quiz (6/6)

Which integers could the variable store?

RandomRangeQuiz.java

📝 Homework

Complete it on time and submit it before the deadline.

🧭 Next Class

Check what comes next and arrive ready.

🔖 Summary

Key takeaways from this lesson.

Methods

A method is a named tool that performs a task or returns a result.

Signatures

Read the return type, method name, and ordered parameters.

Arguments

Arguments must match the parameter list in number, order, and type.

Class Methods

Call Math methods with the class name, a dot, and the method name.

01 / 58