Homework 1

This homework is worth 50 points and is due on Thursday, September 20th by 11:55 PM. Please upload your work to the moodle in a single zip or tar archive. The archive should contain a single PDF file or text file that contains the answers to questions 1 and 2 and should also contain a folder that holds the source code, README file, and output.txt file that you wrote/generated for question 3. At the top of your PDF or text file, please insert the honor code statement and type your name under it, in lieu of your handwritten signature. The honor code statement is:

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

This request serves as a reminder that I do not want you working with other students on homework assignments.

If you have questions about the homework, please send mail to Professor Anderson, or come to his office hours.

Question 1. (10 points)

The figure below contains two class diagrams showing different was to arrange three classes: Shape, Rectangle, Square. Describe what each diagram is asserting about the classes, i.e. what is it that you know about these classes based on the semantics of the UML notation. How are the semantics of these two diagrams similar and how are they different?

fig. 1a.fig. 1b.

Now, imagine in the diagram on the left that the Rectangle class defines two attributes: width and height and defines getter and setter methods for these attributes in the standard way. However, the Square class cannot directly reuse all four of these methods. Why not? Does this have any implication on using inheritance between Rectangle and Square? Put another way, can using inheritance between these two classes cause any problems?

Given your answer to the previous questions, discuss how the diagram on the right might address the problems and/or implications that you described.

Question 2. (10 points)

Create a class diagram that captures as much information as possible from the textual description below.

A structural computing system is made up of elements. There are two types of elements, atoms and collections. Atoms are used to store application-specific objects supplied by clients; Collections are used to group other elements. All elements have a unique id and a set of attributes. Each attribute has a name, which is a string, and a value, which can be one of any number of different types all of which share a common interface provided by an abstract class called AttributeValue. Elements are stored by a repository, which manages their persistence and which also can be used to search for specific elements via their attributes.

Question 3. (30 points)

Create a simple implementation of a shape environment that includes the ability to manipulate rectangles, circles, squares, and lines. The environment is defined by a canvas 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 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, 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.

Kenneth M. Anderson, 2007.