CS 4448 - Fall 1998
Object-Oriented Programming and Design
Homework #2 - A Solution

Due on Monday, September 14 at the start of class.

This assignment must be typed (handwritten homework will not be graded)
Print out Part 1 and Part 2 with some example tests that you ran on Part2. Some tests means each function is demonstrated but I do not want pages of tests. Also email the TA a gzipped file with the file for part 2 in the format that Squeak can file in. Squeak files must be gzipped since they have some special characters that get corrupted when emailed.

Part 1 - AJG class

  1. Use the file hw2.example.st located here or on the ugrad computers in directory (~griff/CS4448 or /nfs/home/cia/griff/CS4448) and file it in.
  2. What category is my class in? AJG-HW2-Example
  3. What is the name of my new class? GeomFigure
  4. What class is it derived from? Object
  5. How many constructors does this class have and what are they? 2 and they are "new" and "at:"
  6. What is the instance variable and what methods effect it? "location" is the instance variable and it can be read using the method "location" and modified using the "at:" method.
  7. Now it is time to use the class. So open a Workspace and do the following commands in order and make sure to answer the questions in order as well. Note: If you get an error then you are not using the classes correctly, so keep working on it. Also, use print-it (not do-it) so we can see the results. Note: you will be copying your workspace when you are done into your homework.

      Create a new instance with the instance variable uninitialized and store the class in variable "temp".
    1. Before doing anything else inspect the variable "temp" and write down what is in "location" and what is the class of "location" which you get by inspecting it? nil which is in the class UndefinedObject
      Change the location in "temp" to the value 5@1
    2. Now look at "temp", what class is "location" an instance of and what instance variable does it have? Point with instance variable x and y
      Create a new instance where location gets initialized at instantiation to 3. Store the class to "temp2".
    3. So in "temp2" what class does "location" belong to? SmallInteger which is a primitive
      Try "location" from "temp2" + "location" from "temp".
    4. Why does this work? Note: With the old version of Squeak this did not work. You must look at the code to see why this works for this case and the next case with the variable order reversed. Hint: Try "location" from "temp2" + string 'foo' this one will not work and might help you understand the code. SmallInteger inherits the + method from the Integer class. This method send the method isInteger to the object passed in with the binary method +. It responds with the an instance of the object false. Now the + method does not know how to do addition with none Integer and their subclass objects. The SmallInteger asks parameter object, which is a Point, to convert itself using the method adaptInteger:. The + method is then called on the result of adaptInteger: with the result of Point parameter called with the method adaptToInteger. So, in the end the addition is actually being performed by the Point class. Well, that is true but the instances, x and y, of Point have there addition handled by the class to which they belong.
      Now "location" from "temp" + "location" from "temp2" and store the result into "temp3".
    5. What class is "temp3" an instance of? Point
    6. Explain the numerical result of the math operation, how was the math performed to get that result? How did it work differently this time? Give the details of the process which will require you to check out the class in the browser.
      The add method was called on the Point class and it asked SmallInteger to become a point using asPoint. SmallInteger understands this method via inheritance from the Number class. SmallInteger 3 was converted to a Point with value 3@3 and then Point arithmetic was performed.
      Finally "temp3" + 9 + the "location" of "temp" and store the result into the existing "temp2" object "location" variable.
    7. Paste your workspace for this problem here
      temp _ GeomFigure new. a GeomFigure
      temp at: 5@1. 5@1
      temp2 _ GeomFigure at: 3. a GeomFigure
      temp2 location + temp location. 8@4
      temp3 _ temp location + temp2 location. 8@4
      temp2 at: temp3  + 9 + temp location. 22@14
      
  8. Find the example of a polymorphic interface in this class and give two ways they are different? I do not want you to discuss class to base class polymorphic interfaces, stay within the new code that was added. One "at:" is used for instance creation and returns an object of type GeomFigure while the other "at:" is an instance method and sets and returns "location". Also the instance creation class method calls the instance method to get its job done.

Part 2 - Create your own class

Remember that in Squeak categories for class and methods are just for organization and have no effects. Try to place the methods in nice categories like you see in the other classes.
  • Create a new category called HW#2 and place the new object class called Triple in that category. The Triple class should have base class Point. Add the instance variable z to your new class and methods z and z: . Also add instance method "setX: setY: setZ:" and class method "x: y: z:". You will need to implement printOn: and storeOn: in the class so the object can display itself properly. Implement a way so that Points can be converted to Triples via the @ method but Triples should then undefine the @ method. Implement the methods for operator + and -. + and - needs to work for Triple + Triple, Triple + Point, and Triple + Number. You do not have to implement the other direction (i.e. Number + Triple) but it is pretty easy. Do min:, max:, and rounded so they perform similar tasks that you can SEE in the Point class. Add the conversions asTriple so it is understood by the Number class, Point class, and Triple class. Add asPoint to the Triple class isnce this will help in your implementation (hint + and -). Note: reuse code of the parent classes. The storeOn: method writes out the instance in a format that can then be read back in and regenerate the object with its state. The storeOn: method is used by the system and you will find it hard to test, that is okay.

    A check list of all the methods you have added:
    Number class - asTriple
    Point class - @, asTriple
    Triple class - class method x: y: z:, setX: setY: setZ:, printOn:, storeOn:, z, z:, min:, max:, rounded, @, asPoint, asTriple, +, -

    One final important note. You can see how inheritance works since printOn:, min:, @, and other methods will work even if you don't do them in the Triple class but they don't return the desired result.
    Example code here

    Sample output

    n1 _ 3. 3
    n1 asTriple. 3@3@3
    p1 _ Point x: 1 y: 2. 1@2
    p1 @ 3. 1@2@3
    p1 asTriple. 1@2@0
    t1 _ Triple new. nil@nil@nil
    t1 setX: 5 setY: 7 setZ: 11. 5@7@11
    t2 _ Triple x:1 y:13 z:3. 1@13@3
    t2 z. 3
    t2 z: 6. 1@13@6
    t1 min: t2. 1@7@6
    t1 max: t2. 5@13@11
    t3 _ 9.7 @  3.1 @ 7.4. 9.7@3.1@7.4
    t4 _ Triple x: 1.2 y:(2.3@4.3) z: (2@3@8.7). 1.2@2.3@4.3@2@3@8.7
    t4 rounded. 1@2@4@2@3@9
    t1 asPoint. 5@7
    t1 asTriple. 5@7@11
    t4 asPoint. 1.2@2.3@4.3
    t4 asTriple. 1.2@2.3@4.3@2@3@8.7
    t1 @ 2. "error in a window - This message is not appropriate for this object"
    t1 + n1. 8@10@14
    t1 + p1. 6@9@11
    t1 + t2. 6@20@17
    t1 - n1. 2@4@8
    t1 - p1. 4@5@11
    t1 - t2. 4@-6@5
    "Interesting Additional Tests that don't do what you want or might expect"
    n1 + t1. 8@10       "adaptInteger: and adaptToInteger are inherited from Point"
    n1 * t1. 15@21
    t1 * t2. 5@91       "The * method is comming from the Point class and ignores the z instance and 			 				even returns back a Point."
    "Result after modifying the adapt methods in the Triple class" 
    n1 + t1. 8@10@14
    n1 * t1. 15@21
    t1 * t2. 5@91
    

    Adam Jonathan Griff, computer@griffmonster.com
    Copyright © University of Colorado. All rights reserved.
    Revised: September 12, 1998