JetBrains Academy: Math library - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Math library

Heron's formula:

Many years ago when Paul went to school, he did not like the Heron's formula to calculate the area of a triangle, because he considered it very complex. Once he decided to help all school students: to write and distribute the program, calculating the area of a triangle by its three sides.

However, there was a problem: as Paul did not like the formula, he did not memorize it. Help him finish this act of kindness and write the program, calculating the area of a triangle by the transferred length of its sides, in accordance with the Heron's formula:

S = √ p(p−a)(p−b)(p−c)

where p = a+b+c/2 – half-perimeter of the triangle. On the input, the program has integers, and the output should be a real number corresponding to the area of the triangle.

import java.util.*;

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

        double area = calculateArea(a, b, c);
        System.out.printf("%f", area);
    }

    public static double calculateArea(int a, int b, int c) {
        if (a + b <= c || a + c <= b || b + c <= a) {
            return 0.0;
        } else {
            double p = (a + b + c) / 2.0;
            return Math.sqrt(p * (p - a) * (p - b) * (p - c));
        }
    }
}

Pow:

You are given two floating-point numbers: a and b.

Calculate and output the value of the expression ab.

Note: use double variables for a and b.

Input data format:
Two floating-point numbers in one line.

Output data format:
The result of the expression.

import java.util.*;

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

        System.out.println(Math.pow(a, b));
    }
}

The angle between vectors:

You are given two 2D vectors. Find the angle (in degrees) between them.

Input data format:
The first line contains two components of the first vector; the second line contains two components of the second vector. Components in one line are separated by space.

Output data format:
One double value: an angle between two vectors. The result can have an error of less than 1e-8.

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int u1 = scanner.nextInt();
        int u2 = scanner.nextInt();
        int v1 = scanner.nextInt();
        int v2 = scanner.nextInt();

        Vector u = new Vector(u1, u2);
        Vector v = new Vector(v1, v2);

        double angle = calculateAngle(u, v);
        System.out.println(angle);
    }

    private static double calculateAngle(Vector u, Vector v) {
        double product = dotProduct(u, v);
        double lengthU = u.length();
        double lengthV = v.length();

        double cos = product / (lengthU * lengthV);

        return Math.toDegrees(Math.acos(cos));
    }

    private static double dotProduct(Vector u, Vector v) {
        return u.getX() * v.getX() + u.getY() * v.getY();
    }

}

class Vector {
    private final double x;
    private final double y;

    Vector(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public double length() {
        return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
    }
}

Quadratic equation:

You are given real numbers a, b and c, where a ≠ 0.

Solve the quadratic equation ax2 + bx + c = 0 and output all of its roots in ascending order.

It is guaranteed that the equation always has two roots.

import java.util.*;

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

        double[] roots = solveQuadraticEquation(a, b, c);
        Arrays.stream(roots).sorted().forEach(e -> System.out.print(e + " "));
    }

    private static double[] solveQuadraticEquation(double a, double b, double c) {
        double[] roots = new double[2];
        // ax2 + bx + c = 0
        final double sqrt = Math.sqrt(Math.pow(b, 2) - 4 * a * c);
        roots[0] = (-b - sqrt) / (2 * a);
        roots[1] = (-b + sqrt) / (2 * a);
        return roots;
    }
}

⚠️ **GitHub.com Fallback** ⚠️