Pin Me

Java Example: Algorithm and Program For Area of Square

written by: Noel Kalicharan•edited by: Bill Fulks•updated: 12/27/2008

In this article, we get our first glimpse of Java code when we show you how to write a program from an algorithm. As our example, we write a program to calculate the area of a square. We introduce the technical terms class, syntax, declaration, input, output and comments.

  • slide 1 of 3

    In Java: Data, Variable, Algorithm we wrote the algorithm for finding the area of a square. We now show how to write the program in Java.

  • slide 2 of 3

    Write the program for the algorithm

    We have specified the example algorithm using English statements. (Click image to enlarge)

    Java Example Algorithm 

    However, these statements are sufficiently ‘computer-oriented’ for a computer program to be written directly from them. Before we do this, let us see how we expect the program to work from the user’s point of view.

    First, the program will type the request for the length of a side; we say the program prompts the user to supply data. The screen display might look like this:

    Enter length of side:

    The computer will then wait for the user to type the length. Suppose the user types 12. The display will look like this:

    Enter length of side: 12

    The program will then accept (we say read) the number typed, calculate the area and print the result. The display may look like this:

    Enter length of side: 12

    Area of square is 144

    Here we have specified what the output of the program should look like. For instance, there is a blank line between the prompt line and the line that gives the answer; we have also specified the exact form of the answer. This is a simple example of output design. This is necessary since the programmer cannot write the program unless he knows the precise output required.

    In order to write the computer program from the algorithm, a suitable programming language must be chosen. We can think of a program as a set of instructions, written in a programming language, which, when executed, will produce a solution to a given problem or perform some specified task.

    The major difference between an algorithm and a program is that an algorithm can be written using informal language without having to follow any special rules (though some conventions are usually followed) whereas a program is written in a programming language and must follow all the rules (the syntax rules) of the language. (Similarly, if we wish to write correct English, we must follow the syntax rules of the English language.)

    In this series, we will be showing you how to write programs in Java, the programming language developed by Sun Microsystems in the 1990s, and one of the most popular and widely used today.

    Pursuing our example, the Java program for the algorithm which requests the user to enter the length of a side and prints the area of the square is shown here (click image to enlarge):

    Java Example Algorithm - Area of Square 

    It is not too important that you understand anything about this program at this time. But you can observe that a Java program consists of something called a class (named AreaOfSquare here) which contains something (a method) called main. After the name of the class comes a left brace { which indicates the beginning of the class; a matching right brace } (the last one) ends the class. Similarly, there is a left brace to begin main and a matching right brace to end it.

    The statement

    int a, s;

    is called a declaration. The parts after // are comments which help to explain the program to a human being but have no effect when the program is run. And * is used to denote multiplication.

    Note also that Java uses to refer to the standard input (usually the keyboard) and System.out to refer to the standard output (usually the monitor/screen). In this program, the user will be expected to type the length of the side on the keyboard and the computer will print the result on the monitor (screen).

    All of these terms will be explained in detail in due course.

    Finally, a program written in a high-level language is usually referred to as a source program or source code.

    In the next article, Java Programmming: Test, Debug, Document, Maintain, we continue to explain the other stages of the programming process.

  • slide 3 of 3


    Java Programming – A Beginner’s Course

    Related programming references

    Article: Java Example: Algorithm & Program For Area of Square