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

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?
  3. What is the name of my new class?
  4. What class is it derived from?
  5. How many constructors does this class have and what are they?
  6. What is the instance variable and what methods effect it?
  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?
      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?
      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?
      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.
      Now "location" from "temp" + "location" from "temp2" and store the result into "temp3".
    5. What class is "temp3" an instance of?
    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.
      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
  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.

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.


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