Changes between Version 5 and Version 6 of ControlSystems/SoftwareTeam/Training/GettingStarted/Variables


Ignore:
Timestamp:
Aug 3, 2020, 5:06:51 PM (5 years ago)
Author:
Angelina Zhou
Comment:

added variable types to descriptions of what variables can hold

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/Variables

    v5 v6  
    1 Let's try a slightly more complex program that will introduce variables.  A variable is like a box that can hold something for you.  In Java, you can put only one type of thing in a given box.  Variables can hold things like:
    2 * An integer number (e.g. 42)
    3 * A real number (e.g. 3.141592)
    4 * A character (e.g. 'a' or '0' or '!')
    5 * A string of characters (e.g. "Howdy")
    6 * Something you define
     1Let's try a slightly more complex program that will introduce variables.  A variable is like a box that can hold something for you.  In Java, you can put only one type of thing in a given box.  Different types of variables can hold different things like:
     2
     3 * `int` can hold an integer number (e.g. 42)
     4 * `double` can hold a real number (e.g. 3.141592)
     5 * `char` can hold a character (e.g. 'a' or '0' or '!')
     6 * `String` can hold a string of characters (e.g. "Howdy")
     7 * `boolean` can hold values with two states: true or false
    78
    89Variables are an important part of every program because they allow the program to do different things depending on what's in the box.
    910
    10 1. Close your current project if you have one open (File->Close Folder)
    11 2. Make a new folder in your Java Projects folder named !VariablesExample
    12 3. Open the folder (File->Open Folder)
    13 4. Create a new file: (File->New File)
    14 5. Cut and paste the program below into the editor window:
     11 1. Close your current project if you have one open (File->Close Folder)
     12 1. Make a new folder in your Java Projects folder named !VariablesExample
     13 1. Open the folder (File->Open Folder)
     14 1. Create a new file: (File->New File)
     15 1. Cut and paste the program below into the editor window:
    1516{{{
    1617public class VariablesExample {
     
    2728}
    2829}}}
    29 6. Save the file as !VariablesExample.java (File->Save) [[Image(JavaVariables.jpg,50%,margin=10,align=right)]]
    30 7. Run the program
    31 8. In the output terminal window, observe that the number 12 is output.
     30 1. Save the file as !VariablesExample.java (File->Save) [[Image(JavaVariables.jpg,50%,margin=10,align=right)]]
     31 1. Run the program
     32 1. In the output terminal window, observe that the number 12 is output.
    3233
    3334=== Understanding the program ===
    3435Our example program:
    35 * Creates 3 variables (boxes) named a, b, c.  They are all integer variables (they can only hold integer numbers). 
    36 * The program then places the number 3 in variable a, the number 4 in variable b, and the product of whatever is in a and b into variable c. 
    37 * Finally, the program prints whatever value is in variable c.
     36
     37 * Creates 3 variables (boxes) named a, b, c.  They are all integer variables (they can only hold integer numbers).
     38 * The program then places the number 3 in variable a, the number 4 in variable b, and the product of whatever is in a and b into variable c.
     39 * Finally, the program prints whatever value is in variable c.
    3840
    3941=== Extra Credit ===
    40 1. Change the program: adding more variables, change numbers and formulas and run it again.
    41 2. Read more about Java variables [https://www.w3schools.com/java/java_variables.asp here]
     42 1. Change the program: adding more variables, change numbers and formulas and run it again.
     43 1. Read more about Java variables [https://www.w3schools.com/java/java_variables.asp here]