Next page | Contents page |

Let's make a program

We want to write a simple program called Squarer, to calculate the square of a number.

It will be run by the command java Squarer 5 (as an example, in which 5 is the number to be squared).

It will output its result as 5 squared is 25

It will work with any input whole number up to 99999 (5 digits).

Some tips

  1. We have called the program Squarer, as a simple description of what it does. Always name things in a meaningful way like this because it makes it easier for someone coming along later to maintain the program to understand what it is about. Never use abbreviated or cryptic names for things. To begin with we will simply be using a text editor and so long names involve more typing. However, when we start using an Integrated Development Environment (IDE) later you will see that the IDE can save you having to type long names. So do get into the habit of using meaningful names, even if they are long.

    Notice also that Squarer starts with a capital letter but the rest is lower case. This is the normal convention in Java - everyone expects a program (and any class name, as we will see later) to be written this way, beginning with a capital.

  2. Get into the habit of always writing a specification out in words like this before you start to code. This specification, or requirements list, should then be agreed with your (non-technical) customer before you waste time writing something that is not really needed, or is plain wrong.

    The specification also gives you something to test against after coding: does the program do exactly what it was required to do? It is important that that can be demonstrated to the customer (or end-user) in user acceptance testing (UAT).

    It has to be admitted that writing requirements is one of the most difficult parts of programming because the words must be understandable unambiguously by the customer but also be capable of being translated into Java (or whatever programming language) without errors of interpretation. There are tools that attempt to help with this. For example UML (Unified Modelling Language) which we will encounter again later in the course.

Next page | Contents page |