Skip to main content

Methods

Standard Library Methods

Introduction

So, imagine you're cooking, and you have your own recipe (those are like the methods you create). But sometimes, you use pre-made ingredients or tools (those are like the standard library methods).

In Java, there are these pre-made methods that are already there for you to use without having to explain how they work. We call them "standard library methods." It's like having a magic tool that can do something specific, and you just need to know how to use it.

For example, in the Java programming language, there's a tool called System.out.println(). It's like a magical printer that prints whatever you want on the screen. You don't need to know exactly how the printer works; you just need to know how to tell it what to print.

So, when you write System.out.println("Hello World!"), you're using this pre-made tool to print "Hello World!" on the screen. You didn't have to create the printing tool; it's already part of the Java kitchen toolkit.

And there are many other tools like nextInt() for getting number inputs. These tools are like helpers you can call on whenever you need them, and you don't have to worry about how they do their job – you just use them!

Magic Math Library Methods

Imagine you have a super calculator in Java that can do all sorts of math tricks. Well, all these math tricks are neatly packed inside a special box called the "Math class."

So, when you want to use these math tricks, you just ask the Math class nicely. For example, if you want to find the square root of a number, there's a trick called sqrt() inside the Math class that you can use.

It's like having a treasure chest of math tools, and Math is the key to open that chest.

Now, let's go through some of the cool math tricks one by one.

Finding a power of a number

Well, in Java, there's a cool method called pow() that lets you find the power of a number. It's like telling the calculator, "Hey, raise this number to that power for me!"

Let's try it out in a simple program:

import java.util.Scanner;

class PowerFinder {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Get input values for the base and power
        System.out.println("Enter the base number:");
        double base = input.nextDouble();
        
        System.out.println("Enter the power:");
        double power = input.nextDouble();

        // Find the power of the number
        double result = Math.pow(base, power);
        System.out.println("Result: " + result);
    }
}

Now, when you run this program, it will ask you for a base number and a power. You can enter any numbers, and it will magically tell you the result of raising the base to that power.

For example:

Enter the base number:
2
Enter the power:
3
Result: 8.0

Give it a try with different numbers!

Finding the square root of a number

There is also a standard method called sqrt() that lets you calculate the square root. It's like telling the calculator, "Hey, give me the square root of this number!"

Let's try it out with a simple program:

import java.util.Scanner;

class SquareRootFinder {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Get a number to find the square root
        System.out.println("Enter a number to find the square root:");
        double number = input.nextDouble();

        // Find the square root
        double squareRoot = Math.sqrt(number);

        System.out.println("Square root of " + number + " is " + squareRoot);
    }
}

Now, when you run this program, it will ask you for a number, and it will magically tell you the square root of that number.

For example:

Enter a number to find the square root:
25
Square root of 25 is 5.0

Give it a try with different numbers!

Finding the Highest Value

The Math.max(x, y) method is like asking, "Hey calculator, tell me the bigger number between x and y."

For example:

int result = Math.max(5, 9);
// result will be 9, because 9 is bigger than 5

Give it a try with different numbers!

Finding the Lowest Value

The Math.min(x, y) method is like saying, "Calculator, what's the smaller number between x and y?"

For example:

int result = Math.min(5, 9);
// result will be 5, because 5 is smaller than 9.

Give it a try with different numbers!

Finding the Absolute Value

The Math.abs(x) method is like saying, "Calculator, make x positive no matter what."

For example:

int result = Math.abs(-7);
// result will be 7, because the absolute value of -7 is 7.

Give it a try with different numbers!

Getting a Random Number

The Math.random() method is like asking, "Give me a random number between 0 and 1."

For example:

double random = Math.random();
// random could be something like 0.54 or 0.89

If you want more control, like getting a random whole number between 0 and 50, you can use this trick:

int random = (int)(Math.random() * 51);
// random could be anything from 0 to 50.

It's like telling the computer, "Hey, give me a random number, but I only want it between 0 and 50, and make it a whole number." The (int) part helps in turning the decimal into a whole number. Give it a try!