> ## 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.

# Methods

* **Statement** = a line of code that performs a basic operation

* **Method** = a named sequence of statements

* Usage:
  * to do non-simple operations repeatedly
  * to break complex problems into smaller, simpler parts

## Types of Methods

| VOID Method                      | VALUE Method                    |
| -------------------------------- | ------------------------------- |
| Does not return a value          | Returns a value                 |
| Often referred to as a procedure | Often referred to as a function |

### Void Method

```java theme={null}
public static void sayHello() {
    System.out.println("Hello");
}

public static void sayHello(String name) {
    System.out.println("Hello " + name);
}

public static void main(String[] args) {
    sayHello();
    sayHello("Ani");
}
```

<Accordion title="Penjelasan">
  * Method Names ![Method Names](https://i.ibb.co/1dGM5PQ/Screenshot-2024-04-03-191448.png)
  * Parameters and Arguments ![Parameters and Arguments](https://i.ibb.co/tKWB3QY/Screenshot-2024-04-03-191459.png)
  * Statements ![Statements](https://i.ibb.co/xXhLYZv/Screenshot-2024-04-03-191511.png)
  * Overloading ![Overloading](https://i.ibb.co/SBZmkHQ/Screenshot-2024-04-03-191521.png)
</Accordion>

#### Exercise Void Method

<Tabs>
  <Tab title="Exercise 1">
    Write one or several methods to do the following

    1. Prints a String n times
    2. Display the message "Good Morning" in various languages
    3. Display an alphanumeric character as an ASCII art
  </Tab>

  <Tab title="Solution Exercise 1">
    ```java theme={null}
    public static void printString(String str, int n) {
        for (int i = 0; i < n; i++) {
            System.out.println(str);
        }
    }
    ```
  </Tab>
</Tabs>

### Value Method

```java theme={null}
public static double computeBMI(double weight, double height) {
    double bmi = weight / height / height;
    return bmi;
}

public static void main(String[] args) {
    double weight = 40;
    double height = 1.65;
    double bmi = computeBMI(weight, height);

    System.out.println("Ani's BMI is: " + bmi);
}
```

<Accordion title="Penjelasan">
  * Return type and value ![Return type and value](https://i.ibb.co/Hq66kb0/Screenshot-2024-04-03-192156.png)
  * ![2](https://i.ibb.co/n7LqLWC/Screenshot-2024-04-03-192204.png)
</Accordion>

#### Exercise Value Method

<Tabs>
  <Tab title="Exercise 1">
    Write one or several methods to do the following

    1. Calculates the distance between two coordinates
    2. Determines whether a number is prime or not
    3. Determines the number of days in a year
  </Tab>

  <Tab title="Solution Exercise 1">
    ```java theme={null}
    public static double distance(double x1, double y1, double x2, double y2) {
        double dx = x2 - x1;
        double dy = y2 - y1;
        return Math.sqrt(dx * dx + dy * dy);
    }
    ```
  </Tab>
</Tabs>

# Method Composition

```java theme={null}
public static double computeTax(double price) {
    double tax = 0;
    if (price > 100) {
        tax = 0.2 * price;
    }
    return tax;
}

public static double computePayment(double price) {
    return price + computeTax(price);
}

public static void main(String[] args) {
    double itemPrice = 600;
    double payment = computePayment(itemPrice);
}
```

# Recursive Methods

<Frame caption="https://keenformatics.blogspot.com/2013/08/how-to-solve-json-infinite-recursion.html">
  <img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglzxsfghEu3st1QOHLTdCUkcc9E6kX8tNtW2QkDC21AAcaOaS2HKDv5H7jKn_tDdmTrwiXRJSqNluSBB4hVYTVglPTpgRz4QOo_MCTx-T3AY8SR92yeQBFeSBiU994Yt1pktej5xAipoic/s320/recursion.jpg" />
</Frame>

* A method that invokes itself
* example:

<CodeGroup>
  ```java factorial theme={null}
      0! = 1
      n! = n ∙ (n – 1)!

      factorial(5) = 5 _ factorial(4)
      factorial(4) = 4 _ factorial(3)
      factorial(3) = 3 _ factorial(2)
      factorial(2) = 2 _ factorial(1)
      factorial(1) = 1 * factorial(0)
      factorial(0) = 1

  ```

  ```java Code theme={null}
  public static int factorial(int n) {
      if (n == 0) {
          return 1;
      }
      return n * factorial(n - 1);
  }

  public static void main(String[] args) {
      System.out.println(factorial(5));
  }
  ```
</CodeGroup>

* Write the ***base case***:

```java theme={null}
if (n == 0) {
    return 1;
}
```

* Write the ***reduction step***. Ensure that the base case can be reached:

```java theme={null}
return n * factorial(n - 1);
```

## Recursion vs Iteration

* Recursion is not always needed
* Everything that can be done with recursion can be done using iterations (loops)
* Recursion
  * consumes more memory and slower than iterations
  * elegant, shorter code

# Documentation

<Tabs>
  <Tab title="True">
    ![True](https://i.ibb.co/1dzQSFX/Screenshot-2024-04-03-193718.png)
  </Tab>

  <Tab title="False">
    ![False](https://i.ibb.co/c30vJXr/Screenshot-2024-04-03-193726.png)
  </Tab>
</Tabs>
