> ## Documentation Index
> Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Advanced Arithmetic Operation, Input, Output Formatting

# Advanced Arithmetic Operation

## Math class ([docs](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html))

`Math` is a class that provides common mathematical operations:

* Power and root
* Logarithm
* Max, Min
* Trigonometry
* Random
* ...

```java Program.java theme={null}
public class Program {
    public static void main(String[] args) {
        double n = 5;
        double power = 4;
        double nPower = Math.pow(n, power);
        System.out.println(n + " to the " + power + "th power = " + nPower
        );
    }
}
```

# Data Type: Integer and Double

<AccordionGroup>
  <Accordion title="Integer [+,-,*,/] Integer = Integer">
    ```java theme={null}
    double a = 3.75, b = 2.5; 
    int p = 13, q = 4; 
    System.out.println(p + q); 
    System.out.println(p - q); 
    System.out.println(p * q);
    System.out.println(p / q); 
    ```
  </Accordion>

  <Accordion title="Double [+,-,*,/] Double = Double">
    ```java theme={null}
    double a = 3.75, b = 2.5;
    int p = 13, q = 4;
    System.out.println(a + b);
    System.out.println(a - b);
    System.out.println(a * b);
    System.out.println(a / b);
    ```
  </Accordion>

  <Accordion title="Double [+,-,*,/] Integer = Double">
    ```java theme={null}
    double a = 3.75, b = 2.5;
    int p = 13, q = 4;
    System.out.println(a + p);
    System.out.println(a - p);
    System.out.println(a * p);
    System.out.println(a / p);
    ```
  </Accordion>

  <Accordion title="Integer / Integer">
    ```java theme={null}
    System.out.println(1 / 2);     //  0
    System.out.println(3 / 2);     //  1
    System.out.println(14 / 3);    //  4
    System.out.println(-1 / 2);    //  0
    System.out.println(-3 / 2);    // -1
    System.out.println(-14 / 3);   // -4
    ```
  </Accordion>

  <Accordion title="Integer → Double">
    ```java theme={null}
    double a = 3.75, b = 2.5;
    int p = 13, q = 4;
    double c = p / q;
    double d = (double) p / q;
    double e = (double) (p / q);
    System.out.println(c);
    System.out.println(d);
    System.out.println(e);
    ```
  </Accordion>

  <Accordion title="Double → Integer">
    ```java theme={null}
    double a = 3.75, b = 2.5;
    int p = 13, q = 4;
    int r = a / b;
    int s = (int) a / b;
    int t = (int) (a / b);
    System.out.println(r);
    System.out.println(s);
    System.out.println(t);
    ```
  </Accordion>
</AccordionGroup>

# Input

<CodeGroup>
  ```java Before theme={null}
  String name = "Arif";
  String message = "Welcome " + name + "!";

  System.out.println(message);
  ```

  ```java After theme={null}
  import java.util.Scanner;
  public class Program {
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          String name = input.nextLine();
          String message = "Welcome " + name + "!";

          System.out.println(message);
      }
  }
  ```

  ```java Code theme={null}
  Scanner input = new Scanner(System.in);
  System.out.print("What is your name? ");
  String name = input.nextLine();
  String message = "Welcome " + name + "!";

  System.out.println(message);
  ```
</CodeGroup>

## Basic Methods of Scanner

### Reading Strings

<Tabs>
  <Tab title="nextLine()">One line</Tab>
  <Tab title="next()">One word</Tab>
</Tabs>

### Reading Numners

<Tabs>
  <Tab title="nextInt()">Integer</Tab>
  <Tab title="nextDouble()">Double</Tab>
  <Tab title="nextLong()">Long</Tab>
</Tabs>

## Special Case

![Special Case](https://i.ibb.co/rdBybYH/Screenshot-2024-04-03-150910.png)

<CodeGroup>
  ```java Before theme={null}
  Scanner input = new Scanner(System.in);
  System.out.print("What is your name? ");
  String name = input.nextLine();
  System.out.print("What is your age? ");
  int age = input.nextInt();
  System.out.println("Hello, " + name);
  System.out.println("You are " + age + " years old");
  ```

  ```java After theme={null}
  Scanner input = new Scanner(System.in);
  System.out.print("What is your age? ");
  int age = input.nextInt(); // apa yang terjadi ketika nextInt digunakan sebelum NextLine?
  System.out.print("What is your name? ");
  String name = input.nextLine();
  System.out.println("Hello, " + name);
  System.out.println("You are " + age + " years old");
  ```

  ```java Solution theme={null}
  Scanner input = new Scanner(System.in);
  System.out.print("What is your age? ");
  int age = input.nextInt();
  input.nextLine(); // tambahkan ini untuk mengatasi permasalahan nextInt sebelum nextLine
  System.out.print("What is your name? ");
  String name = input.nextLine();
  System.out.println("Hello, " + name);
  System.out.println("You are " + age + " years old");
  ```
</CodeGroup>

# Output Formatting

## Too Many Trailing Digits!

```java theme={null}
double athird = 1.0 / 3;
System.out.println(athird);

//Output 0.3333333333333333 - Too Long
```

## Try use this!

### System.out.printf()

* System.out.format()
  ```java theme={null}
  System.out.printf(format, param1, param2, ...);
  ```
* Example
  ```java theme={null}
  double tax = 0.2 * price;
  System.out.printf("Price: %.2f, Tax: %.2f", price, tax);
  ```

## Format Works

![Format](https://i.ibb.co/44FYrBk/Screenshot-2024-04-03-151455.png)

## Converters & Flags

| Converters | Flag | Description                                                                      |
| :--------: | :--: | :------------------------------------------------------------------------------- |
|      d     |      | Integer                                                                          |
|      f     |      | Float/Double                                                                     |
|      n     |      | Newline                                                                          |
|      s     |      | String                                                                           |
|            |   +  | Includes sign, whether positive or negative.                                     |
|            |   ,  | Includes locale-specific grouping characters (thousands)                         |
|            |   -  | Left-justified.                                                                  |
|            |   8  | Eight characters in width, right justified                                       |
|            |  08  | Eight characters in width, with leading zeroes as necessary.                     |
|            |  .3  | Three places after decimal point.                                                |
|            | 10.3 | Ten characters in width, right justified, with three places after decimal point. |

```java theme={null}
String name = "Ani";
int age = 6;
double saving = 1234567.89012345;

System.out.printf("Name: %s %n", name);
System.out.printf("Name: %10s %n", name);
System.out.printf("Name: %-10s %n", name);

System.out.printf("Age: %d years old %n", age);
System.out.printf("Age: %2d years old %n", age);
System.out.printf("Age: %02d years old %n", age);

System.out.printf("Saving: Rp %f %n", saving);
System.out.printf("Saving: Rp %.2f %n", saving);
System.out.printf("Saving: Rp %12.2f %n", saving);
System.out.printf("Saving: Rp %5.2f %n", saving);
System.out.printf("Saving: Rp %,.2f %n", saving);
```

## Saving Formatted String in Variables

```java theme={null}
String.format(format, param1, param2, ...)
```

* Example:
  ```java theme={null}
  String priceString = String.format("Rp %.2f", price);
  ```
