Ever wondered how Java does the math you see in games, or how it reads what you type?
💡 In Simple Words: The Math class is a ready‑made toolbox that gives you handy formulas like square‑root or random numbers. The Scanner class is like a friendly robot that listens to the keyboard and hands you the data you typed.
What are Library Classes in Java?
Library classes are pre‑written code that lives in Java’s standard library. Think of them as a giant cookbook: each class is a recipe you can call whenever you need a particular dish, without having to cook from scratch.
Using the Math Class
Common Math Methods
The Math class is a collection of static methods – that means you don’t create an object, you just write Math.methodName(). Here are the most useful ones for ICSE exams:
- Math.abs(x) – gives the absolute (positive) value of
x. Like measuring the distance you walked, regardless of direction. - Math.sqrt(x) – returns the square‑root of
x. Ifxis the area of a square, this method tells you the side length. - Math.pow(a,b) – raises
ato the powerb. Imagine stackingablocksbhigh. - Math.max(a,b) and Math.min(a,b) – pick the larger or smaller of two numbers.
- Math.random() – gives a random double between 0.0 (inclusive) and 1.0 (exclusive). Like rolling a dice that lands anywhere on a smooth line.
Why Math is a Static Class
Because its methods don’t rely on any particular object’s state. You can think of a static class as a toolbox that stays in the garage; you just walk in, pick the tool you need, and walk out.
Getting User Input with Scanner
Creating a Scanner Object
First you import the class: import java.util.Scanner;. Then you create an object that reads from System.in (the keyboard):
Scanner sc = new Scanner(System.in);Now sc is ready to listen.
Important Scanner Methods
- nextInt() – reads the next integer.
- nextDouble() – reads the next floating‑point number.
- nextLine() – reads an entire line of text, including spaces.
- hasNext() – checks if there’s more input waiting.
All these methods pause the program until the user types something and hits Enter, just like waiting for a friend to finish speaking.
Putting Them Together: A Simple Calculator
Let’s build a tiny program that asks for two numbers, raises the first to the power of the second, and prints the result rounded to two decimal places.
import java.util.Scanner;
public class SimpleCalc {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter base: ");
double base = sc.nextDouble();
System.out.print("Enter exponent: ");
double exp = sc.nextDouble();
double raw = Math.pow(base, exp);
double rounded = Math.round(raw * 100.0) / 100.0;
System.out.println("Result: " + rounded);
sc.close();
}
}
Notice how we never instantiated Math; we just called Math.pow directly.
Quick Comparison: Math vs Scanner
| Feature | Math Class | Scanner Class |
|---|---|---|
| Purpose | Provides mathematical functions | Reads user input from keyboard |
| How to use | Static methods, e.g., Math.sqrt(9) | Instantiate object, e.g., Scanner sc = new Scanner(System.in) |
| Common methods | abs, sqrt, pow, max, min, random | nextInt, nextDouble, nextLine, hasNext |
| Returns | Primitive data types (int, double, etc.) | Primitive data types or String |
📝 Likely Exam Questions
- Q1: Write a Java snippet to read an integer and print its square using Math class.
A:Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println(Math.pow(n,2)); - Q2: Explain the difference between
next()andnextLine()in Scanner.
A:next()reads input up to the next whitespace, whilenextLine()reads the entire line including spaces. - Q3: How do you generate a random integer between 1 and 100 using Math class?
A:int rand = (int)(Math.random()*100) + 1; - Q4: What will
Math.max(5, Math.min(8,3))return and why?
A: It returns 5 becauseMath.min(8,3)is 3, and thenMath.max(5,3)picks the larger value, 5. - Q5: Describe how you would close a Scanner object and why it matters.
A: Callsc.close();after you’re done. It releases the underlying input stream and prevents resource leaks.