1 cse1301 computer programming lecture 26: case study

63
1 CSE1301 Computer Programming Lecture 26: Case Study

Post on 19-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 CSE1301 Computer Programming Lecture 26: Case Study

1

CSE1301Computer Programming

Lecture 26:Case Study

Page 2: 1 CSE1301 Computer Programming Lecture 26: Case Study

2

Topics

• Production principles

• Sample problem: Bingo

Page 3: 1 CSE1301 Computer Programming Lecture 26: Case Study

3

Software Quality Features• Correctness and Reliability

– satisfying the requirements

• Efficiency– obtaining the desired goal with a limited use of

resources (such as time or hardware)

• Modifiability– possibility of adapting software to changes in

requirements, and to correct errors

• Reusability– having modules which may be useful in other

applications

Page 4: 1 CSE1301 Computer Programming Lecture 26: Case Study

4

Design Principles

• Modular– Divide the system into manageable units

• Highly cohesive – Elements inside a module should be highly-

related in purpose

• Loosely coupled– Maximize independence between modules– Avoid implicit coupling (such as the use of

global variables)

Page 5: 1 CSE1301 Computer Programming Lecture 26: Case Study

5

Production Principles

• Design top-down– Break problem down into manageable sub-problems

• Build bottom-up– Code and test the simplest components first– Test each component before using them to build

more complex components

Page 6: 1 CSE1301 Computer Programming Lecture 26: Case Study

6

• Develop incrementally– Make simplified versions first – Add and test one functionality at a time, so that it

evolves into a complete system

• Document as you go along– System documentation: describes the software's

internal composition – User documentation: describes how to use the

software

Production Principles (cont)

Page 7: 1 CSE1301 Computer Programming Lecture 26: Case Study

7

Recall: Software Development Cycle

Analysis Design Implement Test

• Maintain documentation of the system throughout the entire cycle

Page 8: 1 CSE1301 Computer Programming Lecture 26: Case Study

8

• Problem specification: What are the requirements of the system?

Analysis

How do you play the game Bingo?

What are the program's tasks in the game?

Page 9: 1 CSE1301 Computer Programming Lecture 26: Case Study

9

• What's involved?– Bingo Game Boards– A Jar of numbered balls

• Who's involved?– The Game Master – A (number of) Player(s)

The Game of Bingo

Page 10: 1 CSE1301 Computer Programming Lecture 26: Case Study

10

• A 5 x 5 grid. Each square is a cell

Bingo: The Game Board

"cell"

• The central cell is marked initially

Page 11: 1 CSE1301 Computer Programming Lecture 26: Case Study

111-15 16-30 31-45 46-60 61-75

6

15

5

13

9

18

23

17

29

27

31

40

42

34

58

47

51

59

57

70

71

66

62

67

• The boards have random numbers assigned to cells in the ranges shown (each number can appear at most once)

Range for

each column

Bingo: The Game Board (cont)

Page 12: 1 CSE1301 Computer Programming Lecture 26: Case Study

12

• Contains 75 numbered balls

Bingo: The Jar

1 2 3 4

5 etc...

Page 13: 1 CSE1301 Computer Programming Lecture 26: Case Study

13

• At the start of game:

– Puts all balls into the jar

– Shakes the jar to mix

• During the game:– Takes a randomly-selected ball from the jar

– Calls out the number on the ball

– Note: The balls taken out are not returned back to the jar until the next game

Bingo: The Game Master

Page 14: 1 CSE1301 Computer Programming Lecture 26: Case Study

14

• At the start of game:– Fills the game board randomly

• During the game:– Marks a cell on the game board if it contains

the number called out by the Game Master

– Checks if the board has winning positions

– Calls out "Bingo!" if it is a winning board

Bingo: The Player

Page 15: 1 CSE1301 Computer Programming Lecture 26: Case Study

15

Bingo: Sample Winning Boards

6

15

5

13

9

18

23

17

29

27

31

40

42

34

58

47

51

59

57

70

71

66

62

67

6

15

5

13

9

18

23

17

29

27

31

40

42

34

58

47

51

59

57

70

71

66

62

67

6

15

5

13

9

18

23

17

29

27

31

40

42

34

58

47

51

59

57

70

71

66

62

67

Page 16: 1 CSE1301 Computer Programming Lecture 26: Case Study

16

• The player wins when s/he has a winning game board

• The game ends when

– there is a winner, or

– the jar is empty

Bingo: End of Game

Page 17: 1 CSE1301 Computer Programming Lecture 26: Case Study

17

Recall: Software Development Cycle

Analysis Design Implement Test

Page 18: 1 CSE1301 Computer Programming Lecture 26: Case Study

18

ask for the name of the playerask for N (the number of boards for the player)initialize the player’s score to 0

What are the program's tasks?

• Initialization: At the start of the program

Page 19: 1 CSE1301 Computer Programming Lecture 26: Case Study

19

loop{ ask what the user wants to do if ( user wants to start all over again ) { re-initialize program } else if ( user wants to play ) { play game } else if ( user wants to see scores so far ) { print all scores } else if ( user wants to quit ) end program /* say goodbye and all that */}

What are the program's tasks? (cont)

Page 20: 1 CSE1301 Computer Programming Lecture 26: Case Study

20

main

Call Structure (Draft): Main

initialize program

print all scores

end program

options menu

play game

Page 21: 1 CSE1301 Computer Programming Lecture 26: Case Study

21

main

Structure Chart (Draft): Main(control coupling)

initialize program

print all scores

end program

options menu

play game

Page 22: 1 CSE1301 Computer Programming Lecture 26: Case Study

22

place and mix all numbers in jar fill N game boards randomly print out all N game boards while ( player has not won yet ) {

pick a random number from jar update game boards for the player print out all N game boards }

if ( the player won ){ congratulate the player

increment the player’s score }

Algorithm to Play Bingo

Page 23: 1 CSE1301 Computer Programming Lecture 26: Case Study

23

Write a Program to Play Bingo (cont)

If things get too complex:

Start off with a simpler version of the problem!

Page 24: 1 CSE1301 Computer Programming Lecture 26: Case Study

24

• Number of boards per player– Assume: Only one board per player

• Scoring– Assume: No score is kept

Assumptions and Limitations (Version 1)

Page 25: 1 CSE1301 Computer Programming Lecture 26: Case Study

25

place and mix all numbers in jar fill game board randomly print out game board while ( player has not won yet ) {

pick a random number from jar update player’s game board print out game board }

if ( the player won ){ congratulate the player

increment the player’s score }

Version 1: Algorithm

Note that the if and the termination condition are the same

Page 26: 1 CSE1301 Computer Programming Lecture 26: Case Study

26

place and mix all numbers in jar fill game board randomly print out game board while ( player has not won yet ) {

pick a random number from jar update player’s game board print out game board }

congratulate player

Version 1: Algorithm (cont)

Page 27: 1 CSE1301 Computer Programming Lecture 26: Case Study

27

Call Structure (Draft): Version 1

start play

print board

pick a number

update player

initialize jar

fill board

play game

Page 28: 1 CSE1301 Computer Programming Lecture 26: Case Study

28

update player

Call Stucture (Draft): Version 1

mark board

check diagonals

check rows

check columns

Page 29: 1 CSE1301 Computer Programming Lecture 26: Case Study

29

Software Development Cycle

Analysis Design Implement Test

Page 30: 1 CSE1301 Computer Programming Lecture 26: Case Study

30

Data Representation

• Identify shared “objects” in the system, and how to represent information about them in the program

• Produce a data dictionary of type definitions– The type definitions are usually stored in a

header file (bingo.h), which is shared by all the programs

– Keep possible future enhancements in mind

Page 31: 1 CSE1301 Computer Programming Lecture 26: Case Study

31

game

player

board

Data Representation (cont)

• What are the "objects" in Bingo?

celljar

ball called number

Page 32: 1 CSE1301 Computer Programming Lecture 26: Case Study

32

game

player

board

Data Representation (cont)

celljar

ball called number

Variables of type int

Page 33: 1 CSE1301 Computer Programming Lecture 26: Case Study

33

player

board

Data Representation: Cell

cell int cell;

#define MAX_VAL 75

Each cell normally contains an integer between 1 and

MAX_VAL

Page 34: 1 CSE1301 Computer Programming Lecture 26: Case Study

34

player

board

Data Representation: Cell (cont)

cell int cell;

How would you indicate a marked cell?

#define MARKED_VAL 0

A cell with value MARKED_VAL indicates that it has been marked

Page 35: 1 CSE1301 Computer Programming Lecture 26: Case Study

35

struct BingoRec{ int cell[BOARD_DIM][BOARD_DIM];};

typedef struct BingoRec BingoBoard;

player

board

Data Representation: Board

cell

#define BOARD_DIM 5

Makes future modifications (large-sized boards) easy

Page 36: 1 CSE1301 Computer Programming Lecture 26: Case Study

36

/* ================================================ * * DATA: * BingoBoard * * DESCRIPTION: * A board is a grid of BOARD_DIM x BOARD_DIM * cells. * * USAGE: * Each cell normally contains an integer * between 1 and MAX_VAL. * A cell with value MARKED_VAL indicates that * it has been marked. * * ================================================ */

Data Documentation: Board (cont)

Page 37: 1 CSE1301 Computer Programming Lecture 26: Case Study

37

typedef struct

{

BingoBoard board;

} PlayerInfo;

Data Representation: Player

player

board

cell

Page 38: 1 CSE1301 Computer Programming Lecture 26: Case Study

38

typedef struct

{

BingoBoard board;

char name[NAME_LEN];

} PlayerInfo;

Data Representation: Player (cont)

#define NAME_LEN 80

player

board

cell

Page 39: 1 CSE1301 Computer Programming Lecture 26: Case Study

39

typedef struct

{

BingoBoard board;

char name[NAME_LEN];

int isWinner;

} PlayerInfo;

Data Representation: Player (cont)

Acts as a "flag":

True or false

player

board

cell

Page 40: 1 CSE1301 Computer Programming Lecture 26: Case Study

40

/* ====================================== * * DATA: * PlayerInfo * * DESCRIPTION: * Contains a player's details and bingo board. * * USAGE: * `isWinner' is 1 if this player currently * has a winning board; 0 otherwise. * * ======================================= */

Data Documentation: Player

Page 41: 1 CSE1301 Computer Programming Lecture 26: Case Study

41

typedef struct

{

int JarArray[MAX_VAL];

int numballs;

} JarInfo;

Data Representation: Jar (cont)

jar

ball

We know how to represent the jar from Lecture 21

Page 42: 1 CSE1301 Computer Programming Lecture 26: Case Study

42

Data Representation: Game

typedef struct{ PlayerInfo player; JarInfo jar; int calledNumber;} GameInfo;

• Encapsulates all the information needed for Bingo (Version 1: one player, one board)

Page 43: 1 CSE1301 Computer Programming Lecture 26: Case Study

43

Shared Data

• Information that modules need to share with each other

• May require a description of the tasks of each module

Page 44: 1 CSE1301 Computer Programming Lecture 26: Case Study

44

Call Structure (Draft): Version 1

start play

print board

pick a number

update player

initialize jar

fill board

play game

Page 45: 1 CSE1301 Computer Programming Lecture 26: Case Study

45

• Tasks:– input player's name– set player.isWinner to False– put all balls in the jar– fill the board randomly

according to rules

Call Structure (Draft): Version 1

start play

initialize jar

fill board

jar

board

game

play game

Page 46: 1 CSE1301 Computer Programming Lecture 26: Case Study

46

Call Structure (Draft): Version 1

• Tasks:if the called number is on the board:– mark the cell– check for winning rows, columns

or diagonals– update player.isWinner

player, calledNumber

update player

player

play game

Page 47: 1 CSE1301 Computer Programming Lecture 26: Case Study

47

Call Structure (Draft): Version 1

pick a number

number,jar

jar

• Tasks:– select a number at random

from the jar– remove the number from

the jar – return the number

play game

Page 48: 1 CSE1301 Computer Programming Lecture 26: Case Study

48

Call Structure (Draft): Version 1

boar

dprint

boardstart play

initialize jar

fill board

jar

board

game

player, calledNumber

update player

player

jar

pick a number

number,jar

play game

Page 49: 1 CSE1301 Computer Programming Lecture 26: Case Study

49

Modular Development

• Recall:– Each module (or sub-module) is implemented

as a C function– Execution of a module (or sub-module) is done

with a function call

• It is a good idea to store the functions of each component in a separate file

Page 50: 1 CSE1301 Computer Programming Lecture 26: Case Study

50

Modular Development (cont)

• Identify “priority” modulesExamples:– modules for initialization of shared data

• startPlay– modules for testing other modules

• printBoard -- to test fillBoard and updatePlayer– modules which will help glue the sub-modules

together

• playGame

Page 51: 1 CSE1301 Computer Programming Lecture 26: Case Study

51

#include <stdio.h>#include <stdlib.h>

#include "bingo.h" /* data dictionary, #define-s */

int main(){ GameInfo theGame;

/* insert code for main algorithm here */

return 0;}

Skeleton of Main Module

Page 52: 1 CSE1301 Computer Programming Lecture 26: Case Study

52

A "Priority" Module: Print Board

boa

rd

print board

print cellce

ll va

lue

board

cell

Page 53: 1 CSE1301 Computer Programming Lecture 26: Case Study

53

/* ======================================================== * NAME: * void printBoard ( BingoBoard *board ) * * DESCRIPTION: * This function is used to print out a bingo board. * printBoard() calls printCell() to print out an * individual cell. * If the cell value is equal to MARKED_VAL, then the cell * is considered marked. printCell() outputs an indicator * (such as an asterisk) if the cell is marked. * * PRE: * It is assumed that `board' points to a struct of type * BingoBoard, and that the struct has been initialized * appropriately so that the cells contain only valid * values. * * POST: * none. * ======================================================= */

Indicates that the function will not modify the board, even if the

function uses a pointer

"Stub" for module printBoard

Page 54: 1 CSE1301 Computer Programming Lecture 26: Case Study

54

void printCell ( int cellValue ){ if ( cellValue == MARKED_VAL ) { printf(" **"); } else if ((1 <= cellValue) && (cellValue <= MAX_VAL)) { printf("%4d", cellValue); } else { printf(" BAD"); } return;}

bingo-1a-board.c

Page 55: 1 CSE1301 Computer Programming Lecture 26: Case Study

55

Testing Modules

• A test program is used to demonstrate that a module's implementation performs as expected

• Recall: Lecture 15: Flowcharts and debugging– Test data should check every possible execution

path of the algorithm

Page 56: 1 CSE1301 Computer Programming Lecture 26: Case Study

56

Testing Modules (cont)

• Build incrementally: Once a module has been verified using the test program, you can use it to build larger modules

• Simulate dependencies to set controlled conditions for testing

Page 57: 1 CSE1301 Computer Programming Lecture 26: Case Study

57

#include <stdio.h>#include <stdlib.h>#include "bingo.h" /* data dictionary, #define-s *//* assumes board.c is included in the project */

int main(){ /* valid boundary data */ printCell(1); printCell(75);

/* special case */ printCell(MARKED_VAL);

/* invalid data */ printCell(-1); printCell(76);

printf("\n"); return 0;}

bingo-1a-test1a.c

Page 58: 1 CSE1301 Computer Programming Lecture 26: Case Study

58

void printBoard ( BingoBoard *board ){ int row, col ;

for ( row = 0; row < BOARD_DIM; row++ ) { printf("\t"); for ( col = 0; col < BOARD_DIM; col++ ) { printCell ( (*board).cell[row][col] ); } printf("\n"); } return;}

bingo-1a-board.c

Page 59: 1 CSE1301 Computer Programming Lecture 26: Case Study

59

#include <stdio.h>#include <stdlib.h>#include "bingo.h" /* data dictionary, #define-s *//* assumes board.c is included in the project */

const int X = MARKED_VAL;

int main(){ /* We'll fill the board up just for testing. */

BingoBoard theBoard = {{ {1, 16, 31, 46, 61}, {2, 17, 32, 47, 62}, {3, 18, X, 48, 63}, {4, 19, 33, X, 64}, {5, 20, 34, 50, 65} }};

printBoard(&theBoard);

return 0;}

bingo-1a-test1b.c

Page 60: 1 CSE1301 Computer Programming Lecture 26: Case Study

60

System Integration

• Putting components together to form larger components, until the entire system is complete

• Often challenging in large systems where several components have to be developed by different people

• Documentation and Documentation/Naming Conventions

Page 61: 1 CSE1301 Computer Programming Lecture 26: Case Study

61

#include <stdio.h>#include <stdlib.h>#include "bingo.h" /* data dictionary, #define-s *//* assumes board.c., jar.c, player.c are included */int main(){ GameInfo theGame;

startPlay(&theGame); printBoard(&(theGame.player.board));

while (!(theGame.player.isWinner)) { callOutNumber(&(theGame.jar),

&(theGame.calledNumber)); updatePlayer(&(theGame.player)); }

printf("BINGO! Congrats, %s!\n", theGame.player.name);

return 0;}

Code for Main Algorithm -- Version 1

Page 62: 1 CSE1301 Computer Programming Lecture 26: Case Study

62

On to Bingo -- Version 2

• Several boards per player

• Keep the scores in a continuing game

Page 63: 1 CSE1301 Computer Programming Lecture 26: Case Study

63

Reading

• Brookshear (5/e or 6/e): Chapter 6