Operations Test

  1. As discussed in class there are three ways to get an object reference, and three things I can do with an object reference.

    I can get an object reference by:
    1. Retrieving the value of a variable
    2. Creating a new object
    3. Asking another object to return it to me

    Once I have an object reference, I can:
    1. Store it in a variable
    2. Send a message to that address
    3. Pass that reference as a parameter in a message to another object.

    So, for example:

    Point p = new Point(2, 6); // 2A
    bob.setColor(new Color(255, 100, 0)); // 1B, 2C

    In the first line above, I obtained an object reference by creating an object, then stored that reference in a variable. So, I labeled that line 2A. In the second line, there are two references I deal with: first I retrieve the value of bob and send a message to it (1B), then I create a Color and pass its reference to bob along with the message.

    Label the following lines in the same way:

    Turtle fred = new Turtle();


    (new Color()).getRed();

    Turtle tom = fred;


    Color c = fred.getColor();


  2. Suppose that I have two Point objects, referred to by variables p1 and p2. I don't know what their x and y coordinates are; the picture to the right is just an example of what they might look like.

    These two Point objects can be treated as the corners of a rectangle, as shown in the picture. Write some code that will use p1 and p2 to calculate, and store into variables, the area and perimeter of that rectangle.

    Make sure that the values you calculate will come out positive, even though the points could be in any orientation relative to each other. Here are the signatures of three method of Math that you may find helpful:

    public double abs(double a)
    public double min(double a, double b)
    public double max(double a, double b)

  3. Write a bit of code that will provide a dramatic reenactment of a gunfight, using Turtles to represent the two cowboys wyatt and ike. They turn to face in opposite directions (East and West), each walk a distance of 5, and then turn to face each other. You can decide who turns first and thus is quicker on the draw.

    Remember, all Turtles when they are created are at the center of the canvas, facing North. Also, just in case you forgot, here are some methods and constructors of Turtle:

    // Constructors
    public Turtle()

    // Methods
    public void rt();
    public void rt(double degrees);
    public void lt();
    public void lt(double degrees);
    public void move();
    public void move(double distance);

  4. Read through this code, and tell me what p and q are equal to at the end.

    Point p, q;

    p = new Point(Math.sqrt(3), 3);

    double d = p.getX() * p.getX();

    new Point(d, -d);

    q = p;

    d = Math.abs(p.getY() - q.getY() + 4);


    q = new Point(d, Math.sqrt(d));

    p = new Point(q.getY(), q.getX());

    d = p.getX() + p.getY();

    q = new Point(d * q.getX(), d * q.getY());

    At this point, what are the (x, y) coordinates of the two points?

    p =

    q =

  5. Suppose that a class Test has the following methods and constructors:

    // Constructors
    public Test()
    public Test(Test t)

    // Methods
    public Test getYourFriend()
    public double getTheThing(double a)
    public void doSomething(double a, double b)
    public double getTheOtherThing()

    Read through the following lines. If there is an error in a line, explain what the error is. Not all lines have errors. The first two are examples.

    Test bob = new Test(3); // Wrong type of parameter
    bob.getTheThing().getTheOtherThing(); // Can't send a message to a double

    bob.getYourFriend().doSomething(2, 3);

    Test friend = bob.getYourFriend(5);

    double d = friend.getTheThing(3);

    friend.doSomething(d, bob.getTheThing(2));

    (new Test()).doSomething(2 * 3, bob);

    bob = new Test(bob.getYourFriend());

    bob.getTheThing(fred.doSomething(2, 1));