Java Code Examples: Matrix, Shapes, Math, and Threads

Java Code Examples

Matrix Operations

import java.util.Scanner;

// Class for matrix operations
class MatrixOperations {
    // Addition of two matrices
    public static int[][] addMatrices(int[][] matrix1, int[][] matrix2) {
        int rows = matrix1.length;
        int cols = matrix1[0].length;
        int[][] result = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result[i][j] = matrix1[i][j] + matrix2[i][j];
            }
        }
        return result;
    }

    // Subtraction of two matrices
    public static int[][] subtractMatrices(int[][] matrix1, int[][] matrix2) {
        int rows = matrix1.length;
        int cols = matrix1[0].length;
        int[][] result = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result[i][j] = matrix1[i][j] - matrix2[i][j];
            }
        }
        return result;
    }
}

Geometric Shapes

// Class for geometric shapes
class Shapes {
    // Class for rectangle
    static class Rectangle {
        double length;
        double width;

        // Constructor
        public Rectangle(double length, double width) {
            this.length = length;
            this.width = width;
        }

        // Method to calculate area
        public double calculateArea() {
            return length * width;
        }
    }

  // Class for cuboid
    static class Cuboid {
        double length;
        double width;
        double height;

        // Constructor
        public Cuboid(double length, double width, double height) {
            this.length = length;
            this.width = width;
            this.height = height;
        }

        // Method to calculate surface area
        public double calculateSurfaceArea() {
            return 2 * (length * width + width * height + height * length);
        }

        // Method to calculate volume
        public double calculateVolume() {
            return length * width * height;
        }
    }
}

Mathematical Operations

// Class for mathematical operations
class Mathematics {
    // Method to calculate factorial
    public static long factorial(int n) {
        if (n == 0 || n == 1)
            return 1;
        else
            return n * factorial(n - 1);
    }

    // Method to generate Fibonacci series
    public static void fibonacci(int n) {
        int a = 0, b = 1;
        System.out.print("Fibonacci series up to " + n + " terms: ");
        for (int i = 1; i <= n; ++i) {
            System.out.print(a + " ");
            int sum = a + b;
            a = b;
            b = sum;
        }
    }
}

Multithreading Example

import java.util.Scanner;

// Multithreaded program to find factorial of a number
class FactorialThread extends Thread {
    private int number;
    private long factorialResult;

    public FactorialThread(int number) {
        this.number = number;
    }

    public long getFactorialResult() {
        return factorialResult;
    }

    @Override
    public void run() {
        factorialResult = calculateFactorial(number);
    }

    private long calculateFactorial(int n) {
        if (n == 0 || n == 1)
            return 1;
        else
            return n * calculateFactorial(n - 1);
    }
}

Sum of Natural Numbers

// Program to find sum of natural numbers up to n
class SumOfNaturalNumbers {
    public static long sum(int n) {
        return (long) n * (n + 1) / 2;
    }
}

Main Class

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

        // Multithreaded factorial calculation
        System.out.print("Enter a number to find its factorial: ");
        int num = scanner.nextInt();

        FactorialThread factorialThread = new FactorialThread(num);
        factorialThread.start();

        try {
            factorialThread.join();
            System.out.println("Factorial of " + num + " is: " + factorialThread.getFactorialResult());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Sum of natural numbers
        System.out.print("Enter a number to find the sum of natural numbers up to it: ");
        int n = scanner.nextInt();
        long sum = SumOfNaturalNumbers.sum(n);
        System.out.println("Sum of natural numbers up to " + n + " is: " + sum);

        scanner.close();
    }
}