Requirements: using methods, functions, procedures, actions, conditions and control structures effectively, checking input data for validity, input from a user, user-defined methods, functions or procedures.
On completion of this page you will know how to develop a program that complies with the heuristics of good human computer interface, as well as being well structured and commented to comply with the NCEA requirements sepcified above.
In the previous pages we looked at simple programs whose sole aim was demonstrating how to add features to a program to enhance human computer interface. On this page we have restructured this program into individual functions, which are then called from main().
Of those functions we shall be paying attention to the first two. Here we have created two generic validating functions: one for validating floating point numbers and another for validating integers. Although based on the validating functions from the previous examples, we have here introduced formatted strings. Thus we shall be having a quick look at those before examining the restructured program.
Formatted strings are somewhat similar to the mail merge facility provided by word processors.
Formatted Strings
The code above could be the first stage of creating an automated menu for a restaurant or cafe.
In the first three lines it defines three variables which it names meal, menu1 and menu2. To each fo those it gives the values "Breakfast", "Muesli and yoghurt" and "Bacon and eggs". Nothing new here!
Line 4 however is a bit of a cruncher. It consists of two parts: building up the string and matching the fields of the string to the variables declared in lines 1 to 3.
Below is the first part
("Today's {meal} options are: {menu1} or {menu2}")
At first glance this looks like an English sentence except for the three items between curly brackets. Those three items are named meal, menu1 and menu2. This means that they will connect to the three variables declared in lines 1 to 3. This connection happens in the second part which is shown below.
format(meal=meal,menu1=menu1,menu2=menu2)
Here it is matching the {meal} placeholder in the string with the variable meal declared at line 1. This happens to contain the value "Breakfast". Thus when printed out the string cafeMenu would start off as "Today's Breakfast options are:..."
What we have said about meal applies also to the other two variables menu1 and menu2 as well as their placeholder counterparts
Thus the entire line would be "Today's Breakfast options are: Muesli and yoghurt or Bacon and eggs". This is confirmed when we run the program, as shown below.
The example in Listing 1 should be simple and easy to follow. With no distractions, it focuses on how to incorporate external variables into a string in order to produce a complete English sentence.
It is, however, very inefficient and would have to be almost entirely rewritten if we wanted a different output
Below we have a structured version of it that can easily produce completely different menus.
This version contains two functions: the user defined function makeMenu() and the default function main()
The function makeMenu()has been designed to be as similar to the code in Listing 1 as possible. Its three arguments, meal, menu1 and menu2 are taken directly from Listing 1.
At line 2 the string is formatted in exactly the same way as we did before.
The code in main(), however, shows us that the new version is more versatile than its predecessor. It consists of three calls to makeMenu(), each time with different menus. The output can be seen in Fig 2 below.
As well as the function main() this program has eight other user defined functions. Except for the first two they should be familiar to you by now. Therefore we shall concentrate on the functions validateFloat() and validateInt()
Unlike our previous menu generator the function validateFloat() is meant to produce prompts that will give a user clear unambiguous instructions about the data they they are to enter into a computer.
The function is passed three arguments: a description of the data item, the smallest value allowed and the largest value allowed. The argument names are descr, smallest and largest The template string is shown below.
("Enter value for {descr}. The value must be greater than {smallest} and less than {largest}")
The values of the three arguments passed to the function validateFloat() will be passed into the the three similarly named placeholders by the format function shown below.
This works exactly like the example in Listing 2 above.
The structure of the function validateInt() is structured like validateFloat(). The only difference is that one deals with floating point numbers and the other deals with integers.
Working through this page we looked at formatted strings and building prompts and error message that would guide users of a computer application towards entering valid data into the same application. Although this topic was covered in the previous two pages, the programs were written solely for the purpose of demonstrating how to build a good human computer interface. In this page we looked at building the same human computer interface, but this time using properly structured programs.