u06.01

P1 - Describe the application and limits of procedural, object-oriented and event driven programming paradigms


P2 - Describe the factors influencing the choice of programming language

P3 - Explain sequence, selection and iteration as used in computer programming

P4 - Outline the benefits of having a variety of data types available to the programmer
------



Main programming Types

There are three main types of programming; procedural, Object-oriented (OOPs) and event driven (EDP).

Procedural programming is usually used in programs which don’t require much user interface, it provides step by step instructions for a computer to follow in the order they are written. The main features are; Pre-defined functions, Local variables, Global variables, Parameter passing, Modularity, programming libraries and the paradigm. Procedural was the first type of programming. An advantage is that it acts as you’d expect, it follows the simple instructions in an order which makes sense. Another advantage is that they can take up very little space and therefore be used on devices with very little storage space. Disadvantages of this language are that there can be a lot of writing for simple programs as it has to be written in very simple steps. It is also very hard (lots of code and time) to create graphical applications.

Object-oriented programming is most often used in large projects/programs as it allows the program to be spit up into lots of sections which can then be assigned to different teams or employees within a company. Its main features are; Encapsulation, inheritance, polymorphism, Objects, Classes and message passing. OOPs is an approach to problem solving which every calculation is carried out using objects. An object is a part of a program that knows how to interact with other components in the program and perform certain actions. You could have a dog as an object in a program, it would have a name and details about it which would be properties in the program, it would also have the actions of the dog such as eating, barking, and running; this would be the method. A class is like a blueprint of an object. An advantage of oops is that it has encapsulation, this means that code/program can be protected from being broken when someone tries to change it without knowing what they are doing. The program will also be easy to maintain as it is split into sections, this means that it can be upgraded one section at a time without breaking or changing the whole program. A disadvantage of OOPs is that it takes up a lot of memory so it isn’t the most suitable when memory is an issue. OOP’s is also slower as they demand a lot of resources and are bigger than other programming types.

Event driven programming runs sections of code called events when they are triggered, some may be running by default and triggered to switch off or on, some example events are keyboard presses, mouse clicks, a temperature and light sensor or even a combination. EDP is used for lots of computer programs with a graphical user interface. There is usually 1 main loop called an event listener which checks for events. An advantage of EDP is that is allows multiple sensors and hardware to interact with the program. EDP can also be easy to program with drag and drop type windows and features, they also look good for users and are interactive. A disadvantage is that it can be hard to follow the flow of the program as there isn’t a defined order. They can also use up more RAM than procedural.

Choosing a language

When choosing a language there are multiple factors which you need to consider.

The size of the project will play a role in choosing what programming language you will choose to build your project. OOPs is the most suitable language type for large projects so you must choose a language which can be coded for OOPs so procedural languages like Pascal can’t be used. For a database system OOPs or EDP would a suitable type so a suitable language would be C# or Java.

Whoever you are creating the program for may have an organizational policy, this may mean that they stick to one language when getting a new program, this may be because some staff know one language so they can then make small changes to parts of the program if needed. They may also have a license or support for a programming language in the case that they have problems with the program.

Different languages have different features and different tools which may be required in your program. In a database program it is important that the program can work with lots of files and connect to a database. This would make EDP or OOPs the most suitable as most databases can be easily connected and data sets setup. It is important to check a languages strengths and weaknesses so you can match languages which have strength in the areas you find most important. If the most important factor for your program is file size then a procedural language would be the most appropriate.

If you have staff trained in a language or one that is very similar then it would be best to use that language if it meets the other factors. Having staff that know a language will save time training them to code it if you are working on a project or if they need to make small changes to the code if they have changed their mind with something small.

It is important that the language you choose is reliable; you know a language is reliable by its history and the support available for it if you need help. You want the program to be sustainable so it must not be some new language created by someone or an organization that is unheard of with little history. If you have used a language in the past and has worked well then you may want to use that language again.

Some languages and programing types are easier to maintain than others so for programs that are used by big companies it is important that it is easy to maintain programs and change them quickly when necessary so there is little disruption. There are also costs to buying development software and licences for support.

OOP is the best type of programming when you need to be able to expand and add things to the program quickly as new sections can be created without losing functionality of old sections. If deeper changes would need to be made then EDP would be better as it doesn’t have encapsulation which hides some code.

Overall I think C# would be the most appropriate choice of language for a program to access database systems through a graphical user interface.

Sequence, Selection and Iteration

Procedural is run in a sequence, first the main function is run in order and then the other functions when they are called. Things are outputted in a logical order, the welcome message is outputted first, then the menu and so on.ek

I used one selection statement to make a choice in the menu. Switch statements allow more options to be chosen and take the user down different paths.

switch (userOp) // A switch statement to make the users choice happen

{

case 1:

for (int loops = 0; loops < 10; loops++)

{

iNumber1 = rand() % 10 + 1;

iNumber2 = rand() % 10 + 1;

cout << iNumber1 << " x " << iNumber2 << endl;

do {

cin >> input;

if (cin.fail()) { // again this is used so that only numbers are entered

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

if (input > 100 || input < 1) // This is so that only numbers between 0 1 and 100 can be inputed

{

cout << "Re-try, enter a number equal to or less than 100 and more than 0.\n";

}

} while (input > 100 || input <1); // This is so that the program does not continue if they enter a number out of the specified range

answer = iNumber1*iNumber2;

totalScore(); // calling the function to output the total socre

}

break;

case 2: // this is option two for if they want to see instructions

cout << "You will be given a multiplication question and you will need to type the answer as an integer and then press enter. Another question"

<< " will appear and you will return to the menu after 10 questions.\n" << endl;

break;

case 3: // the third option (to exit)

system("exit");

return 0;

break;

default: // This shows when they enter a wrong number for the menu selection

cout << userOp << " Is not a valid choice, Enter a number\n";

break;

}

Iteration is event driven programming is the looping of the event handler. I used a while loop with a for loop inside which had a do while loop inside. The first while loop was used to loop the whole program once it’s finished so it returns to the menu, from there they can push 3 to quit. The for loop was used to create the multiplication part of the program, it was the best loop as I could control the amount of loops to 10 so 10 questions were asked. I did this with’ for (int loops = 0; loops < 10; loops++)’. Finally a do while loop was used to output an error when an invalid number was entered and stop them from getting a wrong answer for anything over 100 or less than 1. do {

cin >> input;

if (cin.fail()) { // again this is used so that only numbers are entered

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

if (input > 100 || input < 1) // This is so that only numbers between 0 1 and 100 can be inputed

{

cout << "Re-try, enter a number equal to or less than 100 and more than 0.\n";

}

} while (input > 100 || input <1); // This is so that the program does not continue if they enter a number out of the specified range.

It says to do the following while the condition at the bottom is true (met). The condition here is that a number less than 1 and more than a hundred is entered. When the condition is met an error message is shown. If the condition isn’t me then the program continues and ignores it.

Variable Types

Type Name

Bytes

Range of Values

Reason for choice

string

Depends on number of character

Was not necessary

int

4

-2147483648 to 2147483647

Most suitable as only numbers were being used in program.

double

8

1.7E +/- 308 (15 digits)

Only whole numbers were used in the test

Char

1

-128 to 127

Wasn’t necessary as numbers could be used.

bool

1

0 to 1

Good

A string would be used for the majority of the data in a database, there will be conversions which convert some of the data to be stored as a string variable. A string will be able to hold many characters, this is useful for when a user wants to input a post code to a database through the program created.

An int could be used to store a number, you can get short int variables as well which use less memory, they could be useful in devices with little memory such as toys. Int would be used as int iOne = 5; or you could get the user to input a value to iOne. This could be the users age or even a selection.

The double variable is used for decimals and large numbers, It can produce the most numbers however it won’t always be spot on accurate and will make some mistakes eventually but it is the best c++ has.

The char variable can store a character, it could be useful for menu selection in my program. It is used in the same was as int but a letter has to be stored.

Bool is used for True or False and is used to make decisions when a condition is met or when it’s not. Bool is used as Boolean statements, and example is if (numOne == numTwo) { cout << “both are equal”; The bool statement is the part in these brackets ().