Villages Lab

September 1997 - Fell

Functions with Value-Passed Parameters

In this laboratory you will create villages in the drawing window by making appropriate calls to a function that draws a house like the one shown here.

You will vary the values of width, and height to make houses of varying sizes and proportions and of x and y to place the house in desired positions. Finally, you will modify the house function to add features to the house, e.g. windows, door, chimney, tree.

Practice calling a function with value-passed parameters.
Modify a function with value-passed parameters.
Practice loops.
Call on a random number generator.

Copy the VillagesLab folder. Run the application program Villages. When the program starts, there will be one house in the drawing window. Successive clicks of the mouse button should have the following effects:

  1. Two more houses are added to the first.
  2. Sixteen identical houses in four rows of four houses form the first village.
  3. Village two has houses that are varied. Random widths and heights were used.
  4. Village three is a row of town houses of equal width and varying height.
  5. Village four is a row of town houses of varying height and width.
  6. Village five has ten houses (possibly overlapping) somewhere in the drawing window.

Run the application program villagesW. Now the houses have a door and windows.
Open the project villages. and run it. Only the first house appears. Look at the source code villages.cp.

Two More Houses
(1) Add code to twoMoreHouses to create a large house in the lower left of the drawing window and a small house in the upper left of the drawing window. These do not have to be the same sizes or in the same positions as the ones in the application program villages .

First Village
(2) Add code to firstVillage to create sixteen identical houses in four rows of four houses. This should be done with a pair of nested for loops. The width and height should be the same for all the houses, somewhere between 20 and 45.

Make sure you get this working correctly before you go on.

Second Village
(3) First Village was pretty dull with its rows of identical houses. In your second village, you will use a random number generator to alter the width and height of the houses. Do not alter the function house. You should start by using select , copy , and paste to copy the body of the function FirstVillage into function SecondVillage.

Don't let the houses get too small or too big. Try something like:

width = RandomLong(20, 45);

Town Houses
(4) This time, you will make a row of attached houses like those one finds in the inner city. Add the code to the function townHouses to make a row of houses, across the middle of the drawing window all of width 20, but with random height between 40 and 90. You should need only a simple for loop for this function.

Can you make a small space between each pair of adjacent houses?

More Town Houses
(5) Use RandomLong to add a little more variety to your houses. Add code to the function moreTownHouses to make a row of houses, across the middle of the drawing window with random width between 15 and 25, random height between 40 and 90. Draw only those houses that fit entirely in the Drawing window, i.e. houses that satisfy: x + width <= 200.

This task is different than the last one in two ways. The x coordinate for each house depends on the width and position of the last house and you must decide whether a house is going to fit before you draw it. Use a while loop.

Random Houses - the Last Village
(6) Generate and draw ten random houses. You should use one for loop in which all four parameters are set to random values in suitable ranges.

These are better houses; see below.

Better Houses
Up to this point, you should not have altered the function house. No matter what size or shape or position your houses had, they all had black bottoms and white tops and no windows or doors or chimneys. Now it's time to build a better house. Only the function house should be changed. You may add windows or doors or chimneys or trees of steps... You may make the houses flat roofed with windows on one or more stories. The number of stories could depend on the height. Use your imagination.

(7) Make some improvement in the house design.

Make a screen dump of your last village with your new and improved houses and add it to your lab report.

Turn in:
A printed copy of your source code.
A print of your screen dump.
A diskette with
your source code (villages.cp)
your project (villages. )
your application (villages).

Last Updated: October 3, 1997 3:13 pm by
Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: