CS 2135: Lab 6


Lab Motivation and Goals


Exercises

Do this lab in the Advanced Language Level

We are interested in manipulating circles. A circle is defined by its radius (a number). We want to have two operations on circles: area, which returns the area of a circle and resize, which consumes a number and changes the radius of a circle to that number.

  1. Create an object for a circle with radius 5. Implement objects as functions as we did in class on Tuesday. Do not use the classes and object constructs built into MzScheme.

  2. Did you implement resize using set!? Either explain why you did not use set! or explain why you are justified in using set! to implement resize.

  3. Test your circle object. Make sure that if you resize the circle, that the area is computed on the new radius, rather than the old radius.

  4. Create another circle object, this one with radius 8.

  5. Using the two circle objects as a guide, define a function (class) for making circle objects. Test your function by creating two circles with different radii. Make sure that you can resize one circle without affecting the radius of the other circle.

  6. Augment your circle class with a center location and a a draw operation that draws a circle on the screen. For the location, use the (make-posn x y) structure built into DrScheme. Implement your drawing function using DrScheme's graphics library. To use the library, set your teachpack to draw.ss (under the Language menu) and include the line (start 300 300) in your definitions window (this initializes a drawing canvas).

    The two drawing functions you will need are:

  7. Add an operation shrink-away to your circle class. This operation should draw a series of successively smaller circles at the same center point until the circle "disappears" (has radius zero) -- when you run the program, it should look like the circle is shrinking in place.

    Your solution should support the following sequence of operations (ie, it should not change the original radius):

    > (define c1 (make-circle 10))
    > ((c1 'area))
    #i314.1592653589793
    > ((c1 'shrink-away))
    > ((c1 'area))
    #i314.1592653589793
    

    Make sure you can justify all uses of set! in your shrink-away code.

  8. Add an operator max-radius to your circle class. It should consume a circle and return a number. The returned number should be the larger of the radius of the circle object and the input circle.

  9. If you finish all of the above exercises, consider how you could have create a circle object with the original and graphics operations without modifying the original (ie, figure out how to implement inheritence).