Ever wondered how Java can crunch numbers or read your keyboard input in a flash?

The Math class is like a built‑in calculator, and the Scanner class is your friendly ear that listens to what you type. Together they let you do math and interact with the user without writing extra code.

What is a Library Class in Java?

A library class lives inside Java’s standard library – a huge toolbox that comes with every Java installation. Think of it as a ready‑made set of gadgets you can pull out whenever you need them, just like a kitchen drawer full of spoons, forks, and knives.

Java Math Class – Your Pocket Calculator

The Math class packs static methods (functions you call directly without creating an object) for common arithmetic and trigonometric tasks. It’s perfect for ICSE questions that ask you to find squares, cubes, or random numbers.

Most‑used Math methods

MethodWhat it doesExample
abs(x)Returns the absolute value (makes negative numbers positive)Math.abs(-5) // 5
max(a,b)Gives the larger of two numbersMath.max(3,9) // 9
min(a,b)Gives the smaller of two numbersMath.min(4,7) // 4
sqrt(x)Square‑root of xMath.sqrt(16) // 4.0
pow(a,b)a raised to the power bMath.pow(2,3) // 8.0
random()Returns a double between 0.0 (inclusive) and 1.0 (exclusive)Math.random()*10 // 0‑9.999…

Worked example – Finding the area of a circle

double radius = 5.0;
double area = Math.PI * Math.pow(radius, 2); // πr²
System.out.println("Area = " + area);

Here Math.PI is a constant that stores the value of π, and Math.pow raises the radius to the power of 2.

Java Scanner Class – Talking to the User

The Scanner class is like a microphone that listens to whatever you type on the keyboard (or any other input stream). You create a Scanner object linked to System.in, then call its methods to fetch numbers, words, or whole lines.

Common Scanner methods

  • nextInt() – reads the next integer token
  • nextDouble() – reads the next floating‑point number
  • nextLine() – reads everything up to the end‑of‑line
  • hasNext() – checks if there is more input available
  • close() – releases the underlying stream (good practice after you’re done)

Simple input‑output program

import java.util.Scanner;

public class SumTwoNumbers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int a = sc.nextInt();
        System.out.print("Enter second number: ");
        int b = sc.nextInt();
        int sum = a + b;
        System.out.println("Sum = " + sum);
        sc.close();
    }
}

This tiny program shows how Scanner makes reading user input almost as easy as asking a friend a question.

Math vs. Scanner – Quick Comparison

AspectMath ClassScanner Class
PurposePerform calculationsRead data from input streams
Typical useFinding roots, powers, random numbersGetting numbers or strings from the keyboard
InstantiationNever needed (static methods)Need to create a Scanner object
Common methodsabs, max, sqrt, pow, randomnextInt, nextDouble, nextLine, hasNext

Tips for ICSE Exams

  • Remember that Math methods are static – you call them with Math.methodName(), not with an object.
  • Always close your Scanner with sc.close() to avoid resource leaks.
  • Use Math.round() when the question asks for the nearest integer.
  • If a problem needs a random integer between 1 and 10, combine Math.random() with casting: (int)(Math.random()*10) + 1.
  • Practice reading different data types in one go – e.g., int id = sc.nextInt(); String name = sc.next(); double salary = sc.nextDouble();

📝 Likely Exam Questions

  1. Write a Java program to read a number and display its absolute value using the Math class.
    int n = sc.nextInt(); System.out.println(Math.abs(n));
  2. How do you generate a random integer between 5 and 15 (inclusive) in Java?
    Use (int)(Math.random()*11) + 5; because Math.random() gives 0‑0.999…, multiply by the range size (11), then shift by the lower bound (5).
  3. Explain the difference between next() and nextLine() methods of Scanner.
    next() reads the next token (stops at whitespace), while nextLine() reads the whole line up to the newline character.
  4. Calculate the area of a rectangle whose length and breadth are entered by the user.
    Scanner sc = new Scanner(System.in);
      double l = sc.nextDouble();
      double b = sc.nextDouble();
      double area = l * b;
      System.out.println("Area = " + area);
      sc.close();
      
  5. What will be the output of System.out.println(Math.pow(2, 3) + Math.sqrt(16));?
    Math.pow(2,3) = 8.0 and Math.sqrt(16) = 4.0, so the result is 12.0.
#ICSE#Class 10#Java#Math class#Scanner class