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.
drink = orderDrink("Milk Tea", "Large", "Less Sugar")

Tool Name

orderDrink names the tool.

Input Values

The blue strings are the inputs.

Returned Result

drink stores the result.

What Is a Method?

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

Do Something

Some methods do a job when you call them.

Return Something

Other methods give back a value you can use.

Use the Name

You 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);

Reading Signatures

A method signature shows return type, name, and parameters.

MethodGuide.java
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

For each signature, describe the name, parameter count, parameter types, and return type.

SignatureQuiz.java
java
Question 1 of 7

API & Libraries

Do not guess. Read the tools first.

Library

A library is a collection of ready-made tools.

API

An API shows how to use those tools correctly.

Appendix Habit

Check the CED appendix before you guess a method.

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
java

Class Tool

A class method is connected to the class, not one object.

Uses static

In the method header, class methods use the keyword static.

Name + Dot

We usually call them 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 random value from 0.0 to 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
java

Math Quiz

For each line, decide what value is stored in the variable.

MathQuiz.java
java
Question 1 of 8

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

For each line, decide which integers the variable could store.

RandomRangeQuiz.java
java
Question 1 of 6

Course Summary

Key ideas for methods, APIs, and the Math class

Methods

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

Signatures

A signature shows the return type, method name, and parameters.

Return Types

Some methods return a value like int or double, and some use void.

Class Methods

Math methods are class methods, so we call them with the class name, a dot, and the method name.

01 / 16