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

Due on Tuesday, January 27 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 me the file for part 2 in the format that I can file in.

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 to add the "location" from "temp2" to the one in "temp".
    4. Why does it fail? SmallInteger does not know how to do addition with a point object. The SmallInteger asks Point to do the addition by using conversion method adaptInteger: but Point does not understand this method.
      Add the "location" from "temp" to the one in "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? Why did it work 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 add "temp3" to 9 and 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
      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 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. DO NOT implement the other direction (i.e. Number + Triple) since this is the deadly problem I mentioned in class. 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. Bonus if you can write a storeOn: method that can be filed in and generate an object.

    Just to list all the methods to add:
    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
    

    Bonus:

  • Hint: The Internet and search engines have made trivia easy.http://us.imdb.com/
  • Name the book and author that inspired the movie, "The Adventures of Buckaroo Banzai Across the 8th Dimension ?" Thomas Pynchon's book "The Crying of Lot 49"
  • Who played Doctor Emilio Lizardo/Lord John Whorfin? John Lithgow
  • Name 2 movies for which this actor received Academy Awards nominations? Terms of Endearment (1983) (S:AAN) .... Sam Burns and World According to Garp, The (1982) (S:AAN) .... Roberta Muldoon
  • Finally lets play the closest path connecting names game which I explained in class. Give me the shortest path between Quentin Tarantino and Jack Nicholson?Quentin Tarantino to Harvey Keitel in "From Dusk 'til Dawn". Harvey Keitel to Jack Nicholson in "The Two Jakes" and Tarantino wrote "Natural Born Killers" which starred Julliette Lewis who was in "The Evening Star" with Jack
    Adam Jonathan Griff, computer@griffmonster.com
    Copyright © University of Colorado. All rights reserved.
    Revised: February 2, 1998