Prev Main Next

Building an Elementary Java Progam

Building a computer program is like building any other large thing. Imagine you are building a skyscraper. You will start off with very simple things, such a girders, bolts, pipes, etc. Then you might put some of the girders together to make modular section of a wall or a floor. Several modular sections attached together make a complete wall or floor. With a crane, you lift the modules up and attach them together, building larger and larger pieces.

Java gives you a few basic things for writing programs, such as variables, assignment statements, if-else statements and while loops. To build a program, you put the pieces together to form modular units, which you can use to build larger and larger modular uints. Java provides two different kinds of modular units.

  1. A method is made by putting statements together to make an algorithm, a way of solving a problem. You give the method a name, and refer to it by its name.

  2. You put a group of related methods and also some variables that hold information for the methods to use into another kind of modular unit that is called a class.

Java requires all methods to be put into classes. The simplest Java programs have just one class.

One of your classes must have a method that is called main. This is where the program starts when you run it. You can have many methods, written in any order. Regardless of how you write them, the computer will always start by running the method called main. It will not run any other methods unless they are invoked by main, or by a methods that is invoked by main, etc.


Public and private annotations

Think of an egg. From the outside, all you see is the shell, and the egg looks very simple. But inside, the egg is remarkable complex.

Classes and methods are similar. From the outside, all you see of a method is what it does. If a method computes the square root of a number, then that is all you care about from the outside. It might be very complex inside. If a method is draws a picture on a screen, then you just use it to draw a picture. You do not really care that it is actually very involved to draw a picture on a computer monitor.

When you make a class, you can say that some of its parts are on the outside, like the shell of an egg, and so can be seen by anyone who uses the class. The things on the outside are called public components. But you can also indicate that some are on the inside, and cannot be seen by anyone who uses the class. They are called private things.

For now, the simplest thing to do is to make everything in a class public. Later, it will be important to hide some components of a class inside the class, making them private.


A very simple class

The simplest Java program that you can write has just one class and one method, called main, in that class. You create a file whose name (minus .java at the end) is the same as the name of the class that is written in the file. For example, you might create file Buffalo.java, and call your class Buffalo.

  import java.io.*;

  public class Buffalo
  {
    public static void main(String[] args)
    {
      System.out.println("I am the buffalo.");
    }
  }

Inside the main method you can write any statements. They will be performed when your program is run. For example, you can write file Dog.java.

  import java.io.*;

  public class Dog
  {
    public static void main(String[] args)
    {
      System.out.println("I am a dog");
      System.out.println("I like to chase my tail ");
      int k;
      for(k = 1; k <= 10; k++) 
      {
        System.out.print("around and ");
      }
      System.out.println("around.");
    }
  }

Note. Make the heading for the main method exactly as it is in the examples.


Prev Main Next