## CS 101 (Fall 2000) Lab 1: Expressions, Functions, and Procedural Abstraction

Lab Assigned Design Due
(In class)

1 PM
Implement
(In Lab)
Demo
(In Lab)
Lab Due
(In class)
Friday
1 PM
11 Sep None 13-14 Sep 20-21 Sep 22 Sep

### Goals:

By the end of this lab, you should...
• be able to write Java expressions for mathematical formulas.
• understand procedural abstraction, formal parameters, actual parameters, and return values.
• be able to define procedures that describe simple computations.
• understand naming abstraction, and the difference between a name and a value.

### Before starting:

• Read over the entire lab before you start.
• Before beginning the assignment, download the provided portions of the lab into your cs101 folder by clicking on the "download project" button in the on-line version of this assignment.
• Run the sample solution.
• Use the paint program to experiment with how to draw arcs.
• If you take CS 102, you could make a version like this one, by Nate Bayless. It uses threads to get the cars moving. Click to drop anvils on the cars.

To run that version, click here. The write-up for this lab is here if you want to try to program it.

### Assignment:

You're a city planner for St. Louis, which is looking to expand its borders to the surrounding acreage of Missouri. Your job, in this lab, is to be able to direct the building of St. Louis' expansion to your own ends.

You will implement a `Skyline` class that draws buildings on the screen and tells you some important planning information about these buildings. Think of this as a much-simplified version of playing architect; although the task seems simple enough (with one twist we'll reveal at the end), this is an example of computer-aided design; to build a building, just point and click!

1. Begin by opening the Skyline.java file in Emacs. (When you choose "Open File..." from the Files menu, a message will appear at the bottom of the screen asking which file you wish to open. Please see a TA if you need assistance.)

Supply the necessary header information (name, lab section, etc.). This is the only file you need to modify for this lab. Look over the file carefully before modifying it! Please note the following:
• There is a place for each of your methods in the file. All you need to do is fill in the code where it says "Your code goes here". We declare each of the methods properly for you; you do not need to worry about method declarations, just their bodies.
• Sometimes there are partial implementations that tell you important hints about how to accomplish some of the requirements. Please delete any code which does not end up being a part of your solution, especially if we have marked it for deletion! The code is there to illustrate how to do things in Java; it is not meant to serve as part of your solution unless it is explicitly stated as such.

Each of the following specifications describes the input parameters and return values of a desired procedure that is needed as a method of the Skyline simulator. For each specification,

• write a Java procedure that correctly implements that specification, and
• type your procedure into the place indicated in the file Skyline.java.
Hint: To avoid making the same mistakes over and over, test as you go. That is, type in a procedure, compile, execute, and verify the results, and then go on to the next problem.  After you get the hang of it, you can write more code before you test.  Detailed instructions for running your program are listed below.

2. Write a method called `drawBuilding` that does the following:
• draws a building on the screen. You will receive three parameters:
• an integer, x, which represents the x-coordinate of the upper left-hand corner of the building;
• an integer, y, which represents the y-coordinate of the upper left-hand corner of the building;
• an integer, width, which represents the width of the building
• prints the area of the building to the Transcript.
It is important to remember that in computer graphics, the y-coordinate increases going down the screen rather than going up. We have provided the named constant CANVAS_HEIGHT to represent the y-coordinate of the bottom of the screen. A diagrammatic representation of the building you should produce looks something like this:

```      (x,y)   		    (x+width,y)
+-----------------------+
|			|
|			|
|			|
|			|
|			|
(x,CANVAS_HEIGHT)	(x+width,CANVAS_HEIGHT)
```

There are only three lines; you should leave the bottom line out (as if the building were to extend below the bottom of the canvas).

3. Write a method called `reportSquixels` that:
• prints out the total area of all buildings, and
• prints out the average area per building.
The method takes no parameters. (FYI, "squixels" is a term we made up to represent "square pixels". A pixel is a dot on the screen, essentially.) In order to do what is required, you will need to keep track of instance variables in your Skyline.java file; for example, you may want to have a variable which represents the total of all building areas, and update it anytime a building is drawn. Instance variables (also known as fields) will be covered in class.

4. Write a method called `makeArch` that draws an arch in the middle of the skyline. To accomplish this, 2 `Arcs` will be drawn. The `makeArch` method takes in an integer Y-value. The first `Arc` will be placed at this y-coordinate. The x-coordinate must be calculated. To do this:

• First find the middle of the ViewFrame...this is easy as you are given CANVAS_WIDTH.
• Then calculate the width of the main Arc. The width should be 3/4 of the height.
• Now you can find the x-coordinate by subtracting half the width from the middle.
• That's everything you need for the first `Arc`!!!

The second `Arc` will be placed inside the first one to create the full arch. For this `Arc`:

• The width will be 4/5 of the first `Arc`.
• The height will be 19/20 of the first `Arc`.
• Then, you can calculate the x and y-coordinates using these values.
• And that takes care of the second `Arc`!!!

NOTE that Arcs are created in a U-shape. You must rotate it to get the proper result. Add them to the skyline and an arch is born!!

5. Write a method called `earthquake` that causes the last building you drew on the skyline to collapse into the ground. It takes no parameters. Again, you will need instance variables in your class to "remember" the last building you drew. You can use the instance variables to represent the three lines that make up the building and the height of the building. Your `earthquake` method should make the building fall by using the `smoothMove` method within canvas. Call `smoothMove(int newX, int newY, int milliseconds)` on each of the three lines. The lines' x-coordinates DO NOT change, their new y-coordinates should be the CANVAS_HEIGHT, and the input value for milliseconds is how many milliseconds it will take to move the lines to the new position.

6. Execute your program by opening Lab1.java (in the same folder as Skyline.java) and compiling it (choose "Compile" under the "JDE" menu). After the compile has finished successfully (you will see a message saying so), you may run the program by choosing "Run App" from the "JDE" menu.

You should see an empty skyline. Click anywhere on the screen to place a building; the next building will be drawn starting from the left side of the screen and whose top is exactly aligned with the y-coordinate of where you clicked. Be sure that your program prints out the correct messages and that the earthquake happens smoothly.

Run the SAMPLE SOLUTION!!

### What To Turn In:

For every CS101 lab you turn in, you should fill in a cover sheet and staple it on the front of your lab. Attach a paper printout of the following:

1. your Skyline.java file containing the methods you wrote, and
2. your transcript.txt file created when you ran your program. This must be turned in by the start of class on the due date.  Check that you have header information (name, email, date, and lab section) at the top of the file, and be sure to demonstrate the lab for a TA before turning it in!

If you need help printing, ask a TA or refer to the lab homepage, which has detailed instructions for how to print from the labs.