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

# Variables

> 5 assignments

## 9. Variables and Names - [Link](https://programmingbydoing.com/a/variables-and-names.html)

<Tabs>
  <Tab title="Task">
    You can print things out with System.out.println and you can do math. The next step is to learn about variables. In programming a variable is nothing more than a name for something so you can use the name rather than the something as you code. Programmers use these variable names to make their code read more like English, and because programmers have a lousy ability to remember things. If they didn't use good names for things in their software they'd get lost when they came back and tried to read their code again.

    If you get stuck with this exercise, remember the tricks you've been taught so far for finding differences and focusing on details:

    1. Write a comment above each line explaining to yourself what it does in English.
    2. Read your .java file backwards.
    3. Read your .java file out loud, saying even the punctuation and symbols.

    ```java VariablesAndNames.java theme={null}
    public class VariablesAndNames
    {
        public static void main( String[] args )
        {
            int cars, drivers, passengers, cars_not_driven, cars_driven;
            double space_in_a_car, carpool_capacity, average_passengers_per_car;

            cars = 100;
            space_in_a_car = 4.0;
            drivers = 30;
            passengers = 90;
            cars_not_driven = cars - drivers;
            cars_driven = drivers;
            carpool_capacity = cars_driven * space_in_a_car;
            average_passengers_per_car = passengers / cars_driven;

            System.out.println( "There are " + cars + " cars available." );
            System.out.println( "There are only " + drivers + " drivers available." );
            System.out.println( "There will be " + cars_not_driven + " empty cars today." );
            System.out.println( "We can transport " + carpool_capacity + " people today." );
            System.out.println( "We have " + passengers + " to carpool today." );
            System.out.println( "We need to put about " + average_passengers_per_car + " in each car." );
        }
    }
    ```

    <Note>The \_ in `space_in_a_car` is called an underscore character. Find out how to type it if you do not already know. We use this character a lot to put an imaginary space between words in variable names.</Note>
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java VariablesAndNames
    There are 100 cars available.
    There are only 30 drivers available.
    There will be 70 empty cars today.
    We can transport 120.0 people today.
    We have 90 to carpool today.
    We need to put about 3.0 in each car.

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

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

    1. I used 4.0 for `space_in_a_car`, but is that necessary? What happens if it's just 4?
       > Answer : It's not necessary. If it's just 4, the result will be the same.
    2. Remember that 4.0 is a "floating point" number. Find out what that means.
       > Answer : A floating-point number is a number that has either a decimal point or an exponent or both.
    3. Write comments above each of the variable assignments.
       > Answer :
       ```java VariablesAndNames.java theme={null}
       cars = 100; // Assign 100 to cars
       space_in_a_car = 4.0; // Assign 4.0 to space_in_a_car
       drivers = 30; // Assign 30 to drivers
       passengers = 90; // Assign 90 to passengers
       cars_not_driven = cars - drivers; // Assign cars - drivers to cars_not_driven
       cars_driven = drivers; // Assign drivers to cars_driven
       carpool_capacity = cars_driven * space_in_a_car; // Assign cars_driven * space_in_a_car to carpool_capacity
       average_passengers_per_car = passengers / cars_driven; // Assign passengers / cars_driven to average_passengers_per_car
       ```
    4. Make sure you know what = is called (equals) and that it's making names for things.
       > Answer : The = is called an assignment operator. It's used to assign a value to a variable.
    5. Remember \_ is an underscore character.
       > Answer : An underscore character is a character that is used to put an imaginary space between words in variable names.
  </Tab>

  <Tab title="FAQ">
    What is the difference between = (single-equal) and == (double-equal)?

    > The = (single-equal) assigns the value on the right to a variable on the left. The == (double-equal) tests if two things have the same value. You'll learn more about comparing things in a later assignment.

    What do you mean by "read the file backwards"?

    > Very simple. Imagine you have a file with 16 lines of code in it. Start at line 16, and compare it to my file at line 16. Then do it again for 15, and so on until you've read the whole file backwards.

    Why did you use 4.0 for space\_in\_a\_car? Changing it to 4 doesn't seem to do anything.

    > That is because space\_in\_a\_car was previously defined as a double variable. If it had been defined as an int variable, putting 4 into it would have made a difference.
  </Tab>
</Tabs>

## 10. More Variables and Printing - [Link](https://programmingbydoing.com/a/more-variables-and-printing.html)

<Tabs>
  <Tab title="Task">
    Now we'll do even more typing of variables and printing them out. Every time you put " (double-quotes) around a piece of text you have been making a string. A string is how you make something that your program might give to a human. You print them, save them to files, send them to web servers, all sorts of things.

    Strings are really handy, so in this exercise you'll learn how to make strings that have variables embedded in them.

    As usual, just type this in even if you don't understand it and make it exactly the same.

    ```java VariablesAndNames.java theme={null}
    public class MoreVariablesAndPrinting
    {
        public static void main( String[] args )
        {
            String myName, myEyes, myTeeth, myHair;
            int myAge, myHeight, myWeight;

            myName = "Zed A. Shaw";
            myAge = 35;     // not a lie
            myHeight = 74;  // inches
            myWeight = 180; // lbs
            myEyes = "Blue";
            myTeeth = "White";
            myHair = "Brown";

            System.out.println( "Let's talk about " + myName + "." );
            System.out.println( "He's " + myHeight + " inches tall." );
            System.out.println( "He's " + myWeight + " pounds heavy." );
            System.out.println( "Actually, that's not too heavy." );
            System.out.println( "He's got " + myEyes + " eyes and " + myHair + " hair." );
            System.out.println( "His teeth are usually " + myTeeth + " depending on the coffee." );

            // This line is tricky; try to get it exactly right.
            System.out.println( "If I add " + myAge + ", " + myHeight + ", and " + myWeight
                + " I get " + (myAge + myHeight + myWeight) + "." );
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java MoreVariablesAndPrinting
    Let's talk about Zed A. Shaw.
    He's 74 inches tall.
    He's 180 pounds heavy.
    Actually that's not too heavy.
    He's got Blue eyes and Brown hair.
    His teeth are usually White depending on the coffee.
    If I add 35, 74, and 180 I get 289.

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

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

    1. Change all the variables so there isn't the my in front. Make sure you change the name everywhere, not just where you used = to set them.
       > Answer :
       ```java MoreVariablesAndPrinting.java theme={null}
       public class MoreVariablesAndPrinting
       {
           public static void main( String[] args )
           {
               String name, eyes, teeth, hair;
               int age, height, weight;

               name = "Zed A. Shaw";
               age = 35;     // not a lie
               height = 74;  // inches
               weight = 180; // lbs
               eyes = "Blue";
               teeth = "White";
               hair = "Brown";

               System.out.println( "Let's talk about " + name + "." );
               System.out.println( "He's " + height + " inches tall." );
               System.out.println( "He's " + weight + " pounds heavy." );
               System.out.println( "Actually, that's not too heavy." );
               System.out.println( "He's got " + eyes + " eyes and " + hair + " hair." );
               System.out.println( "His teeth are usually " + teeth + " depending on the coffee." );

               // This line is tricky; try to get it exactly right.
               System.out.println( "If I add " + age + ", " + height + ", and " + weight
                   + " I get " + (age + height + weight) + "." );
           }
       }
       ```
    2. Try to write some variables that convert the inches and pounds to centimeters and kilos. Don't just type in the measurements, but work out the math inside your Java program.

       ```bash theme={null}
       Let's talk about Zed A. Shaw.
       He's 74 inches (or 187.96 cm) tall.
       He's 180 pounds (or 81.6466266 kg) heavy.
       Actually that's not too heavy.
       He's got Blue eyes and Brown hair.
       His teeth are usually White depending on the coffee.
       If I add 35, 74, and 180 I get 289.
       ```

       > Answer :

       ```java MoreVariablesAndPrinting.java theme={null}
       public class MoreVariablesAndPrinting
       {
           public static void main( String[] args )
           {
               String name, eyes, teeth, hair;
               int age, height, weight;
               double heightCm, weightKg;

               name = "Zed A. Shaw";
               age = 35;     // not a lie
               height = 74;  // inches
               weight = 180; // lbs
               eyes = "Blue";
               teeth = "White";
               hair = "Brown";

               heightCm = height * 2.54;
               weightKg = weight * 0.453592;

               System.out.println( "Let's talk about " + name + "." );
               System.out.println( "He's " + height + " inches (or " + heightCm + " cm) tall." );
               System.out.println( "He's " + weight + " pounds (or " + weightKg + " kg) heavy." );
               System.out.println( "Actually, that's not too heavy." );
               System.out.println( "He's got " + eyes + " eyes and " + hair + " hair." );
               System.out.println( "His teeth are usually " + teeth + " depending on the coffee." );

               // This line is tricky; try to get it exactly right.
               System.out.println( "If I add " + age + ", " + height + ", and " + weight
                   + " I get " + (age + height + weight) + "." );
           }
       }
       ```
  </Tab>

  <Tab title="FAQ">
    Can I make a variable like this: `1 = "Zed Shaw"`?

    > No, 1 is not a valid variable name. They need to start with a letter, so a1 would work, but 1 will not, and neither would 1a.

    How can I round a floating point number?

    > You can use the `round()` function like this: `Math.round(1.7333)`.

    Why does this not make sense to me?

    > I'm not sure, but you can try changing the numbers in this program to your own information (your height and weight, your eye color, etc). It's weird, but talking about yourself will make it seem more "real".
  </Tab>
</Tabs>

## 11. Using Variables - [Link](https://programmingbydoing.com/a/using-variables.html)

<Tabs>
  <Tab title="Task">
    Write a program that creates three variables: an `int`, a `double`, and a `String`.

    Put the value `113` into the first variable, the value `2.71828` into the second, and the value `"Computer Science"` into the third. It does not matter what you call the variables... *this time*.

    Then, display the values of these three variables on the screen, one per line.

    ```bash theme={null}
    This is room # 113
    e is close to 2.71828
    I am learning a bit about Computer Science
    ```

    Your program *SHOULD NOT* look like this:

    ```java UsingVariables.java theme={null}
    System.out.println( "This is room # 113" );
    System.out.println( "e is close to 2.71828" );
    System.out.println( "I am learning a bit about Computer Science" );
    ```

    You *must* use three variables. Your program will probably have *nine* lines of code inside the curly braces of `main()`.
  </Tab>

  <Tab title="Answer">
    ```java UsingVariables.java theme={null}
    public class UsingVariables
    {
        public static void main( String[] args )
        {
            int room;
            double e;
            String learning;

            room = 113;
            e = 2.71828;
            learning = "Computer Science";

            System.out.printf( "This is room # %d\n", room );
            System.out.printf( "e is close to %f\n", e );
            System.out.printf( "I am learning a bit about %s\n", learning );
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java UsingVariables
    This is room # 113
    e is close to 2.71828
    I am learning a bit about Computer Science

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

## 12. Still Using Variables - [Link](https://programmingbydoing.com/a/still-using-variables.html)

<Tabs>
  <Tab title="Task">
    Write a program that stores your name and year of graduation into variables, and displays their values on the screen.

    Make sure that you use two variables, and that the variable that holds your name is the best type for such a variable, and that the variable that holds the year is the best type for *that* variable.

    Also make sure that your variable names are good: the name of a variable should always relate to its contents.

    ```bash theme={null}
    My name is Juan Valdez and I'll graduate in 2010.
    ```

    Notice that in the example above, the values "Juan Valdez" and 2010 have been stored in variables before printing.

    You're *doing it wrong* if your program looks like this:

    ```java StillUsingVariables.java theme={null}
    System.out.println( "My name is Juan Valdez and I'll graduate in 2010." );
    ```

    You *must* use three variables. Your program will probably have *nine* lines of code inside the curly braces of `main()`.
  </Tab>

  <Tab title="Answer">
    ```java StillUsingVariables.java theme={null}
    public class StillUsingVariables
    {
        public static void main( String[] args )
        {
            String name =  "Muhammad Muqoffin Nuha";
            int graduation = 2027;

            System.out.printf( "My name is %s and I'll graduate in %d.\n", name, year );
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java StillUsingVariables
    My name is Muhammad Muqoffin Nuha and I'll graduate in 2027.

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

## 13. Your Schedule - [Link](https://programmingbydoing.com/a/your-schedule.html)

<Tabs>
  <Tab title="Task">
    Use several variables to store the names of your classes and their teachers. Then, display a nice little table displaying your schedule. Just FYI, my column of courses has a width of 26 characters, and the teacher column has a width of 15. The first and last lines are a plus sign, fifty dashes (a.k.a. minus signs) and another plus sign.

    Your table doesn't need to look exactly like this, or even line up. I used a total of sixteen variables (course1, course2, ... course8, teacher1, teacher2, etc.). You should do the same.

    ```bash theme={null}
    +------------------------------------------------------------+
    | 1 |                          English III |       Ms. Lapan |
    | 2 |                          Precalculus |     Mrs. Gideon |
    | 3 |                         Music Theory |       Mr. Davis |
    | 4 |                        Biotechnology |      Ms. Palmer |
    | 5 |           Principles of Technology I |      Ms. Garcia |
    | 6 |                             Latin II |    Mrs. Barnett |
    | 7 |                        AP US History | Ms. Johannessen |
    | 8 | Business Computer Infomation Systems |       Mr. James |
    +------------------------------------------------------------+
    ```
  </Tab>

  <Tab title="Answer">
    ```java YourSchedule.java theme={null}
    public class YourSchedule
    {
        public static void main( String[] args )
        {
            String course1, course2, course3, course4, course5, course6, course7;
            String teacher1, teacher2, teacher3, teacher4, teacher5, teacher6, teacher7;
            course1 = "PKN";
            course2 = "BIG";
            course3 = "MATDIS";
            course4 = "Aljabar Linear";
            course5 = "Statistika dasar";
            course6 = "Pemrograman 1";
            course7 = "Sistem Operasi";
            teacher1 = "Pak Ikhsan";
            teacher2 = "Bu Isti";
            teacher3 = "Bu Ngatini";
            teacher4 = "Bu Puji";
            teacher5 = "Pak Arif";
            teacher6 = "Bu Fiqo";

            System.out.println("+------------------------------------------------------------+");
            System.out.println("| 1 |                                 "+course1+" |       "+teacher1+" |");
            System.out.println("| 2 |                                 "+course2+" |          "+teacher2+" |");
            System.out.println("| 3 |                              "+course3+" |       "+teacher3+" |");
            System.out.println("| 4 |                      "+course4+" |          "+teacher4+" |");
            System.out.println("| 5 |                    "+course5+" |       "+teacher3+" |");
            System.out.println("| 6 |                       "+course6+" |         "+teacher5+" |");
            System.out.println("| 7 |                      "+course7+" |          "+teacher6+" |");
            System.out.println("+------------------------------------------------------------+");
        }
    }
    ```
  </Tab>

  <Tab title="Output">
    ```bash theme={null}
    U:\My Documents\CompSci\>java YourSchedule
    +------------------------------------------------------------+
    | 1 |                                 PKN |       Pak Ikhsan |
    | 2 |                                 BIG |          Bu Isti |
    | 3 |                              MATDIS |       Bu Ngatini |
    | 4 |                      Aljabar Linear |          Bu Puji |
    | 5 |                    Statistika dasar |       Bu Ngatini |
    | 6 |                       Pemrograman 1 |         Pak Arif |
    | 7 |                      Sistem Operasi |          Bu Fiqo |
    +------------------------------------------------------------+

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