On
completion of this section you will know
Programming
means giving instructions a machine to perform a series of activities. At the
most non-academic and very domestic level, automatic washing machines have
different programmes for washing clothes. Each programme simply controls how
much water to use, what the water temperature is, what washing cycles to perform
and what length each cycle is.
In
programming a computer we essentially do the same thing, that is, we give the
computer a series of instructions to do certain activities. Those activities
will, however, be somewhat more complex than those performed by the washing
machine.
We shall
select a simple payroll application as an example for our first programme. At
its simplest level a payroll requires two pieces of information – the hours
worked and the hourly rate. Once those are known the hours worked is multiplied
by the hourly rate, giving us the gross. From the gross the tax is calculated
and once the tax is known the nett can be calculated
by subtracting the tax from the gross.
Below is
the code for our programme.
Listing 1.1
|
1 |
public class Untitled1 |
|
2 |
{ |
|
3 |
public static void
main(String[] args) |
|
4 |
{ |
|
5 |
double hours,rate,gross,tax,nett; |
|
6 |
hours = 30; |
|
7 |
rate = 10; |
|
8 |
gross = hours * rate; |
|
9 |
tax = gross * .25; |
|
10 |
nett
= gross - tax; |
|
11 |
System.out.println("Hours
" + hours); |
|
12 |
System.out.println("Rate
" + rate); |
|
13 |
System.out.println("Gross
" + gross); |
|
14 |
System.out.println("Tax
" + tax); |
|
15 |
System.out.println("Nett " + nett); |
|
16 |
} |
|
17 |
} |
Java is an
object oriented language and thus all of our programme code must reside inside
a class. Thus in line 1 of our code we have a class called Untitled1. In line 2 a left facing curly bracket indicates the
start of the class area while in line 17 the right facing curly bracket
indicates the end of the class area. All
code belonging to that class must reside between those two curly brackets. This
code is in lines 3 – 16.
Our class,
in this example, is made up of one method, which is defined in line 3 as:
public static void
main(String[] args)
For the
moment we shall not explain the words public,
static or void. Let us for the moment assume that they are required. A full
explanation of them will be given later on in the course. The same goes for String[]
args. We are thus left with main
which is the name of our method.
The left
and right facing curly brackets in lines 4 and 16 indicate the beginning and
end of the body of the method main.
The code for the method starts at line 5
The first
word on line 5 – double – stands for
double precision real number, in
other words a number with decimal places. Following it are the familiar words: hours, rate, gross, tax and nett. In this case those words are referred to as programme
variables.
Let us
spend some time on programme variables. They are logically similar to algebraic
variables such as x, y, z. A very simple way of using algebraic variables is a
follows:
If x = 2
and y = 5, then if z = 2x + 3y, what is the value of z?
Substituting
2 and 5 for x and y we get
2(2) + 3(5)
= 4 + 15 = 19.
Thus z =
19.
In our
programming application we need to have values for the hourly rate, the rate
per hour, the gross pay, the amount of income tax and the nett
pay. In order to hold those values in memory a programme uses variables. The
only difference between algebraic variables and programme variables is that
whereas names of algebraic variables are confined to one letter only such as x,
y and z being the most common ones which are followed by a, b and c. Although
we could use single letters for programme variables as well, in almost all
cases we use full words. Those words can then be used to fully describe the
type of data that each variable holds. Thus in our example the variables hours,
rate, gross, tax and nett fully describe the
components of the payroll application that they will be storing. Thus, getting
back to line 5, we are telling the system to create 5 variables of data type double – in other words variables that
will be able to hold decimal values – and to call those variables hours, rate, gross, tax and nett.
With our
variables set up we can now proceed to do the actual calculation of the
payroll. To do this in real life we must first know the value of the hours and
rate and thus our first action is to put values into those variables.
At line 6
we have
hours = 30;
This simply
tells the system to store the value 30 in the variable hours. Line 7 does a similar job – it stores the value 10 in the
variable rate.
At this
stage no calculations have been performed, we have simply given the system the
essential data it needs to do its processing. This stage is referred to as the input stage.
Now that we
have our essential data we can commence the calculation of the rest of the payroll,
beginning with the calculation of the gross at line 8. Here
gross = hours
* rate;
means multiply
the values stored in the variables hours
and rate and store the result in the
variable gross. The result of this would cause the value 300
to be stored in gross.
Similarly
line 9 is equivalent to multiply the value stored in the variable gross by .25 and store the result in
the variable tax, while line 10 is
equivalent to subtract the value stored in the variable tax from the value stored in the variable gross and store the result in the variable nett.
By now we
have managed to calculate the values for the gross pay, the amount of tax and
the nett pay. Since this involved processing the data
received from the hours worked and the rate per hour, this part of the
programme is referred to as the processing
part.
At this
stage the values for the gross pay, the amount of tax and the nett pay are stored in the variables gross, tax, and nett. Those variables are, however, only registers of the computer’s
memory and with our human senses we cannot see those results. For this reason
copies of those values will either have to be printed out, displayed on the
screen or stored in some storage unit where they can be viewed later. In our
case we shall print them out to the screen.
Lines 11 –
15 are involved in printing the data to the screen. Again we shall not explain
until later the sequence of keywords System.out.println, let us assume that, together, they
display data on the screen. In line 11
System.out.println("Hours " + hours);
prints
the value of the variable hours
preceded by a label. Let us look exactly at how it does this. The first part of
the data inside the brackets – “Hours “ – is enclosed
in quotation marks. Generally any text within quotes is displayed exactly as it
appears inside the quotes. The “=” sign following the quotes means add the next item to the printout. The
next item – hours – is simply the
variable of that name and what is meant in this case is to print out its
value. Thus the expected result of
running line 11 would be
|
Hours 30 |
Lines 12 –
15 work in exactly the same manner as line 11. the
total output of the programme would be
Hours 30.0
Rate 10.0
Gross 300.0
Tax 75.0
Nett
225.0
Since this
last section of the programme involved outputting the data to the screen it is
referred to as the output section.
Notice in
line 1 that the first line of the programme, that is the line where the name of
the class is specified in flush against the left hand margin and that the
opening bracket that follows is also flush against the same margin. Earlier we mentioned that the correspoding closing bracket at line 17 is also flush
against the same margin. The name of the class and its corresponding brackets
are the outermost regions of the programme and consequently are always flush
against the left margin.
Once we go
inside the body of the class we notice that everything else is indented to
various degrees. At line 3 the declaration of the method main as well as its
opening and closing brackets at lines 4 and 16 are indented one tab space from
the left margin. This is to indicate
that the method is one level lower than the class itself.
The body of
the method main, which spans lines 5 – 15, is indented by 2 tab spaces, again
indicating that the code is one level below the description of the method main.
The reason
for the indentation is to make the structure of the programme easier for the
viewer to read. Thus looking at Listing 1.1, by aligning the opening and
closing brackets at lines 2 and 17 we notice that the body of the class itself
lies between those two lines. Similarly by glancing at the brackets in lines 4
and 16 we can deduce that the body of the method main lies between those two
lines.
This
indentation of code may look like an overkill at the
moment as the structure of the code is very simple. However as we progress in
the course and our code gets bigger and more complex then the usefulness of the
indentation will become very apparent to us.
It should
be pointed out, however, that Java does not require indentation of its code. In
fact it does not even require the code to be on separate lines. If our code was
like that below Java would not complain, instead it would quite happily compile
and run it!
public
class Untitled1{public static void main(String[] args){ double hours,rate,gross,tax,nett;hours
= 30;rate = 10;gross = hours * rate;tax = gross *
.25;nett = gross - tax;System.out.println
("Hours " + hours);System.out.println("Rate
" + rate);System.out.println ("Gross "
+ gross);System.out.println("Tax " + tax);System.out.println ("Nett
" + nett);}}
believe
it or not the apparent piece of garbage above is in fact the code in Listing
1.1 with all indentation and line breaks removed. No actual code has been
removed and thus it will be perfectly acceptable to Java.
On the
other hand it is quite apparent that if you try to examine the code line by
line it is not as easy to do as it is in Listing 1.1
A computer
programme is a set of instructions given to the computer so that a particular
task can be performed. A programme is made up of variables and code. A programming
variable is logically similar to algebraic variables,
the main difference between them is that the programming variables have longer
names.
Programme
code is divided into three parts: input,
process and output.
Copy the
code from listing 1.1 into your computer and run it. Ensure that it gives
output consistent with the output shown above.
Next experiment with it by altering the value given to hours and rate in
lines 6 and 7, and, the value of the tax rate in line 9. Try out different combinations of
those. In each case once you have made an alteration work out yourself what the
output should be and then check that when you run the programme that the output
will be the same.
1)
What
does programming a computer mean?
2)
Give
two uses for curly bracket pairs in a Java programme
3)
What
is a programming variable?
4)
What
makes a programming variable similar to an algebraic variable?
5)
Give
one difference between a programming variable and an algebraic variable
6)
What
are the three main parts of a computer programme?
7)
Explain
what exactly happens at all three of those parts?
8)
Give
a reason for using indentation in your code.
9)
explain where semi colons should be used in your code
and also where they should not.
A factory
sells lawnmowers to retail outlets. As they manufacture only one make of
lawnmower then all mowers have the same price. For every lawnmower they sell
they charge the purchaser the price of the lawnmower as well as 10% GST.
Write a
programme that, if given the cost of a lawnmower and the amount sold, can work
out the GST for one lawnmower, the cost of the mower including the GST and the
total charged to the purchaser.
Your
programme must have variables for:
The
programme must put values into the variables for price of a single lawnmower
and the number sold.
The
programme will then do the following calculations:
Finally the
programme will print out on the screen the following:
Once your
modifications are complete you must test the programme. For doing this you must prepare test data as
follows:
|
Input Data |
Expected output |
Actual output |
|||||
|
Price of L’mower |
Amount Sold |
GST |
Price inc GST |
Customer Total |
GST |
Price inc GST |
Customer Total |
|
200 |
12 |
20 |
220 |
2640 |
|
|
|
|
350 |
9 |
35 |
385 |
3465 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Two sets of
test data are shown above. Beside them
in the section “Expected output” are the expected values for GST, price
including GST and total charged to customer.
These values must be calculated using either a spreadsheet or a
calculator and entered into the columns under “Expected output”.
Next one
set of the input data is entered into the programme. To do this supposing that the first two lines
of your code after the declaration of the varibles
was:
priceOfMower = 120;
amtSold = 15;
then
those two lines should be changed to:
priceOfMower = 200;
amtSold = 12;
The
programme should then be compiled and the values it prints out for GST, price
including GSt and Customer
total should be entered into the columns under “Actual output”
The values
under “Expected output” and “Actual output” should be the same. If they are not then you should proceed as
follows:
1)
Recheck
your original recalculation of the numbers.
If you have done the calculations on a calculator then user a spreadsheet
and vice versa.
2)
If you
feel that your calculations are correct then recheck the calculations in your
programme and run it a second time.
Once your
“Expected output” and “Actual output” match, then modify your programme again
with the second set of test data and proceed as above.
Now enter
two or three sets of data of your own, calculate the expected outputs for each
set. Next alter your programme so that
each set of data is tested in turn and compare each output with the expected
output for that set of inputs.