Java Application Structure

Every Java program consists of at least one class. It is impossible to write a Java program that doesn’t use classes. Classes describe a logical entity that has data as well as methods (the instructions) to manipulate that data. An object is a physical instantiation of the class that contains specific data. Here, I provide a shell that contains the basic format of a Java application with a class name of GhostInThe. Your source code should use this format, changing the class name as appropriate.

Screen Shot 2018-12-08 at 10.22.47 PM

Here, the numbers to the left of each line are not part of the program code; they are included here for your convenience. IDEs typically allow you to display line numbers. From application to application, the name of the class, GhostInThe, will change, because you will want to name your class something meaningful that reflects its function. Each Java source code file must have the same name as the class name with a .java extension. In this case, the source file must be GhostInThe.java. Whatever name you select for a class must comply with the Java syntax for identifiers.

Java identifiers are symbolic names that you assign to classes, methods, and data. Identifiers must start with a Java letter and may contain any combination of letters and digits, but no spaces. A Java letter is any character in the range a–z or A–Z, the underscore (_), or the dollar sign ($), as well as many Unicode characters that are used as letters in other languages. Digits are any character between 0 and 9. The length of an identifier is essentially unlimited. Identifier names are case sensitive, so Number1 and number1 are considered to be different identifiers.

In addition, none of Java’s reserved words can be used as identifiers. These reserved words, which are listed here, consist of keywords used in Java instructions, as well as three special data values: true, false, and null. Given that Java identifiers are case sensitive, note that it is legal to use True or TRUE as identifiers, but true is not a legal variable name. The table below lists the rules for creating Java identifiers.

The shell code in above uses three identifiers: GhostInThe, main, and args. The remainder of it consists of comments, Java keywords, and required punctuation. The basic building block of a Java program is the statement. A statement is terminated with a semicolon and can span several lines. Any amount of white space is permitted between identifiers, Java keywords, operands, operators, and literals. White space characters are the space, tab, newline, and carriage return. Liberal use of white space makes your program more readable. It is good programming style to surround identifiers, operands, and operators with spaces and to skip lines between logical sections of the program.

A block, which consists of 0, 1, or more statements, starts with a left curly brace ({) and ends with a right curly brace (}). Blocks are required for class and method definitions and can be used anywhere else in the program that a statement is legal. The example above has two blocks: the class definition (lines 5 through 10) and the main method definition (lines 7 through 9). As you can see, nesting blocks within blocks is perfectly legal. The main block is nested completely within the class definition block.

Screen Shot 2018-12-08 at 10.33.27 PM

Comments document the operation of the program and are notes to yourself and to other programmers who read your code. Comments are not compiled and can be coded in two ways. Block comments can span several lines; they begin with a forward slash-asterisk (/*) and end with an asterisk-forward slash (*/). Everything between the /* and the */ is ignored by the compiler. Note that there are no spaces between the asterisk and forward slash. Lines 1–3 in the above code are block comments and illustrate the good software engineering practice of providing at the beginning of your source code a few comments that identify yourself as the author and briefly describe what the program does. The second way to include comments in your code is to precede the comment with two forward slashes (//). There are no spaces between the forward slashes. The compiler ignores everything from the two forward slashes to the end of the line. In the code above, the compiler ignores all of line 8, but only the part of line 6 after the two forward slashes.

Let’s look at an example to get a sense of what a simple program looks like and to get a feel for how a program operates. Here, we calculate the area of one of the nine circles of hell:

Screen Shot 2018-12-08 at 11.05.57 PM

This is the output:

Screen Shot 2018-12-08 at 11.11.48 PM

If we change the radius to 666, the output is:

Screen Shot 2018-12-08 at 11.13.47 PM

You can see that this Divine Comedy-themed example has the basic elements that we saw in the GhostInThe. I have added some statements in lines 9 to 23 that explain what is going on with the program.

First, we identify the data we will need. To calculate the area of a circle, we use the formula (πr²). We know the practical value of π here (3.14159), so we store that value in a memory location we name PI (line 10). We also need places in memory to hold the radius and the area. We name these locations in lines 13 and 14. In line 17, we give the radius a value; here we have chosen 6.66. Now we’re ready to calculate the area. We want this program to output correct results with any radius, so we need to write the algorithm of the program using the formula for calculating a circle’s area given above. Java provides arithmetic operators for performing calculations. We use Java’s multiplication operator (*) in line 20 to multiply PI times the radius times the radius and store the result into the memory location we named area. Now we’re ready to output the result. On line 23, we write a message that includes the area value we calculated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s