Understanding the Programming Process A computer system can be divided into two major components: its hardware and its software. Hardware is the physical equipment of the computer system. Computer programs, the software of the computer system, are the instruction sets that tell the computer what to do. A computer program is a set of structured, ordered, explicit instructions to a computer, written in a programming language, to solve a particular problem. All computer programs are written in a programming language (source code) and translated into machine language (object code). Object code is the language the computer executes.
So how does a programmer begin the programming process?
1. Understand the problem
Usually programmers write computer programs to provide solutions for others. First and foremost, the programmer must understand the problem he/she is trying to solve. All aspects of the problem description must be clarified and completely understood at this stage. So let’s take an example and analyze it:
Problem Description: Write a program that takes a dog’s age and converts it into the equivalent age in human years. The user (a dog owner) will enter his/her dog’s age in years and the program will calculate the equivalent age in human years. The program will display the age in human years.
At this point in the process, the programmer insures that he/she understands what the program is expected to do. If the user’s problem specification is vague, the programmer must seek clarification before proceeding. In this example, a dog’s age will be entered. This age is multiplied by 7 to convert it to its equivalent human age, and then the human age will be displayed.
Plan the logic
Once the programmer understands the problem he/she is expected to solve, it is time to plan the logic of the program. During this phase of the process, the programmer uses tools to layout the program’s logical steps from beginning to end. The programmer determines the variables and constants needed in the program. Also, the programmer uses planning tools like IPO Charts, Flowcharts, and Pseudocode to depict the steps involved in his/her programming solution.
All the work being done by a program happens in memory (main memory, primary storage, or RAM). In order to accomplish this work, a program needs locations in memory to temporarily hold data. A programmer defines and uses variables (named locations in memory) to serve this purpose. A variable’s content (or value) can vary during program execution. A programmer also defines constants (named locations in memory where the contents do not change during program execution). These memory locations hold specific data types (i.e., numbers or characters). A programmer names these variables. A variable’s name should be meaningful. In Visual Basic, the programming language used in this class, a variable name must be one word with no spaces, must begin with a letter, and should use “camel casing format” (each new word within the variable name begins with an uppercase letter).
Let’s revisit our example:
Problem Description: Write a program that takes a dog’s age and converts it into the equivalent age in human years. A dog owner will enter his/her dog’s age in years and the program will calculate the equivalent age in human years. The program displays the age in human years.
What variables are needed for this problem? That is, what values could be different, or vary, every time the program runs? We need a variable to hold the dog’s age and one to hold the human age. We also need a constant to hold the conversion factor.
AgeConverter = 7 When writing a program, three basic instruction types are employed. Input instructions are designated by keywords like GET or READ. Process instructions include computations like addition, subtraction, multiplication, and division. Process instructions can be conditional (IF logic). Output instructions are designated by keywords like PRINT or DISPLAY or WRITE.
A programmer uses an Input/Processing/Output (IPO) Chartto plan his/her logic. This 3-column chart lists inputs, outputs, and main steps (in the order preformed) to solve the problem. Let’s create the IPO Chart for our example:
The IPO Chart allows the programmer to divide his/her work into what is needed from the program user (Input), what steps must be done in the program to solve the problem (Process), and what must produced for the user (Output). Note: an IPO Chart lists WHAT must be done, but it does not list the specifics of how it will be accomplished.
Once the programmer has an IPO Chart, he/she can use Pseudocode (“False Code”) to represent in English-like statements his/her problem-solving logic. Pseudocode does not follow the syntax (rules) of any particular programming language. Its purpose is to determine the sequence of events that will lead the input(s) to the desired output(s). Here’s Pseudocode for our example:
AgeConverter = 7
HumanAge = DogAge * AgeConverter
Note: Pseudocode shows in detail how HumanAge will be calculated.
Code the program (Source Code)
Once the programmer has planned the program’s logic, he/she writes the program instructions using the specific syntax of a programming language. In this class, Visual Basic (VB) is the programming language that will be used.
VB is a programming language used to create a custom application. Its Graphical User Interface (GUI) and object-oriented programming design allow for Rapid Application Development (RAD). VB supports event-driven programming. The programmer works with objects (forms, buttons, labels, etc.). These objects have properties (i.e., color, caption, font, etc.) that can be manipulated. Also the objects handle events (i.e., click, change, etc.) and support methods.
Every program created in this class will have only one project with only one form. Remember, the project and the form are separate entities and must be saved individually. Also, in this class only three controls will be employed: Label, TextBox, and CommandButton.
A TextBox is an object that allows INPUT of text from a user. A Label DISPLAYS text on a Form. It is an object used to display READ ONLY text (i.e., descriptions, instructions, program output etc.). The CommandButton is an object that ACCEPTS user commands. Clicking on a command button often triggers program code to run.
In VB, a programmer declares constants and variables prior to their use. Variable declarations are denoted by DIM and constant declarations are denoted by CONST. VB supports many data types. In this class, only two data types will be employed: Integer (represents whole numbers) and Single (represents decimal numbers). In VB, variable and constant names must begin with a letter, must be one word (contiguous set of characters, NO spaces!!), must be 40 characters or less in length, and cannot use any VB Reserved Words (If, Then, Else, Print, etc.).
Programming languages employ assignment statements. Though these statements resemble a mathematical equation, they are quite different. The part of the statement to the right of the equal sign is evaluated first. Its “outcome” is then assigned to what appears to the left of the equal sign.
The VB syntax used to assign a value to an object’s property is:
object_name.property_name = value The VB syntax used to assign a value to a variable is:
variable_name = value To perform mathematical calculations in VB, the following operators are used:
VB uses Functions, built-in procedures that are not attached to an object and that return something. In this class, we will use the Val Function and the Format Function. All data entered into a TextBox is stored as character data even if it appears numerical. To be used mathematically, numerical entries must be converted to numeric form using the Val Function. The Format Function sets the display format for an object (i.e., displays a dollar sign, rounds to a whole number, etc.).
Let’s return to our example and code the program. The first step is to create the user interface. The programmer creates one form within a project that would resemble the following:
This VB form has 3 Labels, 1 TextBox, and 3 CommandButtons. Remember: save the form and save the project.
In VB, the programmer writes code to handle events. The Event-handler is a block of code that runs every time an event occurs. Now the programmer, using the pseudocode, writes code in VB syntax to handle the specific events that occur. In our example, we want to respond to a user’s click on the calculate button, a user’s click on the clear button, a user’s click on the quit button, and a change event on the TextBox. The code written for our example follows:
Private Sub cmdCalculate_Click()
Dim DogAge As Integer
Dim HumanAge As Integer
Const AgeConverter As Integer = 7
DogAge = Val(txtDogAge.Text)
HumanAge = DogAge * AgeConverter
lblAnswer.Caption = Format(HumanAge, "##0")
Private Sub cmdClear_Click()
txtDogAge.Text = ""
lblAnswer.Caption = ""
cmdClear.Enabled = False
cmdCalculate.Enabled = False
Private Sub cmdQuit_Click()
Private Sub txtDogAge_Change()
cmdClear.Enabled = True
cmdCalculate.Enabled = True
Translate the program into machine language (Source Code into Object Code)
Remember the computer does not understand source code. It only understands machine language (object code). The programmer uses a compiler or an interpreter to convert the source code into object code. This process checks for syntax errors (VB rule violations) only. It cannot catch errors in programmer logic. To compile a program in VB, the programmer creates an executable program (EXE). Once converted into this format, the EXE can run outside the VB environment.
Test and debug the program
After a program is written and compiled, it must be tested and debugged. The programmer runs the program using test data to insure the results outputted by the program are accurate. If the programmer finds a bug (error in the program), he/she should fix the bug and repeat the testing process.
Document the program
Before a program is considered complete and ready for production, the programmer must provide documentation of the program’s development. This documentation explains the design of the program. Such documentation makes program maintenance easier and more economical. In this class, a print out of your IPO Chart, Pseudocode, and Source Code serve as your program’s documentation.