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

# Keyboard Input

> 7 assignments

## 14. Asking Questions - [Link](https://programmingbydoing.com/a/asking-questions.html)

<Tabs>
  <Tab title="Task">
    It's now time to pick up the pace a bit. I've got you doing a lot of printing so that you get used to typing simple things, but those simple things are fairly boring. What we want to do now is get you getting data into your programs. This though is a little tricky so we have to have you learn to do two things that may not make sense right away, but if you stick with it they should click suddenly a few exercises from now.

    Most of what software does is the following:

    1. Take some kind of input from a person.
    2. Change it.
    3. Print out something to show how it changed.

    So far you've only been printing, but you haven't been able to get any input from a person, or change it. You may not even know what "input" means, so rather than talk about it, let's have you do some and see if you get it. Next exercise we'll do more to explain it.

    ```java AskingQuestions.java theme={null}
    import java.util.Scanner;

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

            int age;
            String height;
            double weight;

            System.out.print( "How old are you? " );
            age = keyboard.nextInt();

            System.out.print( "How tall are you? " );
            height = keyboard.next();

            System.out.print( "How much do you weigh? " );
            weight = keyboard.nextDouble();

            System.out.println( "So you're " + age + " old, " + height + " tall and " + weight + " heavy." );
        }
    }
    ```

    <Note>Notice that we used print instead of println. This is so that the program doesn't end the line with a newline and go to the next line.</Note>
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java AskingQuestions
    How old are you? 35
    How tall are you? 6'2"
    How much do you weigh? 180
    So, you're 35 old, 6'2" tall and 180 heavy.

    U:\My Documents\CompSci\>
    ```
  </Tab>

  <Tab title="Question">
    Assignments turned in *without* these things will not receive any points.

    1. Change the program so that it reads in the height in two parts. The first part should read in an int for the number of feet. Then read in a second int for the number of inches. Try to make the output look the same, though.
       ```bash theme={null}
       How old are you? 35
       How many feet tall are you? 6
       And how many inches? 2
       How much do you weigh? 180
       So, you're 35 old, 6'2" tall and 180 heavy.
       ```
       > Answer :
       ```java AskingQuestions.java theme={null}
       import java.util.Scanner;

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

               int age, feet, inches;
               double weight;

               System.out.print("How old are you? ");
               age = keyboard.nextInt();

               System.out.print("How many feet tall are you? ");
               feet = keyboard.nextInt();
               System.out.print("And how many inches? ");
               inches = keyboard.nextInt();

               System.out.print("How much do you weight? ");
               weight = keyboard.nextDouble();

               System.out.print("So you're " + age + " old, " + feet + "'" + inches + "\" tall and " + weight + " heavy.");
           }
       }
       ```
  </Tab>
</Tabs>

## 15. The Forgetful Machine - [Link](https://programmingbydoing.com/a/the-forgetful-machine.html)

<Tabs>
  <Tab title="Task">
    Ask the user for two words and two numbers, and let the person at the keyboard type in some values, but don't bother storing their responses into any variables.

    Again, there is no need to create *any variables*, except for the `Scanner` variable typically named `keyboard`.

    ```bash theme={null}
    Give me a word!
    lorry!
    Give me a second word!
    deoxyribonucleic?

    Great, now your favorite number?
    42
    And your second-favorite number...
    1

    Whew!  Wasn't that fun?
    ```
  </Tab>

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

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

            System.out.println("Give me a word!");
            keyboard.next();
            System.out.println("Give me a second word!");
            keyboard.next();
            System.out.println("\nGreat, now your favorite number?");
            keyboard.nextInt();
            System.out.println("And your second-favorite numbber...");
            keyboard.nextInt();

            System.out.println("\nWhew! Wasn't that fun?");
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java ForgetfulMachine
    Give me a word!
    muqoffin
    Give me a second word!
    nuha

    Great, now your favorite number?
    5
    And your second-favorite numbber...
    10

    Whew! Wasn't that fun?

    U:\My Documents\CompSci\>
    ```
  </Tab>
</Tabs>

## 16. Name, Age, and Salary - [Link](https://programmingbydoing.com/a/name-age-and-salary.html)

<Tabs>
  <Tab title="Task">
    Ask the user for their name. Then display their name to prove that you can recall it. Ask them for their age. Then display that. Finally, ask them for how much they make and display that. You should use the most appropriate data type for each variable.

    ```bash theme={null}
    Hello.  What is your name?
    Dennis 

    Hi, Dennis!  How old are you?
    37 

    So you're 37, eh?  That's not old at all!
    How much do you make, Dennis?
    8.50 

    8.5!  I hope that's per hour and not per year! LOL!
    ```

    ```bash theme={null}
    Hello.  What is your name?
    Catsup 

    Hi, Catsup!  How old are you?
    12 

    So you're 12, eh?  That's not old at all!
    How much do you make, Catsup?
    99.9 

    99.9!  I hope that's per hour and not per year! LOL!
    ```
  </Tab>

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

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

            String name;
            int age;
            double salary;

            System.out.println("Hello. What is your name?");
            name = keyboard.next();

            System.out.println("\nHi, " + name + "! How old are you?");
            age = keyboard.nextInt();

            System.out.println("\nSo you're " + age + ", eh? That's not old at all!");
            System.out.println("How much do you make, " + name + "?");
            salary = keyboard.nextDouble();

            System.out.println("\n" + salary + "! I hope that's per hour and not per year! LOL!");
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java NameAgeAndSalary
    Hello. What is your name?
    Muqoffin

    Hi, Muqoffin! How old are you?
    21

    So you're 21, eh? That's not old at all!
    How much do you make, Muqoffin?
    1000

    1000.0! I hope that's per hour and not per year! LOL!

    U:\My Documents\CompSci\>
    ```
  </Tab>
</Tabs>

## 17. More User Input of Data - [Link](https://programmingbydoing.com/a/more-user-input-of-data.html)

<Tabs>
  <Tab title="Task">
    Ask the user for several pieces of information, and display them on the screen afterward as a summary.

    * first name
    * last name
    * grade (classification)
    * student id number
    * login name
    * GPA (0.0 to 4.0)
      You must use the most appropriate type for each variable and not just `Strings` for everything.

    ```bash theme={null}
    Please enter the following information so I can sell it for a profit!

    First name: Helena
    Last name: Bonham-Carter
    Grade (9-12): 12
    Student ID: 453916
    Login: bonham_453916
    GPA (0.0-4.0): 3.73

    Your information:
        Login: bonham_453916
        ID:    453916
        Name:  Bonham-Carter, Helena
        GPA:   3.73
        Grade: 12
    ```
  </Tab>

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

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

            String firstName, lastName, login, studentID;
            int grade;
            double gpa;

            System.out.println("Please enter the following information so I can sell it for a profit!\n");

            System.out.print("First name: ");
            firstName = keyboard.next();
            System.out.print("Last name: ");
            lastName = keyboard.next();
            System.out.print("Grade (9-12): ");
            grade = keyboard.nextInt();
            System.out.print("Student ID: ");
            studentID = keyboard.next();
            System.out.print("Login: ");
            login = keyboard.next();
            System.out.print("GPA (0.0-4.0): ");
            gpa = keyboard.nextDouble();

            System.out.println("\nYour information:");
            System.out.println("\tLogin:  "+login);
            System.out.println("\tID:     "+studentID);
            System.out.println("\tName:   "+lastName+", "+firstName);
            System.out.println("\tGPA:    "+gpa);
            System.out.println("\tGrade:  "+grade);
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java MoreUserInputOfData
    Please enter the following information so I can sell it for a profit!

    First name: Muqoffin
    Last name: Nuha
    Grade (9-12): 12
    Student ID: 123456
    Login: muqoffin_123456
    GPA (0.0-4.0): 3.5

    Your information:
        Login: muqoffin_123456
        ID:    123456
        Name:  Nuha, Muqoffin
        GPA:   3.5
        Grade: 12

    U:\My Documents\CompSci\>
    ```
  </Tab>
</Tabs>

## 18. Age in Five Years - [Link](https://programmingbydoing.com/a/age-in-five-years.html)

<Tabs>
  <Tab title="Task">
    Ask the user for their name. Then display their name to prove that you can recall it. Ask them for their age. Then display what their age would be five years from now. Then display what their age would be five years ago.

    ```bash theme={null}
    Hello.  What is your name? Percy_Bysshe_Shelley

    Hi, Percy_Bysshe_Shelley!  How old are you? 34

    Did you know that in five years you will be 39 years old?
    And five years ago you were 29! Imagine that!
    ```

    ```bash theme={null}
    Hello.  What is your name? Gramps

    Hi, Gramps!  How old are you? 87

    Did you know that in five years you will be 92 years old?
    And five years ago you were 82! Imagine that!
    ```
  </Tab>

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

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

            String name;
            int age;

            System.out.print("Hello. What is your name? ");
            name = keyboard.next();

            System.out.print("\nHi, " + name + "! How old are you? ");
            age = keyboard.nextInt();

            System.out.println("\nDid you know that in five years you will be " + (age + 5) + " years old?");
            System.out.println("And five years ago you were " + (age - 5) + "! Imagine that!");
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java AgeIn5
    Hello. What is your name? Muqoffin

    Hi, Muqoffin! How old are you? 21

    Did you know that in five years you will be 26 years old?
    And five years ago you were 16! Imagine that!

    U:\My Documents\CompSci\>
    ```
  </Tab>
</Tabs>

## 19. A Dumb Calculator - [Link](https://programmingbydoing.com/a/a-dumb-calculator.html)

<Tabs>
  <Tab title="Task">
    Make a simple numeric calculator. It should prompt the user for three numbers. Then add the numbers together and divide by 2. Display the result. Your program must support numbers with decimals and not just integers.

    ```bash theme={null}
    U:\>java DumbCalculator

    What is your first number? 1.1
    What is your second number? 2.2
    What is your third number? 5.5

    ( 1.1 + 2.2 + 5.5 ) / 2 is... 4.4
    ```
  </Tab>

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

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

            double firstNumber, secondNumber, thirdNumber, results;

            System.out.print("What is your first number? ");
            firstNumber = keyboard.nextDouble();
            System.out.print("What is your second number? ");
            secondNumber = keyboard.nextDouble();
            System.out.print("What is your third number? ");
            thirdNumber = keyboard.nextDouble();

            results = (firstNumber + secondNumber + thirdNumber) / 2;

            System.out.println("\n( " + firstNumber + " + " + secondNumber + " + " + thirdNumber + " ) / 2 is... " + results);
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java DumbCalculator
    What is your first number? 1.1
    What is your second number? 2.2
    What is your third number? 5.5

    ( 1.1 + 2.2 + 5.5 ) / 2 is... 4.4

    U:\My Documents\CompSci\>
    ```
  </Tab>
</Tabs>

## 20. BMI Calculator - [Link](https://programmingbydoing.com/a/bmi-calculator.html)

<Tabs>
  <Tab title="Task">
    (This assignment was suggested by Joel H in 2012.)

    The body mass index (BMI) is commonly used by health and nutrition professionals to estimate human body fat in populations.

    It is computed by taking the individual's weight (mass) in kilograms and dividing it by the square of their height in meters.

    ```bash theme={null}
    Your height in m: 1.75
    Your weight in kg: 73

    Your BMI is 23.83673
    ```
  </Tab>

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

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

            double height, weight, bmi;

            System.out.print("Your height in m: ");
            height = keyboard.nextDouble();
            System.out.print("Your weight in kg: ");
            weight = keyboard.nextDouble();

            bmi = weight / Math.pow(height, 2);

            System.out.printf("\nYour BMI is %.5f", bmi);
        }
    }
    ```
  </Tab>

  <Tab title="Bonus">
    Bonus #1 - Imperial Measurements

    1. For +10 bonus points, input their weight and height using pounds and inches, and convert to kilograms and meters to figure the BMI.
       ```bash theme={null}
       Your height in inches: 69
       Your weight in pounds: 160

       Your BMI is 23.625289
       ```
       > Answer :
       ```java BMICalc.java theme={null}
       import java.util.Scanner;

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

               double height, weight, bmi;

               System.out.print("Your height in inches: ");
               height = keyboard.nextDouble();
               System.out.print("Your weight in pounds: ");
               weight = keyboard.nextDouble();

               height = height * 0.0254;
               weight = weight * 0.453592;

               bmi = weight / Math.pow(height, 2);

               System.out.printf("\nYour BMI is %.6f", bmi);
           }
       }
       ```
    2. For an extra +3 bonus points (+13 total), input their height in feet and inches.
       ```bash theme={null}
       Your height (feet only): 5
       Your height (inches): 9
       Your weight in pounds: 160

       Your BMI is 23.625289
       ```
       > Answer :
       ```java BMICalc.java theme={null}
       import java.util.Scanner;

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

               double height, weight, bmi, feet, inches;

               System.out.print("Your height (feet only): ");
               feet = keyboard.nextDouble();
               System.out.print("Your height (inches): ");
               inches = keyboard.nextDouble();
               System.out.print("Your weight in pounds: ");
               weight = keyboard.nextDouble();

               height = (feet * 12 + inches) * 0.0254;
               weight = weight * 0.453592;

               bmi = weight / Math.pow(height, 2);

               System.out.printf("\nYour BMI is %.6f", bmi);
           }
       }
       ```
  </Tab>
</Tabs>
