Hereford and Ludlow College Assignment Title Unit number and name

Student name and number January 2012 Contents

Introduction The purpose of this report is to demonstrate an understanding of the knowledge and skills required to produce a computer program. In order to do this, four tasks have been set. The key objective is to produce two programs, written in Python, which perform the given functions.

The first is to produce a program that is required to aid a technician in his task of ensuring the continuity of heating of a substance in a vat. The second is show the functionality of the game ‘rock, paper, scissors’. Both of the programs illustrated in this report have been produced using Python version 2.6.

Python Background Python developer, Guido van Rossum, says it was “derived from ABC, a language designed to teach programming in the 1980's by Lambert Meertens, Leo Geurts, and others at CWI (National Research Institute for Mathematics Computer Science) in Amsterdam.“ He further adds that he had “imagined Python would be most useful in education...as a first language....the preferred medium for beginners” [1]

According to John Zelle in his book ‘Python Programming an Introduction to Computer Science’, Python “has a number of features that make it a near-perfect choice as a first programming language”. Among these are a standard library consisting of built-in-functions (BIFs), an online support community and numerous tutorials. It is a freely available, very quick and powerful high- level language that can be used on almost every platform, including Windows and UNIX, and comes pre-loaded on MAC OS X. Commonly, developers make use of an interpreter program, which converts the code into machine code for direct execution.

Software Design Before embarking on any sort of project it is important to have a clear objective and formulate a set of steps that will progress towards achievement of the goal. Weddings are an excellent illustration, for there are very few brides who do not spend months, or even years, agonizing over lists that map each element to the finest detail to ensure the day goes smoothly and is ‘perfect’. Writing a computer program is no exception and the most common starting point for a piece of software is in the preparation of a flowchart.

Page 2 of 16 Flowchart Production The purpose of a flowchart is to provide a graphical representation that follows each stage of a process to its logical conclusion. Along the way, there may be decisions that need to be made. These usually consist of two branches, ‘yes’ and ‘no’ or ‘true’ and ‘false’. In order to fulfil their usefulness, flowcharts should provide an accurate account of how the task is or should be performed. The diagram is constructed using universally recognized symbolic shapes that contain details of the step within. As the name suggests, each piece of data should flow effortlessly to the next. Flowcharts can be drawn by hand or through the use of a software application such as MS Word or Excel. Due to the complexity and technicalities of some business requirements there is also a wide choice of commercially available software for purchase.

The flowchart at Appendix A is produced using a free graphical program, ‘Dia’. It is a precursor to the engineering program outlined at the start of this report. The information known about the process is as follows:

1. The temperature must be checked every 15 minutes

2. The technician need do nothing if the temperature is below 102.5oc

3. If the temperature is above 102.5oc the thermostat must be turned down and the temperature checked after 5 minutes

4. Point 3 is to be repeated until the temperature is below 102.5oc, at which time the process recommences from point 1.

The instructions do not specifically state where the temperature reading originates and there is no indication of an upper or lower limit. It has therefore been assumed that the technician must input the reading. In order to accommodate human error input e.g. 10 or 1000 instead of 100, an ‘are you sure that’s correct’ default range has been included. If this were a true scenario, these points would be established to ensure requirements were being precisely met.

Upon satisfactory completion of a flowchart, the software developer should have a strong basis for the programming code. To help eliminate errors and maintain focus on the logical process it is helpful to next write the program in pseudo code.

Pseudo Code Pseudo code is derived from the meaning of pseudo ‘not the real thing’. The Berkeley Foundation for Opportunities in Information Technology (BFOIT) 2 says this about the thought process of an experienced programmer: “Pseudocode is a term for describing something in your native language. Her pseudocode is English descriptions of what she wants her program to do. Once this is complete and she is convinced that her plan should produce what she wants, it's time for the next phase: converting the pseudocode into... instructions”.

The pseudo code for this program can be found at Appendix B.

Page 3 of 16 Program Development The code listing for this program can be found at Appendix C. The Python ‘time’ module has been imported into the program to produce the time delay function. The module counts in seconds and, for the purpose of this exercise, 1 second was used to represent 1 minute. There are alternative methods for invoking a time delay such as using a nested loop. However, such a process can become lengthy and complicated particularly in tracing errors. It is much more convenient to call on a pre-formulated process.

Testing Nilesh Parekh[3], Computer Engineer and Author on Buzzle.com, has written a number of articles on software testing. He explains, quite clearly, the differences between white box and black box testing including the advantages and disadvantages. Put very basically, in order to perform white box testing, access to the source code is required together with knowledge of coding and the internal workings. With this method of testing it is the security of the program that comes under scrutiny as well as its vulnerabilities. The main disadvantage of this type of testing is that a skilled tester is required to carry out the process, which can increase overheads.

In contrast, black box testing requires no knowledge of the internal workings of the program as it is the requirements and functionality that are examined. Parekh says of black box testing that “nowadays, it is becoming common to route the testing work to a third party as the developer of the system knows too much of the internal logic”. Systems that are run as ‘beta’ are tested under a type of black box testing; relying on user feedback to ensure the program meets expectations and reliability.

Gray box testing, as the name implies, hosts a combination of black and white box testing.

The two programs in this assignment were tested at each stage of coding in order to check correctness before progressing onto the next step. This is generally considered to be a key factor in testing as it can prove difficult and time consuming, particularly when there is a long and complicated process involved, to locate errors at a later stage or at the end of development.

The testing table below follows the process used when testing the code and functionality of the temperature control program; tempchecktest.py. A selection of screen shots of the various testing stages is included to illustrate the progression of development. The problem table indicates the amendments undertaken to address the issues found.

Page 4 of 16 Page 5 of 16 Test Table No Function Test Expected Result Actual Result Problem Tested 1 Float User data Input (whole or decimal) & Request Input & output Requested user input. Raw_Input qualifying output message (testing > value message appropriate to Correct message output Figure 1 IF/ELSE only) value

2 Function Test 1 written as function Request Input & output Requested user input. Fig 2 message appropriate to Correct message output value 3 Time Delay Inclusion of second function using While Wait for count of 15 Output ‘counting’ 15 times in 1 Fig 3 loop with count to invoke time delay of 15 before requesting next quick succession (representing minutes) input 3 Time Delay Import time/ Request input, return Input requested correct Fig 4 & Program Time.sleep () using seconds to represent qualifying statement, qualifying message output & Loop minutes. Within function & IF/ELSE request reset & wait time delay before restart. statements. WHILE to restart program appropriate time before recommencing 4 Qualify To confirm input if outside of a specified Query & request check & Message to check and re- User Input range (based on expected input for re-input of data if outside input value program type) of parameters 6 Sentinel Inclusion of sentinel to quit out of program On entry of specified Did not recognise command 2 character program ends 6 Sentinel Inclusion of sentinel to quit out of program On entry of specified Program ran through loop 3 character program ends twice before ending 8 Sentinel Inclusion of sentinel & break command to On entry of specified On entry of specified quit out of program character program ends character program ended

Page 6 of 16 Problem Table

No Test Problem Solution Status 1 Inclusion of second function using While loop with Produced results Implement IMPORT TIME Fixed 20/11/11 count to invoke time delay of 15 (representing command and use SLEEP() to minutes) invoke time delay using seconds to represent minutes e.g. sleep (5) – waits 5 seconds before moving to next operation 2 Inclusion of sentinel to quit out of program Program did not Amended code to include a Partially operational recognise command & conditional loop. While continued to run reset_program = ‘y’ or ‘Y’ call the through operation with function no suitable ‘exit’ route 3 Inclusion of sentinel to quit out of program On entry of specified Program recognised sentinel but Fixed 4/1/12 character program ends performed an additional loop and exit question before ending

Page 7 of 16 Figure 1: Test 1

Figure 2: Test 2

Page 8 of 16 Figure 3: Test 3 – Fail

Figure 4: Test 3 – Pass

Page 9 of 16 Comments The following was observed on the testing of the programs:

Tempchecktest.py CODE LISTING AT APPENDIX C

It is noted that this program is showing an inconsistency in its execution. Whilst it was believed that the problem with the sentinel had been fixed with the inclusion of the ‘Q to quit’ and break command, it fails to operate at every execution, but reverts to the previous testing problem of running the loop and asking the question a second time before quitting.

A further design flaw, in the developer’s opinion, is the inability to place the program in a continual loop. The user has to confirm the restart of the function at the end of each successful cycle. In a real case situation, if the technician were to be distracted before confirming, then the strict time requirements would not be adhered to, as the 15 minute (in this case, second) time delay takes place after ‘y’ has been entered.

The program would be enhanced by the use of graphics and an audible alarm to indicate the end of the waiting period.

Rpsnew.py (Rock, Paper, Scissors) CODE LISTING AT APPENDIX D

The objective of this program was for the computer to randomly generate a number between 1 and 3, being representative of ‘Rock, Paper, and Scissors’. The user is to input their choice and the winner to be displayed. A tied game must played again to determine the winner. This is a popular choice of program for beginners to programming and there are many examples available on the internet [4]. They vary greatly including features such as ‘best of three’, or using a count function to determine ‘first to win 5’ etc. The code in this report is original. The program has been tested by the developer and family members.

The import.random module was incorporated to generate the random number between one and three (random.randrange). Although this should generate inclusive numbers specified, it only proved successful when the command was issued between one and four. A loop was successfully used to restart the game in the event of a tie. However, when incorporated into the body of the code it caused a system malfunction causing the program to become non responsive. Ctrl +C failed to interrupt and the only way to exit was to reboot the system. The cause has yet to be identified.

Once again, it is determined that graphics would enhance the program and the use of a scoring system would make the game more interesting for the player.

Conclusion Whilst each program delivers the required functionality, there is much that could be done to improve the design and usability, as stated above.

Page 10 of 16 Appendix A

Page 11 of 16 Appendix B

Pseudo-Code For Tempcheck Program

INIT program

GET input temperature

DETERMINE input within defined range

SET valid temperature range

Page 12 of 16 between 15 and 900

IF input outside RANGE PRINT Invalid entry INIT program

ELSE IF input above 102.5 PRINT Temperature too high PRINT Turn down thermostat Wait 5 minutes INIT program ELSE PRINT Temperature acceptable

GET confirmation to reset program

WHILE reset is ‘y’ or ‘Y’ Wait 15 minutes INIT program

IF reset is ‘q’ or ‘Q’

ENDWHILE

Appendix C

Code for tempchecktest.py

#This program is written to monitor and assist maintenance #Of constant vat temperature.

#Define the temperature check function def tempcheck():

#Set the loop and sentinel for the program reset_program='y' while reset_program=='y' or reset_program=='Y':

Page 13 of 16

#Get the temperature reading temp=float(raw_input('Please enter the current temperature in degrees celcius: '))

#Verify the input is acceptable while temp<15 or temp>900: print 'That may be incorrect. Please check your entry' temp=float(raw_input('Please re-enter the current temperature in degrees celcius: '))

#Perform the temperature range check if temp>102.5: print 'The temperature is too high' print 'Please turn down the thermostat and check again in 5 minutes'

#Initiate the time delay import time time.sleep(5) tempcheck() else: print 'The temperature is satisfactory. Next check in 15 minutes'

#Initiate the program restart reset_program=raw_input('Enter y to reset the program or q to quit:') if reset_program=='q' or reset_program=='Q': break

import time time.sleep(15)

#Call the function tempcheck()

Appendix D

Code for rpsnew.py

#This is a game of rock, paper, scissors #which will replay if outcome is tied choice

#Define the function def main():

#Get the random module import random

Page 14 of 16 #Generate a random number between 1 & 3 n=random.randrange(1,4)

#Name the constants if n==1: compc='rock' elif n==2: compc='paper' elif n==3: compc='scissors'

#Get the opponent to input choice opp=str(raw_input("please input your choice 'rock, paper, scissors': "))

#Test the input against the random number if n==1 and opp=='paper': print'I chose rock' print'paper wraps rock, you win' elif n==1 and opp=='scissors': print'I chose rock' print'rock crushes scissors, I win, I win' elif n==2 and opp=='scissors': print'I chose paper' print'scissors cuts paper, you win' elif n==2 and opp=='rock': print'I chose paper' print'paper wraps rock, I win, I win' elif n==3 and opp=='rock': print'I chose scissors' print'rock crushes scissors, you win' elif n==3 and opp=='paper': print'I chose Scissors' print'scissors cuts paper, yipee, I win'

#If tied game play again elif compc==opp: print'I also chose', compc print'Tied game, play again' main()

#Call the function main()

Page 15 of 16 Sources of Information [1] ‘Python Programming an Introduction to Computer Science’ by John Zelle; Franklin, Beedle & Associates 2004

[2] The Berkeley Foundation for Opportunities in Information Technology; www.bfoit.org

[3] www.Buzzle.com https://buildsecurityin.us-cert.gov/bsi/home.html

[4] www.daniweb.com; IT Discussion Community

Page 16 of 16