Chapter Three Class Overheads


Problem: How much will it cost to paint the dining room?

Understand the problem?

If I know: Price of a gallon of paint.
How many square feet one gallon covers.
What the area to be covered is.
Then the answer is easy:
Divide the area to be painted by the number of square feet a gallon covers to get the number of gallons needed.

Then multiply the number of gallons needed by the price.

Top Down Approach
I N P U T    To the problem
O U T P U T    Expected from the solution
C O N N E C T I O N    Of

INPUT
\
- > PROCESS
\
- > OUTPUT

Design the process by successive levels of detail

INPUT:
Cost of a gallon of paint (price).
Number of square feet a gallon of paint covers (ftpergal).
Dimensions of the room (width, length, height).

OUTPUT:
How much it costs to paint the dining room (cost).

LEVEL 0:
Calculate Area
Calculate Gallons Needed
Calculate Cost
Print Cost

LEVEL 1: (second level of detail)
Calculate Area
Area of Ceiling (carea) width * length
Area of Walls (warea)
Area of Room (roomarea) carea + warea
Calculate Gallons Needed
roomarea ÷ sqftpergal
Calculate Cost
price * galneeded
Print Cost
writeln(cost)
LEVEL 2: (third level of detail)
Area of Walls
Width Walls (widtharea)
2(width*height)
Length Walls (lengtharea)
2(length*height)

PROGRAM PAINT(INPUT,OUTPUT);
CONST
WIDTH=15;
LENGTH=12;
HEIGHT=9;
PRICE=14;
SQFTPERGAL=350;
VAR
CAREA,WAREA,ROOMAREA,GALNEEDED,COST:INTEGER;
BEGIN
CAREA:=LENGTH*WIDTH;
WAREA:=2*(WIDTH*HEIGHT)+2*(LENGTH*HEIGHT);
ROOMAREA:=CAREA+WAREA;
GALNEEDED:=ROOMAREA DIV SQFTPERGAL + 1;
COST:=GALNEEDED*PRICE;
WRITE('COST TO PAINT DINING ROOM IS');
WRITELN(COST)
END.

How much will it cost to paint your dining room?

I.E., How can we make this program general?

Make width
length
height
price
VARIABLES

and read them in as data

That is, set up locations for them using the VAR. Then use a READ statement to put values into those locations. Each time you have a new room, you just have to change the data, not the program.

PROGRAM PAINT2(INPUT,OUTPUT);
CONST
SQFTPERGAL=350;
VAR
CAREA,WAREA,ROOMAREA,GALNEEDED,COST,WIDTH,LENGTH,HEIGHT,PRICE:INTEGER;
BEGIN
READ(WIDTH,LENGTH,HEIGHT,PRICE);
CAREA:=LENGTH*WIDTH;
WAREA:=2*(WIDTH*HEIGHT)+2*(LENGTH*HEIGHT);
ROOMAREA:=CAREA+WAREA;
GALNEEDED:=ROOMAREA DIV SQFTPERGAL + 1;
COST:=GALNEEDED*PRICE;
WRITE('COST TO PAINT DINING ROOM IS');
WRITELN(COST)
END.

15 12 9 14

Summarize
Top Down Design
Input
Output
Process
Successive levels of detail
Data Input
Pascal
READ( . . . )
Next READ continues on the same line
READLN( . . . )
Next READ begins new line