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

# Functions Part 2

> 4 assignments

## 110. Keychains for Sale, real ultimate power - [Link](https://programmingbydoing.com/a/keychains3.html)

<Tabs>
  <Tab title="Task">
    You're going to add some error checking and additional features, to Keychains2.

    You need to make sure that the user always has a positive number, or 0, of keychains in the order.

    You need to check for a valid menu choice. If not, display an error message and show the menu again.

    You will need 3 new variables in main, one to store the sales tax (8.25%), one to store the shipping cost per order ($5.00), and one to store the additional per keychain shipping cost ($1.00).

    view\_order() will need to be passed the three additional variables, a total of five, and have a return type of void. It will display, on different lines, the number of keychains in the order, the price per keychain, the shipping charges on the order, the subtotal before tax, the tax on the order, and the final cost of the order.

    view\_order() might look like public static void view\_order( int num\_keychains, double price\_per\_keychain, double tax, int base\_shipping, int per\_keychain\_shipping )

    checkout() will need to be passed the same values as view\_order(), and have a return type of void. It will ask the user for his/her name in order to deliver them correctly, then call view\_order() to display the order information, and then thank the user, by name, for ordering.

    **Sample Output**

    ```bash theme={null}
    Ye Olde Keychain Shoppe

    1. Add Keychains to Order
    2. Remove Keychains from Order
    3. View Current Order
    4. Checkout

    Please enter your choice: 1

    You have 0 keychains. How many to add? 3
    You now have 3 keychains.

    1. Add Keychains to Order
    2. Remove Keychains from Order
    3. View Current Order
    4. Checkout

    Please enter your choice: 2

    You have 3 keychains. How many to remove? 1
    You now have 2 keychains.

    1. Add Keychains to Order
    2. Remove Keychains from Order
    3. View Current Order
    4. Checkout

    Please enter your choice: 3

    You have 2 keychains.
    Keychains cost $10 each.
    Shipping charges are $5.00.
    Subtotal before tax is $20.00.
    Tax on the order is $1.65.
    Total cost is $26.65.

    1. Add Keychains to Order
    2. Remove Keychains from Order
    3. View Current Order
    4. Checkout

    Please enter your choice: 4

    CHECKOUT

    What is your name? Biff
    You have 2 keychains.
    Keychains cost $10 each.
    Shipping charges are $5.00.
    Subtotal before tax is $20.00.
    Tax on the order is $1.65.
    Total cost is $26.65.
    Thanks for your order, Biff!
    ```
  </Tab>

  <Tab title="Code">
    ```java Keychains3.java theme={null}
    import java.util.Scanner;

    public class Keychains3
    {
        public static void main( String[] args )
        {
            Scanner keyboard = new Scanner(System.in);
            int choice;
            int keychains = 0;
            double price = 10;
            double tax = 0.0825;
            double shipping = 5;
            double perShipping = 1;

            System.out.println("Ye Olde Keychain Shoppe\n");

            do
            {
                System.out.println("1. Add Keychains to Order");
                System.out.println("2. Remove Keychains from Order");
                System.out.println("3. View Current Order");
                System.out.println("4. Checkout\n");
                System.out.print("Please enter your choice: ");
                choice = keyboard.nextInt();

                if ( choice == 1 )
                {
                    keychains = add_keychains(keychains);
                }
                else if ( choice == 2 )
                {
                    keychains = remove_keychains(keychains);
                }
                else if ( choice == 3 )
                {
                    view_order(keychains, price, tax, shipping, perShipping);
                }
                else if ( choice == 4 )
                {
                    checkout(keychains, price, tax, shipping, perShipping);
                }
                else
                {
                    System.out.println("Invalid choice.");
                }
            } while ( choice != 4 );
        }

        public static int add_keychains( int keychains )
        {
            Scanner keyboard = new Scanner(System.in);
            int add;

            System.out.printf("\nYou have %s keychains. How many to add? ", keychains);
            add = keyboard.nextInt();
            keychains += add;
            System.out.printf("You now have %s keychains.\n\n", keychains);

            return keychains;
        }

        public static int remove_keychains( int keychains )
        {
            Scanner keyboard = new Scanner(System.in);
            int remove;

            System.out.printf("\nYou have %s keychains. How many to remove? ", keychains);
            remove = keyboard.nextInt();
            keychains -= remove;
            System.out.printf("You now have %s keychains.\n\n", keychains);

            return keychains;
        }

        public static void view_order( int keychains, double price, double tax, double shipping, double perShipping )
        {
            System.out.printf("\nYou have %s keychains.\n", keychains);
            System.out.printf("Keychains cost $%s each.\n", price);
            System.out.printf("Shipping charges are $%s.\n", shipping);
            System.out.printf("Subtotal before tax is $%s.\n", keychains * price);
            System.out.printf("Tax on the order is $%s.\n", (keychains * price + shipping) * tax);
            System.out.printf("Total cost is $%s.\n\n", (keychains * price + shipping) + (keychains * price + shipping) * tax);
        }

        public static void checkout( int keychains, double price, double tax, double shipping, double perShipping )
        {
            Scanner keyboard = new Scanner(System.in);
            String name;
            System.out.print("\nCHECKOUT\n\n");

            System.out.print("What is your name? ");
            name = keyboard.next();
            System.out.printf("You have %s keychains.\n", keychains);
            System.out.printf("Keychains cost $%s each.\n", price);
            System.out.printf("Shipping charges are $%s.\n", shipping);
            System.out.printf("Subtotal before tax is $%s.\n", keychains * price);
            System.out.printf("Tax on the order is $%s.\n", (keychains * price + shipping) * tax);
            System.out.printf("Total cost is $%s.\n", (keychains * price + shipping) + (keychains * price + shipping) * tax);
            System.out.printf("Thanks for your order, %s!\n", name);
        }
    }
    ```
  </Tab>
</Tabs>

## 111. Calling Functions from Other Files - [Link](https://programmingbydoing.com/a/calling-functions-from-other-files.html)

<Tabs>
  <Tab title="Task">
    Rewrite the [Weekday Calculator](https://programmingbydoing.com/a/weekday-calculator.html) to have almost no functions in it. Start by opening up `WeekdayCalculator.java` and saving a copy of it as `CallingFunctionsFromOtherFiles.java`.

    Then erase all the functions except for `main()` and `weekday()`.

    Now, when you compile it, you should get a lot of errors about undefined functions.

    Then rewrite all the function calls so that they refer to versions in your previous assignments. The functions will be these:

    * `MonthName.month_name()`
    * `WeekdayName.weekday_name()`
    * `MonthOffset.month_offset()`
    * `WeekdayCalculator.is_leap()`
  </Tab>

  <Tab title="Code">
    <CodeGroup>
      ```java CallingFunctionsFromOtherFiles.java theme={null}
      import java.util.Scanner;

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

              System.out.println("Welcome to Mr. Mitchell's fantastic birth-o-meter!");
              System.out.println("All you have to do is enter your birth date, and it will tell you the day of the week on which you were born.");
              System.out.println();
              System.out.println("Some automatic tests....");
              System.out.println("12 10 2003 => " + weekday(12,10,2003));
              System.out.println(" 2 13 1976 => " + weekday(2,13,1976));
              System.out.println(" 2 13 1977 => " + weekday(2,13,1977));
              System.out.println(" 7  2 1974 => " + weekday(7,2,1974));
              System.out.println(" 1 15 2003 => " + weekday(1,15,2003));
              System.out.println("10 13 2000 => " + weekday(10,13,2000));
              System.out.println();

              System.out.println("Now it's your turn!  What's your birthday?");
              System.out.print("Birth date (mm dd yyyy): ");
              int mm = keyboard.nextInt();
              int dd = keyboard.nextInt();
              int yyyy = keyboard.nextInt();

              System.out.println("You were born on " + weekday(mm,dd,yyyy));
          }

          public static String weekday( int mm, int dd, int yyyy )
          {
              int yy, total;
              String date = "";

              yy = yyyy - 1900;
              total = yy / 4;
              total += yy;
              total += dd;
              total += MonthOffset.month_offset( mm );

              if ( WeekdayCalculator.is_leap( yyyy ) && (mm == 1 || mm == 2) )
                  total--;

              date = WeekdayName.weekday_name( total % 7 ) + ", " + MonthName.month_name( mm ) + " " + dd + ", " + yyyy;

              return date;
          }
      }
      ```

      ```java MonthName.java theme={null}
      public class MonthName
      {
          public static String month_name( int month )
          {
              String result = "";

              if ( month == 1 )
              {
                  result = "January";
              }
              else if ( month == 2 )
              {
                  result = "February";
              }
              else if ( month == 3 )
              {
                  result = "March";
              }
              else if ( month == 4 )
              {
                  result = "April";
              }
              else if ( month == 5 )
              {
                  result = "May";
              }
              else if ( month == 6 )
              {
                  result = "June";
              }
              else if ( month == 7 )
              {
                  result = "July";
              }
              else if ( month == 8 )
              {
                  result = "August";
              }
              else if ( month == 9 )
              {
                  result = "September";
              }
              else if ( month == 10 )
              {
                  result = "October";
              }
              else if ( month == 11 )
              {
                  result = "November";
              }
              else if ( month == 12 )
              {
                  result = "December";
              }

              return result;
          }
      }
      ```

      ```java WeekdayName.java theme={null}
      public class WeekdayName
      {
          public static String weekday_name( int weekday )
          {
              String result = "";

              if ( weekday == 0 )
              {
                  result = "Sunday";
              }
              else if ( weekday == 1 )
              {
                  result = "Monday";
              }
              else if ( weekday == 2 )
              {
                  result = "Tuesday";
              }
              else if ( weekday == 3 )
              {
                  result = "Wednesday";
              }
              else if ( weekday == 4 )
              {
                  result = "Thursday";
              }
              else if ( weekday == 5 )
              {
                  result = "Friday";
              }
              else if ( weekday == 6 )
              {
                  result = "Saturday";
              }

              return result;
          }
      }
      ```

      ```java MonthOffset.java theme={null}
      public class MonthOffset
      {
          public static int month_offset( int month )
          {
              int result = 0;

              if ( month == 1 )
              {
                  result = 1;
              }
              else if ( month == 2 )
              {
                  result = 4;
              }
              else if ( month == 3 )
              {
                  result = 4;
              }
              else if ( month == 4 )
              {
                  result = 0;
              }
              else if ( month == 5 )
              {
                  result = 2;
              }
              else if ( month == 6 )
              {
                  result = 5;
              }
              else if ( month == 7 )
              {
                  result = 0;
              }
              else if ( month == 8 )
              {
                  result = 3;
              }
              else if ( month == 9 )
              {
                  result = 6;
              }
              else if ( month == 10 )
              {
                  result = 1;
              }
              else if ( month == 11 )
              {
                  result = 4;
              }
              else if ( month == 12 )
              {
                  result = 6;
              }

              return result;
          }
      }
      ```

      ```java WeekdayCalculator.java theme={null}
      public class WeekdayCalculator
      {
          public static boolean is_leap( int year )
          {
              // years which are evenly divisible by 4 are leap years,
              // but years which are evenly divisible by 100 are not leap years,
              // though years which are evenly divisible by 400 are leap years
              boolean result;

              if ( year % 400 == 0 )
                  result = true;
              else if ( year % 100 == 0 )
                  result = false;
              else if ( year % 4 == 0 )
                  result = true;
              else
                  result = false;

              return result;
          }
      }
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## 112. Evenness Function - [Link](https://programmingbydoing.com/a/evenness-function.html)

<Tabs>
  <Tab title="Task">
    Write a function like so:

    ```java theme={null}
    public static boolean isEven( int n )
    ```

    The function should return the value `true` if n is an even number (evenly divisible by 2) and `false` otherwise.

    Also, write

    ```java theme={null}
    public static boolean isDivisibleBy3( int n )
    ```

    The function should return the value `true` if n is evenly divisible by 3 and `false` otherwise.

    Write a `main()` that contains a `for` loop to generate all the numbers from 1 to 20. Use `if` statements inside the loop to mark the number with a "\<" if it's even, with a "=" if it's evenly divisible by 3, and with both if it's divisible by both 2 and 3.

    **Sample Output**

    <CodeGroup>
      ```bash EvennessFunction 1 theme={null}
      1
      2 <
      3 =
      4 <
      5
      6 <
      6 =
      7
      8 <
      9 =
      10 <
      11
      12 <
      12 =
      13
      14 <
      15 =
      16 <
      17
      18 <
      18 =
      19
      20 <
      ```

      ```bash EvennessFunction 2 theme={null}
      If you're cool, it's possible to make the display like this:

      1
      2 <
      3 =
      4 <
      5
      6 <=
      7
      8 <
      9 =
      10 <
      11
      12 <=
      13
      14 <
      15 =
      16 <
      17
      18 <=
      19
      20 <
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Code">
    <CodeGroup>
      ```java EvennessFunction.java 1 theme={null}
      public class EvennessFunction
      {
          public static void main( String[] args )
          {
              for ( int i = 1; i <= 20; i++ )
              {
                  if ( isEven(i) )
                      System.out.println(i + " <");
                  if ( isDivisibleBy3(i) )
                      System.out.println(i + " =");
                  if ( !isEven(i) && !isDivisibleBy3(i) )
                      System.out.println(i);
              }
          }

          public static boolean isEven( int n )
          {
              return ( n % 2 == 0 );
          }

          public static boolean isDivisibleBy3( int n )
          {
              return ( n % 3 == 0 );
          }

      }
      ```

      ```java EvennessFunction.java 2 theme={null}
      public class EvennessFunction
      {
          public static void main( String[] args )
          {
              for ( int i = 1; i <= 20; i++ )
              {
                  if ( isEven(i) && isDivisibleBy3(i) )
                      System.out.println(i + " <=");
                  else if ( isEven(i) )
                      System.out.println(i + " <");
                  else if ( isDivisibleBy3(i) )
                      System.out.println(i + " =");
                  else
                      System.out.println(i);
              }
          }

          public static boolean isEven( int n )
          {
              return ( n % 2 == 0 );
          }

          public static boolean isDivisibleBy3( int n )
          {
              return ( n % 3 == 0 );
          }
      }
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## 113. Finding Prime Numbers - [Link](https://programmingbydoing.com/a/finding-prime-numbers.html)

<Tabs>
  <Tab title="Task">
    Write a function like so:

    ```java theme={null}
    public static boolean isPrime( int n )
    ```

    The function should return the value `true` if `n` is a prime and `false` otherwise.

    Remember that a number is prime if is isn't evenly divisible by anything except for 1 and itself. You can figure this out by using a `for` loop inside the function.

    Make the for loop run through all the numbers from 2 up to n. Inside the loop, use an if statement that determines if n is evenly divisible by your loop control variable.

    If you find any number which divides it evenly, you can go ahead and return `false` from the function without finishing the loop.

    If the loop finishes and doesn't find any numbers which divide it, then return `true` from the function.

    After you finish writing the function write a `main()` that contains another `for` loop. Have it print out all the numbers from 2 to 20, and mark each prime number with a "\<".

    **Sample Output**

    <CodeGroup>
      ```bash FindingPrimeNumbers 1 theme={null}
      2 <
      3 <
      4
      5 <
      6
      7 <
      8
      9
      10
      11 <
      12
      13 <
      14
      15
      16
      17 <
      18
      19 <
      20
      ```

      ```bash FindingPrimeNumbers 2 theme={null}
      If you prefer, you may print out *only* the prime numbers up to 100 or so, like this:

      2
      3
      5
      7
      11
      13
      17
      19
      23
      29
      31
      37
      41
      43
      47
      53
      59
      61
      67
      71
      73
      79
      83
      89
      97
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Code">
    <CodeGroup>
      ```java FindingPrimeNumbers.java 1 theme={null}
      public class FindingPrimeNumbers
      {
          public static void main( String[] args )
          {
              for ( int i = 2; i <= 20; i++ )
              {
                  if ( isPrime(i) )
                      System.out.println(i + " <");
                  else
                      System.out.println(i);
              }
          }

          public static boolean isPrime( int n )
          {
              for ( int i = 2; i < n; i++ )
              {
                  if ( n % i == 0 )
                      return false;
              }

              return true;
          }
      }
      ```

      ```java FindingPrimeNumbers.java 2 theme={null}
      public class FindingPrimeNumbers
      {
          public static void main( String[] args )
          {
              for ( int i = 2; i <= 100; i++ )
              {
                  if ( isPrime(i) )
                      System.out.println(i);
              }
          }

          public static boolean isPrime( int n )
          {
              for ( int i = 2; i < n; i++ )
              {
                  if ( n % i == 0 )
                      return false;
              }

              return true;
          }
      }
      ```
    </CodeGroup>
  </Tab>
</Tabs>
