package expressions;

public class MathExpressions {
    public static void main(String[] args) {

        // some ints and doubles to play with
        int n1 = 2, n2 = 4, n3 = 5;
        double d1 = 1.5, d2 = 2.0;

        /*
         * NOTE: The assignment operator:
         * the = is the assignment operator:
         * a = b
         * it assigns b to a (direction of assignment is right to left)
         */

        // arithmetic operators with integers
        System.out.println("integer arithmetic");
        System.out.printf("%d + %d = %d\n", n1, n2, n1 + n2);
        System.out.printf("%d - %d = %d\n", n2, n3, n2 - n3);
        System.out.printf("%d * %d = %d\n", n1, n3, n1 * n3);
        System.out.printf("%d / %d = %d\n", n3, n1, n3 / n1);
        System.out.printf("%d %% %d = %d\n", n3, n1, n3 % n1);

        // arithmetic operators with floating points
        System.out.println("floating point arithemtic");
        System.out.printf("%.1f + %.1f = %.2f\n", d1, d2, d1 + d2);
        System.out.printf("%.1f - %.1f = %.2f\n", d1, d2, d1 - d2);
        System.out.printf("%.1f * %.1f = %.2f\n", d1, d2, d1 * d2);
        System.out.printf("%.1f / %.1f = %.2f\n", d1, d2, d1 / d2);
        System.out.printf("%.1f %% %.1f = %.2f\n", d2, d1, d2 % d1);

        // mixing integers and floating points
        System.out.println("mixed types");
        System.out.printf("%d + %.1f = %.1f\n", n1, d1, n1 + d1);
        System.out.printf("%.1f - %d = %.1f\n", d2, n1, d2 - n1);
        System.out.printf("%d * %.1f = %.1f\n", n2, d2, n2 * d2);
        System.out.printf("%d / %.1f = %.1f\n", n3, d2, n3 / d2);
        System.out.printf("%d %% %.1f = %.1f\n", n3, d2, n3 % d2);
        // when at least one operand is a floating point, the result
        // is always a floating point

        /*
         * Shorthand Notation not Permitted
         * In Java, you can't use shorthand notation that you might
         * commonly use when you write arithemtic expressions by hand.
         * For example, you might write "xy" to mean "x times y" but
         * this is not permitted in Java (and this is true of most
         * programming languages).
         */

        // invalid:
        // int product1 = n1n2;
        // int product2 = (n1)(n2);

        // valid:
        // int product3 = n1 * n2;

        /*
         * Exponents/Powers
         * We don't use ^ or ** for exponent operations in Java.
         * e.g. these are invalid:
         * x^y
         * x**y
         * Instead, you have to use a method from the Math class:
         * Math.pow(x, y)
         * We'll cover this in another demonstration today.
         */

        /*
         * Java follows the standard order of precedence used in
         * basic mathematics, just like all other programming languages.
         * https://www.cs.bilkent.edu.tr/~guvenir/courses/CS101/op_precedence.html
         */
        System.out.println("order of precedence:");
        System.out.println(n1 + n2 * n3); // 2 + 4 * 5
        System.out.println((n1 + n2) * n3); // (2 + 4) * 5
        System.out.println(d1 * n1 + n2 * d2); // 1.5 * 2 + 4 * 2.0
        System.out.println(d1 * (n1 + n2) * d2);  // 1.5 * (2 + 4) * 2.0

        // Recall that this doesn't add n1 and n2. Why? Fix it.
        // System.out.println("n1 + n2 = " + n1 + n2);

    }
}
