Programming Style and Simple "Hello World"program in java

Programming Style and Documentation

  • Appropriate Comments
  • Naming Conventions
  • Proper Indentation and Spacing Lines
  • Block Styles

Appropriate Comments

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses.
Include your name, class section, instruction, date, and a brief description at the beginning of the program.

Naming Conventions

  •    Choose meaning and descriptive names.
  •    Variables and method names:
    • Use lowercase. If the name consists of several                       words, concatenate all in one, use lowercase for the first  word, and capitalize the first letter of   each  subsequent word in the name.
    • For example, the variables radius and area, and the method compute Area.

Naming Conventions, cont.

  • Class names:
    • Capitalize the first letter of each word in the name. For example, the class name ComputeArea.
  • Constants:
    • Capitalize all letters in constants. For example, the constant PI

Proper Indentation and Spacing

  •  Indentation
    • Indent two spaces.
  •  Spacing
    • Use blank line to separate segments of the code.

Programming Errors

  • Syntax Errors
    • Detected by the compiler
  • Run time Error
    • Causes the program to abort
  • Logic Errors
    • Produces incorrect result

Creating Your First Application

Your first application, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will: 
  • Create a source file
    A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
  • Compile the source file into a .class file
    The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.
  • Run the program
    The Java application launcher tool (java) uses the Java virtual machine to run your application.

Create a source file

To create a source file, you have two options:
  • You can save the file HelloWorldApp.java on your computer and avoid a lot of typing. Then, you can go straight to Compile the Source File into a .class File.
  • Or, you can use the following (longer) instructions.
First, start your editor. You can launch the Notepad editor from the Start menu by selectingPrograms > Accessories > Notepad. In a new document, type in the following code:


import java.util.*;
public class Welcome
{
  public static void main(String[ ] args){
    System.out.println("Hello World");
   }
}

Save the code in a file with the name HelloWorldApp.java. To do this in Notepad, first choose theFile > Save As menu item. Then, in the Save As dialog box:
  1. Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is java on the C drive.
  2. In the File name text field, type "HelloWorldApp.java", including the quotation marks.
  3. From the Save as type combo box, choose Text Documents (*.txt).
  4. In the Encoding combo box, leave the encoding as ANSI.

Compiling Programs

  • On command line
    • Bring up a shell, or "command," window. You can do this from the Start menu by choosing Command Prompt (Windows XP), or by choosing Run... and then entering cmd. The shell window should look similar to the following figure.The prompt shows your current directory. When you bring up the prompt, your current directory is usually your home directory for Windows XP (as shown in the preceding figure.To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is java on the C drive, type the following command at the prompt and press Enter:
      cd C:\java
      Now the prompt should change to C:\java>.
  •  Compile:
    • javac HelloWorld.java
  •  Run:
    • java HelloWorld
    • Output:
           The program prints Hello World! to the screen.

No comments:

Post a Comment