Homework 3

This assignment is worth up to three extra credit points. Most students will receive only one or two points; only the highest quality answers will receive three points. Note: if your code doesn't meet the minimum requirements below, you will be in danger of receiving zero points. Certain aspects of the assignment below are left unspeciifed and you are free to do whatever you want when implementing those areas of the assignment. But if the assignment specifically calls for something (such as the alignment methods having to operate on a collection of shapes) in the implementation, then your implementation must match the stated requirements.

Shape Framework

Create a simple implementation of a shape framework that includes the ability to manipulate rectangles, circles, squares, and lines. This framework defines a shape environment via a canvas class that has an x and y coordinate system. Each shape has an x and y attribute that determines its location. For squares and rectangles, the x and y determine the rectangle's top (y), left (x) point. For circles, the x and y determine the location of the circle's center. For lines, the x and y determine the location of one of the line's endpoints. Each shape class will have to define additional attributes to allow the shape to be positioned on the canvas. Circles need a radius, lines need an additional endpoint, and rectangles/squares need height and width attributes. Define methods that allow shapes to be moved and scaled (scaling takes a shape and makes it X times bigger or X times smaller) as well as methods that return the area and perimeter of each shape. In addition, write one method that takes a collection of shapes and aligns them vertically and an additional method that takes a collection of shapes and aligns them horizontally.

Note: I am NOT asking you to create a graphical environment. Instead, you are creating a framework that could be used by a graphical environment to track the shapes a user creates via point-and-click operations on toolbars, canvases, and the like. Instead, you should create a report() method on the canvas class that will generate a textual report of the state of the environment: what shapes have been created and the state of their attributes.

To test your environment, create a main program that:

  1. creates a canvas, and
  2. adds 5 to 10 shapes of various types to it.
  3. Then randomly select a shape and apply either a move() or scale() operation to it (randomly chosen).
  4. Repeat the previous step 19 times until 20 shapes have been randomly selected and either moved or scaled.
  5. Then, find the two shapes that are farthest apart along the x axis and align them vertically
  6. Then, find the two shapes that are the farthest apart along the y axis and align them horizontally

Your main program should invoke the report() method of the canvas operation after step 2, after each iteration of step 3 (i.e. 20 times total), step 5 and step 6.

Although not specified explicitly above, you should use inheritance when defining your shape hierarchy and should have your canvas class deal with shapes by using the root of your hierarchy as much as possible. That is to say that your canvas class should only deal with specific shape subclasses when creating shapes, but should otherwise manipulate shapes using the root of the shape hierarchy.

Create a README file that contains instructions on how to invoke your program and a file called output.txt that contains the output from executing your main program.

You may use any object-oriented programming language to complete this assignment. However, if you choose to use a language other than Python, Ruby, Java or C#, please let me know ahead of time.

This assignment is due at 11:55 PM on Thursday, September 25th. Please upload a zip archive to the moodle that contains the source code and README file for this assignment.

Any questions? Send them to Prof. Anderson. Recall that you must not work with other students on this assignment. To remind you of this, please include the following pledge at the top of your README file:

"On my honor as a University of Colorado at Boulder student, I have neither given nor received unauthorized assistance on this work."

Kenneth M. Anderson, 2008