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.

Try It

Build a Drink Order

Button
Parameter 1: Size
Parameter 2: Sugar
orderDrink

Choose one button first. That decides which tool you want to use.

Milk TeaMediumLess Sugar

These are the choices you pass in before the machine can do the job.

Ready to Order

Choose the inputs, then press Place Order.

Code Analogy
orderDrink("Milk Tea", "Medium", "Less Sugar")

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
System.out.println("Hi");
Value Example
Math.random()
Stored Result
int bigger = Math.max(4, 9);

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

Which call matches the method signature?

Which call correctly matches pow(double a, double b)?

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()

The Math Class

Java gives you ready-made methods for common calculations

int abs(int a)
Returns the absolute value.
Math.abs(-5) -> 5
int max(int a, int b)
Returns the larger value.
Math.max(4, 9) -> 9
int min(int a, int b)
Returns the smaller value.
Math.min(4, 9) -> 4
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

Return Values

Some methods return a value, and some use void.

ReturnValues.java
java

Math Quiz

What does this method call print?

Quiz.java
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 Number Ranges

Use multiplication and casting to build the range you want.

RandomRange.java
java

1 to 6

Multiply by 6, cast to int, then add 1.

10 to 19

First build 0 to 9, then shift the range by 10.

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.

Built-In Tools

Java already gives you ready-made tools such as Math, String, and Scanner.
Check the CED appendix before guessing.

After-Class
Assignment

Method Notes

  • 1. Create one variable to store a Math result such as abs or sqrt.
  • 2. Print the variable with System.out.println.
  • 3. Add one short comment that labels the method name and return type.

Dice Builder

  • 1. Create a variable for one Math result such as pow or sqrt.
  • 2. Create another variable for a random integer from 1 to 6.
  • 3. Print both values with clear labels using System.out.println.