By Tony Bevis on Saturday, 16 September 2023
Category: Java

Java programming course: 1.7 Creating a simple Java program

This is Lesson 1.7 of the Java programming course.

Creating a simple Java program

 In this course you will learn the major facilities of the Java language through the gradual development of a "virtual zoo". This will entail developing classes that model different types of animals, zookeepers, visitors, etc. and will incorporate a graphical user interface that allows you to simulate being the zoo's administrator.

The project will be developed in a step-by-step manner, in small incremental phases, where each phase builds on and extends the phases which came before. By the end of the course, you will have a good working knowledge of the major facilities of the Java programming language.

Once NetBeans has been installed, start it and you should see the following screen:

Uncheck the Show on Startup checkbox and then choose Help | Check for Updates, following the prompts to download and install any updates that may exist. Depending upon which updates were needed you may be prompted to restart the IDE.

Select File | New Project... after which the following dialog will appear:

Ensure that Java with Ant is highlighted in the Categories list and that Java Application is highlighted in the Projects list, then click the Next > button.

The Java with Maven and Java with Gradle categories enable Java applications to be built using the Maven and Gradle build tools respectively. This is beyond the scope of this course.

 The application that you will develop in this course will model a virtual zoo, so in the New Java Application dialog overtype the Project Name field so that it states VirtualZoo, ensure that the Create Main Class and Set as Main Project checkboxes are both checked. You can leave the Project Location entry to its default value unless you particularly wish to change it.

When you are ready, click Finish. The IDE should now look like this:  

The window in the top left position shows three tabs; Projects, Files and Services, where the Projects tab is active. Within each of the tabs is an expandable tree, and each click-able element within the tree is known as a node:

[1] While it is not mandatory to place your Java source files in packages it is strongly recommended that you do so, since it makes your application more manageable as it becomes more complex.

​The larger window in the central location contains the source code for VirtualZoo (i.e., the VirtualZoo.java file as listed under the virtualzoo package node). This contains Java code generated by NetBeans as a starting point for your application:

Blocks of code exist between an opening and closing brace, like this:

a Java block statement {
        other Java statements go here
}

Some programmers prefer this format with the opening brace on its own line:

a Java block statement
{
        other Java statements go here
}

This course will use the first format with the opening brace on the same line as its preceding statement.

You can therefore see that for the VirtualZoo class the block is in this format:

Inside the class block is a second block that looks like this:

Note the following:

For reasons of brevity this course will often not show the multi-line comments that exist within a source file, since the course's narrative will contain a more detailed explanation. Without the multi-line comments (which you can delete if you wish) the Java source looks like this:

​The public static void main(String[] args) block is said to be nested inside the public class VirtualZoo block. Indentation has been used to aid readability and is strongly recommended.

To see the application do something, change the comment line above to the following, as marked in bold:

The System.out.println("Hello World"); statement sends the text you specify (i.e., "Hello World") to the Output window. To get it to run, you can use any of the following options:

If you receive error notifications when entering the above or trying to run it, check that you have entered the statement exactly as specified – common mistakes include not using the correct capitalisation or forgetting to include a semi-colon at the end of the statement.

Whichever method you choose run it, NetBeans automatically compiles the source code for you, and you should see the output appear in the bottom section of NetBeans known as the Output window:

Later, you will modify the code so that instead of it sending "Hello World" to the Output window it launches the application you will be developing in this course.

In the next lesson you will see how to define a Java class to model an animal.

Lesson 1.8 Example Java class to model an animal

Related Posts

Leave Comments