Java Example: Algorithm & Program

Java Example: Algorithm & Program
Page content

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.

Write the program for the algorithm

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

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.


Java Programming – A Beginner’s Course

Related programming references

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

This post is part of the series: Introduction to Java Programming

This is a series which aims to discuss and teach Java programming to the complete beginner. Absolutely no programming background is assumed.

  1. Introduction to Java Programming - An Overview
  2. Java - Data, Variable and Algorithm Explained To A Beginner
  3. Java Example: Algorithm and Program For Area of Square
  4. Java Programming For Beginners - Test, Debug, Document, Maintain
  5. JDK Java Compiler: The Java Development Kit
  6. Java Programming For Beginners - How To Compile And Run Java Programs
  7. Data Types, Constants And Variables
  8. Java Programming For Beginners - Characters and printf
  9. Java Programming For Beginners - Part 9
  10. Java Programming For Beginners - Part 10
  11. Java Programming For Beginners - Part 11
  12. Java Programming For Beginners - Part 12
  13. Java Programming For Beginners - Part 13
  14. Java Programming For Beginners - Part 14
  15. Java Programming For Beginners - Integer Data Types
  16. Java Programming for Beginners - Part 16
  17. Java Integer Arithmetic For Beginners
  18. Java Programming For Beginners - Part 18
  19. Java Programming For Beginners - Part 19
  20. Java double to int and Other Conversions