Data and Variables in Java
Computer programs, including Java programs, are written to operate on data. For example:
• the data for an action game might be keys pressed or the position of the cursor when the mouse is clicked;
• the data for a word processing program are the keys pressed while you are typing a letter;
• the data for an accounting program would include, among other things, expenses and income;
• the data for a program that teaches Spanish could be an English word that you type in response to a question.
For a program to be run, it must be stored in the computer's memory. When data is supplied to a program, that data is also stored in memory. Thus we think of memory as a place for holding programs and data. One of the nice things about programming in a high-level language (like C or Java) is that you don’t have to worry about which memory locations are used to store your data. But how do we refer to an item of data, given that there may be many data items in memory? We use a variable. In Java, data and variable go hand in hand.
Think of memory as a set of boxes (or storage locations). Each box can hold one item of data, for example, one number. We can give a name to a box, and we will be able to refer to that box by the given name. In our example, we will need two boxes, one to hold the side of the square and one to hold the area. We will call these boxes s and a, respectively.
If we wish, we can change the value in a box at any time; since the values can vary, s and a are called variable names, or simply variables. Thus a variable is a name associated with a particular memory location or, if you wish, it is a label for the memory location. We can speak of giving a variable a value, or setting a variable to a specific value, 1, say. Important points to remember are:
• a box can hold only one value at a time; if we put in a new value, the old one is lost;
• we must not assume that a box contains any value unless we specifically store a value in the box. In particular, we must not assume that the box contains 0.
Variables are a common feature of computer programs. It is very difficult to imagine what programming would be like without them. In everyday life, we often use variables. For example, we speak of an ‘address’. Here, ‘address’ is a variable whose value depends on the person under consideration. Other common variables are telephone number, name of school, subject, size of population, type of car, television model, etc. (What are some possible values of these variables?)
Now that we know a little bit about variables, we are ready to develop the algorithm for calculating the area of a square.
Develop the algorithm (continued)
Using the notion of an algorithm and the concept of a variable, we develop the following algorithm for calculating the area of a square, given one side (click the image to enlarge):
Algorithm for calculating area of square, given one side
(1) Ask the user for the length of a side
(2) Store the value in the box s
(3) Calculate the area of the square (s x s)
(4) Store the area in the box a
(5) Print the value in box a, appropriately labelled
When an algorithm is developed, it must be checked to make sure that it is doing its intended job correctly. We can test an algorithm by ‘playing computer’, that is, we execute the instructions by hand, using appropriate data values. This process is called dry running or desk checking the algorithm. It is used to pinpoint any errors in logic before the computer program is actually written. We should never start to write programming code unless we are confident that the algorithm is correct. Here, the algorithm is fairly simple, so it is easy to check that it is indeed correct.
In the next article, Java Example: Algorithm and Program For Area Of Square, we show how to write the program to implement this algorithm.