2. Main program body
The body for a program named "Example" is as follows:
public class Example {
public static void main(String[] args) {
// program code
}
}
The program is stored in a text file named after the program with the .java extension. For a program named Example, the file should be named Example.java.
The execution of the program begins at the part marked with the // program code comment above. During our first week of programming, we will limit ourselves to this part. When we are talking about commands such as printing, we need to write the commands into the program body. For example: System.out.print("Text to be printed");
public class Example {
public static void main(String[] args) {
System.out.print("Text to be printed");
}
}
From this point on, the main program body will be omitted from the examples.
3. Getting to know your development environment
Programming these days takes place in development environments almost without exceptions. The development environment provides several tools and features to assist the programmer. Although the development environment does not write the program on behalf of the programmer, it contains several handy features such as hinting about mistakes in code and assisting the programmer to visualize the structure of the program.
In this course, we will use the
NetBeans development environment. A guide for using NetBeans is available
here.
Until you become familiar with NetBeans, follow the guides and steps precisely. Most of the following exercises show what needs to be printed to the screen for the program to function correctly.
Note: Do not do the exercises by writing code and then clicking the test button. You should also execute the code manually (green arrow) and observe the result on the screen. This is especially useful if an exercise fails to pass the tests.
In the following exercises, we will practice the use of NetBeans and printing of text on the screen.
Exercise 1: Name
Create a program that prints your name to the screen.
The program output should resemble the following:
Jane Doe
Exercise 2: Hello world! (And all the people of the world)
Create a program that prints out the following:
Hello world!
(And all the people of the world)
Exercise 3: Spruce
Create a program that prints the following:
*
***
*****
*******
*********
*
Note: You probably wrote System.out.println("...") quite a few times. Try typing only
sout on an empty line in NetBeans and then press the
tab key. What happened? This tip will save a lot of your time in the future!