Outline 1 Modular Design multiple choice quiz bottom-up design 2 Python Implementation the modules main program: quiz.py namespaces in Python 3 The Software Cycle quality of product and process waterfall and spiral model 4 Summary + Assignments MCS 260 Lecture 22 Introduction to Computer Science Jan Verschelde, 4 March 2015 Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 1 / 28
modular design software cycle & quality 1 Modular Design multiple choice quiz bottom-up design 2 Python Implementation the modules main program: quiz.py namespaces in Python 3 The Software Cycle quality of product and process waterfall and spiral model 4 Summary + Assignments Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 2 / 28
Multiple Choice Quiz problem specification To test our knowledge of computer science concepts, we would like an automatic multiple choice quiz. Our friend the computer will 1 show a question with multiple possible answers 2 wait for us to make a choice 3 immediately evaluate our choice 4 ask us if we want to continue 5 at the end print how well we did Wanted: a small but extendable program. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 3 / 28
a screen shot: a first prototype The main program quiz.py, executed at the prompt $: $ python quiz.py The octal system has base... 1. 16 2. 8 3. 10 4. 2 type a number between 1 and 4 : 2 this is the correct answer continue? (y/n) n #correct : 1 #wrong : 0 -> 100.00% Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 4 / 28
Tools for Use what does Python provide? Python provides 1 a command line or Python shell environment 2 data structures: lists, tuples, and dictionaries 3 if-elif-else, loops, and functions 4 random number generators in the module random 5 our modules will reflect our bottom-up design What is there at the bottom of our program? Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 5 / 28
modular design software cycle & quality 1 Modular Design multiple choice quiz bottom-up design 2 Python Implementation the modules main program: quiz.py namespaces in Python 3 The Software Cycle quality of product and process waterfall and spiral model 4 Summary + Assignments Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 6 / 28
The Bottom of our Program A multiple choice quiz consists of 1 questions that have short answers; 2 answers, correct ones and variations. Every question and answer will be a string. Dictionaries seem very appropriate: 1 the questions are numbered, key = number, the values are strings 2 answers for the corresponding question have the same key as the question 3 answers are dictionaries of dictionaries 4 convention: correct answer comes first Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 7 / 28
The Other Modules We separate the functionalities: 1 generating questions and evaluating answers; 2 showing questions and asking answers. Two modules of functions: questions and dialogue. Both modules import the Q&A dictionary and export their functions to the main program. The formulation of questions and answers is centralized in the Q&A dictionary. A multiple choice question then consists of 1 the number of the question; 2 a permutation of the answers. In Python we use a tuple, e.g.: (3,[2,1,3,4]). Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 8 / 28
The Modular Design At the bottom: the Q&A dictionary. Two libraries of functions: questions and dialogue. automated quiz generate questions dialogue with user Q&A dictionary Information Hiding: the main program does not import the Q&A dictionary. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 9 / 28
modular design software cycle & quality 1 Modular Design multiple choice quiz bottom-up design 2 Python Implementation the modules main program: quiz.py namespaces in Python 3 The Software Cycle quality of product and process waterfall and spiral model 4 Summary + Assignments Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 10 / 28
the Q&A dictionary in the file quand.py QUESTIONS = { \ 1: How many bits go in one byte?, \ 2: Who is the main developer of Python?, \ 3: Which statement is true?, \ 4: The octal system has base... \ } ANSWERS = { \ 1:{ 1: 8, 2: 4, 3: 2, 4: 16 }, \ 2:{ 1: Guido van Rossum, 2: John von Neumann, \ 3: Rashi Gupta, 4: John Backus }, \ 3:{ 1: a compiler translates source code, \ 2: a compiler executes object code, \ 3: a compiler formats source code, \ 4: a compiler stores code on file }, \ 4:{ 1: 8, 2: 2, 3: 10, 4: 16 } \ } Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 11 / 28
specification of questions.py help(questions) shows under FUNCTIONS: evaluate(ans, chc) Evaluates choice chc to True or False where ans is a list of answers. generate() Returns a multiple choice question as a tuple (qst, ans), where qst is a number, and ans is a list of numbers with answers. update_tally(tly, boolvar) Updates tally of True or False answers where tly is a list of two elements and boolvar is True or False, returns the updated tally. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 12 / 28
specification of dialogue.py help(dialogue) shows under FUNCTIONS: prompt_choice(num) Asks the user for a choice where num is the number of choices. show_outcome(boolvar) Reports outcome to user where boolvar is True or False. show_question(qst, ans) Displays the questions and the answers on input is the number qst of the question and the list ans of answers. show_tally(tly) Shows final tally, tly is list. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 13 / 28
modular design software cycle & quality 1 Modular Design multiple choice quiz bottom-up design 2 Python Implementation the modules main program: quiz.py namespaces in Python 3 The Software Cycle quality of product and process waterfall and spiral model 4 Summary + Assignments Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 14 / 28
the main program in the file quiz.py import questions import dialogue from random import shuffle TLY = [0, 0] while True: (QST, ANS) = questions.generate() shuffle(ans) dialogue.show_question(qst, ANS) CHC = dialogue.prompt_choice(len(ans)) EVA = questions.evaluate(ans, CHC) dialogue.show_outcome(eva) TLY = questions.update_tally(tly, EVA) MORE = raw_input( continue? (y/n) ) if MORE!= y : break dialogue.show_tally(tly) Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 15 / 28
two functions in questions.py import quand def generate(): """ Returns a multiple choice question as a tuple (qst, ans), where qst is a number, and ans is a list of numbers with answers. """ import random qst = random.randint(1, len(quand.questions)) ans = range(1, len(quand.answers[qst])+1) return (qst, ans) def evaluate(ans, chc): """ Evaluates choice chc to True or False where ans is a list of answers. """ return (ans[chc-1] == 1) Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 16 / 28
show_question() in dialogue.py import quand def show_question(qst, ans): """ Displays the questions and the answers on input is the number qst of the question and the list ans of answers. """ print quand.questions[qst] choice = 1 for answer in ans: prt = %d % choice +. prt = prt + quand.answers[qst][answer] print prt choice = choice + 1 Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 17 / 28
giving feedback def show_outcome(boolvar): """ Reports outcome to user where boolvar is True or False. """ if boolvar: print this is the correct answer else: print wrong, please try again def show_tally(tly): """ Shows final tally, tly is list. """ cnt = #correct : %d % tly[1] cnt += #wrong : %d % tly[0] frm = 100.0*tly[1]/sum(tly) cnt += -> %.2f%% % frm print cnt Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 18 / 28
modular design software cycle & quality 1 Modular Design multiple choice quiz bottom-up design 2 Python Implementation the modules main program: quiz.py namespaces in Python 3 The Software Cycle quality of product and process waterfall and spiral model 4 Summary + Assignments Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 19 / 28
namespaces in Python A namespace is a collection of names available to use. Importing from modules enlarges the current namespace. dir() returns a list of the attributes of the module >>> import random >>> dir(random) [..., uniform,... ] >>> import numpy.random >>> dir(numpy.random) [..., uniform,... ] There are two uniform random number generators: random.uniform() and numpy.random.uniform(). >>> from random import uniform as u1 >>> from numpy.random import uniform as u2 u1() and u2 now belong to the current namespace Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 20 / 28
The Software Cycle We are experiencing the software crisis: the industrial sector can not keep up with the ever growing demands of the market. Symptoms: 1 products are delivered late 2 the cost exceed budgets 3 too many bugs appear The relative cost of hardware versus software was 80% versus 20% in the 1960s became 20% versus 80% in the 1980s keeps going in the same direction. Reason: hardware production is technology intensive, whereas software creation is human intensive. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 21 / 28
modular design software cycle & quality 1 Modular Design multiple choice quiz bottom-up design 2 Python Implementation the modules main program: quiz.py namespaces in Python 3 The Software Cycle quality of product and process waterfall and spiral model 4 Summary + Assignments Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 22 / 28
Quality of Software Design industrial quality Industrial quality refers to products 1 that must meet a target value on average, 2 and within a tolerable standard deviation. example: a search engine should give one page of relevant answers within an average of 10 seconds, and standard deviation of 2 seconds. We distinguish between quality of product and process: 1 a high quality product meets user s expectations, 2 also the quality of the production process matters! External qualities can be observed directly by the user. example: easy to use Internal qualities can be measured only by the producer. example: design documentation Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 23 / 28
Software Quality Features what is good software? A short list of important criteria: correctness must satisfy requirements How reliable is the software? efficiency economical use of resources such as hardware and time Do we understand how the complexity of the problem and the software relate? modifiability to different environments and requirements How hard is it to correct errors or to add new features? reusability of the same software in different applications Mathematical libraries are a typical example of reusable software. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 24 / 28
modular design software cycle & quality 1 Modular Design multiple choice quiz bottom-up design 2 Python Implementation the modules main program: quiz.py namespaces in Python 3 The Software Cycle quality of product and process waterfall and spiral model 4 Summary + Assignments Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 25 / 28
The Waterfall Model seven phases in sequence The software life cycle model describes the software production in seven phases: 1 feasibility study: cost-benefit analysis 2 system requirements specification: what must it do? 3 system architecture design: modules and relations 4 implementation and verification of modules 5 system integration and verification 6 installation: deliver software to the user 7 maintenance: fixing bugs and adding new features As the phases occur in a strict sequence, just as water never flows upwards, this model is like a waterfall. This deductive or top-down description of the life cycle has its merits, especially with feedback. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 26 / 28
The Spiral Model Four sectors in the software development: 1 requirements analysis and specification 2 system architecture design 3 implementation 4 verification Incremental development of software: 1 think of a product with limited scope 2 create the design of the first product 3 implement a prototype 4 test and deliver the prototype Based on feedback, enlarge the specifications, revise the designs, build new prototypes, test and deliver again. As the cycles over the four sectors get larger, the complexity of the software grows. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 27 / 28
Summary + Assignments Read 7.2 in Computer Science, an overview. Assignments: 1 Enlarge the Q&A answer dictionary with a least six more questions about relevant CS concepts. 2 Modify the program so the user has the option to see the correct answer when wrong. 3 Design your own trivia quiz. 4 Consider a quiz about the first 13 states of the union. The quiz links names of states with their captitals and year when they joined the union. 5 Develop a modular implementation of your own trivia quiz or of the quiz in the previous exercise. Intro to Computer Science (MCS 260) software cycle & quality L-22 4 March 2015 28 / 28