Changes between Version 20 and Version 21 of ControlSystems/SoftwareTeam/Training/GettingStarted/IntroJava


Ignore:
Timestamp:
Nov 4, 2019, 3:05:51 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

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

    v20 v21  
    1 == Introduction to Java
    2 Java is a programming language.  It consists of a small, concise set of words that let you describe to the computer exactly what you want it to do.  A Java program consists of one or more files.  We use a Microsoft program called VSCode to write and run Java programs.  Each program must have its own folder.  To write your first Java program, you need to create a new folder for it.  First create a folder to hold all of your Java projects in your Documents folder named Java Projects.  Then in the Java Projects folder, create another new folder named Hello World that will hold your first program.
    3 [[Image(ProjectFolder.jpg, 50%)]]
    4 
    5 
    6 In VSCode:
    7 1. Select File->Open Folder and choose the !JavaProjects\Hello World folder you just created.
    8 2. Select File->New  which will open the editor for you to enter a new program
    9 3. Cut and paste the following program into the editor:
    10 {{{
    11 public class Hello {
    12     public static void main(String args[]) {
    13         System.out.println("Hello world");
    14     }
    15 }
    16 }}}
    17 4. Select File->Save and name the file Hello.java  the name is important; it must be Hello.java with a capital H (case matters to Java).  The name of the file must be the same as the name of the class (more on classes later)
    18 5. In the left navigation window, you'll now see Hello.java with a red J icon next to it; right click on the Hello.java and select Run
    19 6. A terminal window will open, the program will be compiled and then run; you'll see the output: Hello world!
    20 
    21 == Variables
    22 Let's try a slightly more complex program that will introduce variables.  A variable is like a box; you can put only one type of thing in the box, for example an integer number or a character or a string of characters.  Variables are an important part of every program because they allow the program to do different things depending on what's in the box.
    23 
    24 1. Close the Hello World folder (File->Close Folder)
    25 2. Make a new folder in your Java Projects folder named !VariablesExample
    26 3. Open the folder (File->Open Folder)
    27 4. Create a new file: (File->New)
    28 5. Cut and paste the program below into the editor window:
    29 {{{
    30 public class VariablesExample {
    31     public static void main(String args[]) {
    32         int a;  // create a variable that can hold an integer number
    33         int b;  // create another integer variable named b
    34         int c;
    35 
    36         a = 3;
    37         b = 4;
    38         c = a * b;
    39         System.out.println(c);
    40     }
    41 }
    42 }}}
    43 6. Save the file as !VariablesExample.java (File->Save)
    44 7. Run the program: right click on !VariablesExample.java in the left panel and select Run
    45 8. In the output terminal window, observe that the number 12 is output.
    46 9. Change the program, adding more variables, changing numbers and the formula and run it again.
    47 
    48 == Debugger
    49 With the !VariablesExample.java program above open in the editor click to the left of the line number (7) for the line a=3; and a red dot will appear called a breakpoint.
    50 [[Image(breakpoint.jpg, 50%, margin=10)]]
    51 1. Click Debug->Start Debugging
    52 2. The program will start running but will stop (break) at line 7
    53 [[Image(stopped.jpg, 50%, margin=10)]]
    54 3. Click on the Step-over icon [[Image(stepover.jpg)]] above the editor (or press function key F10) to execute that line of code and then stop again
    55 4. Observe the Variables window in the left navigation panel and note that there is now a variable named 'a' with the value 3
    56 [[Image(variables.jpg)]]
    57 5. Press F10 again to execute the line b=4 and observe the variable appearing in the Variables window
    58 6. Double click on the variable b in the Variables window and change its value to 5
    59 7. Press F10 again to execute the line c=a*b and observe the variable c appearing in the Variables window with the value 15.
    60 8. Press the Continue tool [[Image(continue.jpg)]] (or function key F5) above the editor window to allow the program to continue running to the end.  Observe the value printed in the output terminal window.
    61 
    62 The VSCode debug toolbar [[Image(VSC_toolbar.png)]] offers a complete set of tools to control the execution of your program:
    63 * Continue / Pause F5
    64 * Step Over F10
    65 * Step Into F11
    66 * Step Out Shift+F11
    67 * Restart Ctrl+Shift+F5
    68 * Stop Shift+F5
    69 
    70 
    711== Conditionals
    722Programs need to be able to react differently to varying data.  Conditional statements let us do this.  For example, a robot must be able to monitor its sensors to prevent collisions.  Consider (and try running) the following program: