Version 9 (modified by 6 years ago) (diff) | ,
---|
Introduction to Java
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, create a new folder in your Documents folder named Java Projects. In the Java Projects folder, create another new folder named Hello World.
In VSCode:
- Select File->Open Folder and choose the Hello World folder.
- Select File->New which will open the editor for you to enter a new program
- Cut and paste the following program into the editor:
public class Hello { public static void main(String args[]) { System.out.println("Hello world"); } }
- Select File->Save and name the file Hello.java
- 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
- A terminal window will open, the program will be compiled and then run; you'll see the output: Hello world!
Variables
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.
- Close the Hello World folder (File->Close Folder)
- Make a new folder in your Java Projects folder named VariablesExample
- Open the folder (File->Open Folder)
- Create a new file: (File->New)
- Cut and paste the program below into the editor window:
public class VariablesExample { public static void main(String args[]) { int a; // create a variable that can hold an integer number int b; // create another integer variable named b int c; a = 3; b = 4; c = a * b; System.out.println(c); } }
- Save the file as VariablesExample.java (File->Save)
- Run the program: right click on VariablesExample.java in the left panel and select Run
- In the output terminal window, observe that the number 12 is output.
- Change the program, adding more variables, changing numbers and the formula and run it again.
Debugger
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.
- Click Debug->Start Debugging
- The program will start running but will stop (break) at line 7
- Click on the Step-over icon
above the editor (or press function key F10) to execute that line of code and then stop again
- Observe the Variables window in the left navigation panel and note that there is now a variable named 'a' with the value 3
- Press F10 again to execute the line b=4 and observe the variable appearing in the Variables window
- Double click on the variable b in the Variables window and change its value to 5
- Press F10 again to execute the line c=a*b and observe the variable c appearing in the Variables window with the value 15.
- Press the Continue tool
(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.
Conditionals
Programs 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:
public class ConditionalExample { public static void main(String args[]) { int rangeInInches; rangeInInches = 10; if (rangeInInches < 20) { System.out.println("stop motors so we don't crash!"); } else { System.out.println("full speed ahead!"); } } }
- Save the program as ConditionalExample?.java (notice how the name of the file must match the name of the class)
- Run the program using the debugger with a breakpoint set on the first executable line (rangeInInches=10); step through the program using the stepover tool and observe the flow of the program.
- What happens if you change the values stored in the variable rangeInInches to 25?
Loops
It's also important for a program to be able to do things repeatedly; for example, the robot must read its sensors repeatedly and react only when certain conditions are met. Consider (and try running) the following program:
public class LoopExample { public static void main(String args[]) { int counter; counter = 1; while (counter <= 10) { System.out.println(counter); counter = counter + 1; } } }
- Save this program as Loop Example
- Run the program in the debugger with a breakpoint set at the line while (counter <= 10)
- Step through the program a line at a time, observing the program output, variables, and flow of execution
Princeton Java
There's a lot to programming and you'll get to the fun stuff a lot faster if you do some self-study. There are many Java tutorials, but if you'd like to try learning the Princeton way, try this one.
Attachments (7)
-
breakpoint.jpg (27.0 KB) - added by 6 years ago.
Breakpoint
-
stopped.jpg (21.4 KB) - added by 6 years ago.
Program stopped at breakpoint
-
stepover.jpg (467 bytes) - added by 6 years ago.
Step Over Tool Icon
-
continue.jpg (568 bytes) - added by 6 years ago.
Continue tool icon
-
variables.jpg (4.3 KB) - added by 6 years ago.
Variables window
-
ProjectFolder.jpg (26.0 KB) - added by 6 years ago.
Java Projects Folder
-
VSC_toolbar.png (4.5 KB) - added by 6 years ago.
VSC Debug Toolbar
Download all attachments as: .zip