assignment title - herefordshire and ludlow...

20
Hereford and Ludlow College Assignment Title Unit number and name Student name and number January 2012

Upload: vuongkhue

Post on 23-Feb-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

Hereford and Ludlow College

Assignment Title

Unit number and name

Student name and numberJanuary 2012

Page 2: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

ContentsIntroduction...........................................................................................................................................3

Python Background...............................................................................................................................3

Software Design.....................................................................................................................................3

Flowchart Production........................................................................................................................3

Pseudo Code......................................................................................................................................4

Program Development......................................................................................................................4

Testing...................................................................................................................................................5

Test Table..............................................................................................................................................6

Problem Table.......................................................................................................................................7

Comments and Conclusions................................................................................................................10

Tempchecktest.py...........................................................................................................................10

Rpsnew.py (Rock, Paper, Scissors)...................................................................................................10

Appendix A..........................................................................................................................................11

Appendix B..........................................................................................................................................12

Pseudo-Code For Tempcheck Program............................................................................................12

Appendix C..........................................................................................................................................13

Code for tempchecktest.py.............................................................................................................13

Appendix D..........................................................................................................................................14

Code for rpsnew.py.........................................................................................................................14

Sources of Information........................................................................................................................16

Page 2 of 17

Page 3: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

IntroductionThe 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 BackgroundPython 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 DesignBefore 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.

Flowchart ProductionThe 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

Page 3 of 17

Page 4: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

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 CodePseudo 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.

Program DevelopmentThe 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.

Page 4 of 17

Page 5: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

TestingNilesh 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 5 of 17

Page 6: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

Test TableNo Date Function

TestedTest Expected Result Actual Result Pass

/FailProblem

1

Figure1

9/11/11 Float Raw_InputIF/ELSE

User data Input (whole or decimal) & qualifying output message (testing > value only)

Request Input & output message appropriate to value

Requested user input. Correct message output

P

2Fig 2

11/11/11 Function Test 1 written as function Request Input & output message appropriate to value

Requested user input. Correct message output

P

3Fig 3

11/11/11 Time Delay Inclusion of second function using While loop with count to invoke time delay of 15 (representing minutes)

Wait for count of 15 before requesting next input

Output ‘counting’ 15 times in quick succession

F 1

3Fig 4

4/12/11 Time Delay & Program Loop

Import time/Time.sleep () using seconds to represent minutes. Within function & IF/ELSE statements. WHILE to restart program

Request input, return qualifying statement, request reset & wait appropriate time before recommencing

Input requested correct qualifying message output & time delay before restart.

P

4 26/12/11 Qualify User Input

To confirm input if outside of a specified range (based on expected input for program type)

Query & request check & re-input of data if outside of parameters

Message to check and re-input value

P

6 26/12/11 Sentinel Inclusion of sentinel to quit out of program

On entry of specified character program ends

Did not recognise command F 2

6 26/12/11 Sentinel Inclusion of sentinel to quit out of program

On entry of specified character program ends

Program ran through loop twice before ending

F 3

8 4/1/12 Sentinel Inclusion of sentinel & break On entry of specified character On entry of specified P

Page 6 of 17

Page 7: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

command to quit out of program program ends character program ended

Problem Table

No Test Problem Solution Status1 Inclusion of second function using

While loop with count to invoke time delay of 15 (representing minutes)

Produced results Implement IMPORT TIME command and use SLEEP() to invoke time delay using seconds to represent minutes e.g. sleep (5) – waits 5 seconds before moving to next operation

Fixed 20/11/11

2 Inclusion of sentinel to quit out of program

Program did not recognise command & continued to run through operation with no suitable ‘exit’ route

Amended code to include a conditional loop. While reset_program = ‘y’ or ‘Y’ call the function

Partially operational

3 Inclusion of sentinel to quit out of program

On entry of specified character program ends

Program recognised sentinel but performed an additional loop and exit question before ending

Fixed 4/1/12

Page 7 of 17

Page 8: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

Figure 1: Test 1

Figure 2: Test 2

Page 8 of 17

Page 9: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

Figure 3: Test 3 – Fail

Figure 4: Test 3 – Pass

Page 9 of 17

This function caused the word ‘counting’ to be printed 15 times in quick succession.

This re-coding corrects the error but requires the user’s attention and no way to ‘quit’ the program.

Page 10: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

Comments The following was observed on the testing of the programs:

Tempchecktest.pyCODE 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.

ConclusionWhilst 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 17

Page 11: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

Appendix A

Page 11 of 17

Page 12: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

Appendix B

Pseudo-Code For Tempcheck Program

INIT program

GET input temperature

DETERMINE input within defined range

Page 12 of 17

Page 13: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

SET valid temperature rangebetween 15 and 900

IF input outside RANGEPRINT Invalid entryINIT program

ELSE IF input above 102.5PRINT Temperature too highPRINT Turn down thermostatWait 5 minutesINIT program

ELSEPRINT Temperature acceptable

GET confirmation to reset program

WHILE reset is ‘y’ or ‘Y’Wait 15 minutesINIT 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 functiondef tempcheck():

#Set the loop and sentinel for the program

Page 13 of 17

Page 14: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

reset_program='y' while reset_program=='y' or reset_program=='Y': #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

Page 14 of 17

Page 15: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

def main():

#Get the random module import random

#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 functionmain()

Page 15 of 17

Page 16: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

Page 16 of 17

Page 17: Assignment Title - Herefordshire and Ludlow Collegewiki.hct.ac.uk/.../exampleprogrammingassignment..docx  · Web viewWriting a computer program is no exception and the most common

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 17 of 17