# Notes on Assignment 2

Rather than copying the whole set of instructions, I've just included the answers for each section. Note that I'm using the revised versions of these labs, which are slightly different from the ones that you received at the beginning of the semester.

I'm probably a little bit faster at this stuff than you folks, but not much (since most of this is copying and running). It took me about an hour of repetitious work. I apologize for the repetition, but I do think that it serves some purposes. I hope you found some interesting things in the lab.

## O2

### O2.1

Step 1.

```(2.0,3.0)
distance from origin: 3.605551275463989
Shifting right by 0.7
(2.7,3.0)
distance from origin: 4.036087214122113
Shifting up by 2.5
(2.7,5.5)
distance from origin: 6.126989472816156
Shifting left by 10.2
(-7.499999999999999,5.5)
distance from origin: 9.300537618869136
```

Step 2.

```(0.0,0.0)
distance from origin: 0.0
Shifting right by 0.7
(0.7,0.0)
distance from origin: 0.7
Shifting up by 2.5
(0.7,2.5)
distance from origin: 2.596150997149434
Shifting left by 10.2
(-9.5,2.5)
distance from origin: 9.82344135219425
```

By default, the point starts at (0,0).

Step 3.

Added my name to the author list.

Changed the version and date. Updated the `import` statement. (No, I didn't expect you to list these.)

Changed the class name and ending comment.

```Point pt = new Point();
```

```NewPoint pt = new Point();
```

Step 4.

```(0.0,0.0)
distance from origin: 0.0
Shifting right by 0.7
(0.7,0.0)
distance from origin: 0.7
Shifting up by 2.5
(0.7,2.5)
distance from origin: 2.596150997149434
Shifting left by 10.2
(-9.5,2.5)
distance from origin: 9.82344135219425
```

The output appears to be the same.

Step 5.

```(1.0,1.0)
distance from origin: 1.4142135623730951
Shifting right by 0.7
(1.7,1.0)
distance from origin: 1.972308292331602
Shifting up by 2.5
(1.7,3.5)
distance from origin: 3.891015291668744
Shifting left by 10.2
(-8.5,3.5)
distance from origin: 9.192388155425117
```

Because the zero-parameter constructor of `NewPoint` uses the zero-parameter constructor of `Point` to create the ``base'' object, updates to the constructor for `Point` affect `NewPoint`.

Step 6.

```    base = new Point();
```

with

```    base = new Point(0,0);
```

That seemed to be enough.

Step 7.

Even without inheritance, it is possible to base one object upon another; it's just somewhat time-consuming to do so.

### O2.2

Step 1.

```(2.0,3.0)
distance from origin: 3.605551275463989
Shifting right by 0.7
(2.7,3.0)
distance from origin: 4.036087214122113
Shifting up by 2.5
(2.7,5.5)
distance from origin: 6.126989472816156
Shifting left by 10.2
(-7.499999999999999,5.5)
distance from origin: 9.300537618869136
```

Step 2.

Done. No error messages.

Step 3.

Updated the `import` statement.

```Point pt = new Point(2,3);
```

with

```ExtendedPoint pt = new ExtendedPoint(2,3);
```

Step 4.

```PointFun.java:17: Wrong number of arguments in constructor.
ExtendedPoint pt = new ExtendedPoint(2,3);
^
1 error
```

We're trying to use a two-parameter constructor, and we haven't defined one.

Step 5.

```(0.0,0.0)
distance from origin: 0.0
Shifting right by 0.7
(0.7,0.0)
distance from origin: 0.7
Shifting up by 2.5
(0.7,2.5)
distance from origin: 2.596150997149434
Shifting left by 10.2
(-9.5,2.5)
```

If we use the default constructor for a subclass, then we end up with the default constructor for the superclass.

Step 6.

```PointFun.java:17: No constructor matching ExtendedPoint() found in class ExtendedPoint.
ExtendedPoint pt = new ExtendedPoint();
1 error
```

We just created a two-parameter constructor. Java doesn't let us use the ``default'' zero-parameter constructor if there is another constructor. Since we haven't created a zero-parameter constructor, the compiler is unhappy with us.

Step 7.

It compiled successfully.

```(1.0,1.0)
distance from origin: 1.4142135623730951
Shifting right by 0.7
(1.7,1.0)
distance from origin: 1.972308292331602
Shifting up by 2.5
(1.7,3.5)
distance from origin: 3.891015291668744
Shifting left by 10.2
(-8.5,3.5)
distance from origin: 9.192388155425117
```

Although the default position for `Point`s is (0,0), the default position for `ExtendedPoint`s is (1,1). This means that you can give different defaults in subclasses.

Step 8.

```(4.0,1.0)
distance from origin: 4.123105625617661
Shifting right by 0.7
(4.7,1.0)
distance from origin: 4.805205510693586
Shifting up by 2.5
(4.7,3.5)
distance from origin: 5.860034129593445
Shifting left by 10.2
(-5.499999999999999,3.5)
distance from origin: 6.519202405202647
```

We can also use the two-parameter constructor. However, we also note that Java has some trouble with simple mathematics (using 5.49999... instead of 5.5).

Step 9.

It still compiles and executes. The output is still the same. This means that `super(x,y)` has a similar effect to `setValue(x,y)`. As we know from the discussion, this is because we're calling the corresponding constructor of the superclass.

### O2.3

Step 1.

```(2.0,3.0)
distance from origin: 3.605551275463989
Shifting right by 0.7
(2.7,3.0)
distance from origin: 4.036087214122113
Shifting up by 2.5
(2.7,5.5)
distance from origin: 6.126989472816156
Shifting left by 10.2
(-7.499999999999999,5.5)
distance from origin: 9.300537618869136
```

Step 2.

The results are the same as in the previous step. That's what I expected (more or less), since I didn't use the `toString` method.

Step 3.

```x=2.0, y=3.0
distance from origin: 3.605551275463989
Shifting right by 0.7
x=2.7, y=3.0
distance from origin: 4.036087214122113
Shifting up by 2.5
x=2.7, y=5.5
distance from origin: 6.126989472816156
Shifting left by 10.2
x=-7.499999999999999, y=5.5
distance from origin: 9.300537618869136
```

This seems to work fine (although the output is slightly different since we've chosen to write ``x=...; y =...'' rather than ``(...,...)''.

Step 4.

```X:2.0, Y:3.0
distance from origin: 3.605551275463989
Shifting right by 0.7
X:2.7, Y:3.0
distance from origin: 4.036087214122113
Shifting up by 2.5
X:2.7, Y:5.5
distance from origin: 6.126989472816156
Shifting left by 10.2
X:-7.499999999999999, Y:5.5
distance from origin: 9.300537618869136
```

It seems a little bit odd to be using `this.xcoord`, given that I haven't defined a field named `xcoord`. However, I understand that I inherit this field from `Point`.

Step 5.

It seems a little bit more efficient to use the fields directly, since you don't have an intervening method call. However, if a subclass uses the fields directly and the superclass later changes the names of the fields (e.g., `xcoord` to `xCoord`, then the subclass will stop working.

### O2.4

Step 1.

```(2.0,3.0)
distance from origin: 3.605551275463989
Shifting right by 0.7
(2.7,3.0)
distance from origin: 4.036087214122113
Shifting up by 2.5
(2.7,5.5)
distance from origin: 6.126989472816156
Shifting left by 10.2
(-7.499999999999999,5.5)
distance from origin: 9.300537618869136
```

Looks the same as it has in the past few experiments. This is getting somewhat repetitious :-).

Step 2.

The `distanceFromOrigin` method has changed (it looks a little bit simpler). It was the square root of the sum of the squares of the x and y coordinates in `Point`. Now it's the sum of the x and y coordinates.

Step 3.

```(2.0,3.0)
distance from origin: 5.0
Shifting right by 0.7
(2.7,3.0)
distance from origin: 5.7
Shifting up by 2.5
(2.7,5.5)
distance from origin: 8.2
Shifting left by 10.2
(-7.499999999999999,5.5)
distance from origin: -1.9999999999999991
```

Those distances are certainly different.

## O3

### O3.2

Step 1.

```(2.0,3.0)
distance from origin: 3.605551275463989
Shifting right by 0.7
(2.7,3.0)
distance from origin: 4.036087214122113
Shifting up by 2.5
(2.7,5.5)
distance from origin: 6.126989472816156
Shifting left by 10.2
(-7.499999999999999,5.5)
distance from origin: 9.300537618869136
```

Still looks the same.

Step 2.

Updated the `import` statement. (Also added one for `PointPrinter` which I'd forgotten to add in the previous step. It's good that Java is casual about such things.)

```    Point pt = new Point(2,3);
```

```    NewPoint pt = new NewPoint(2,3);
```

Step 3.

Java complains that it can't convert `NewPoint`s to `Points` in all the lines that read

```    printer.print(out,pt);
```

It's interesting that in the previous experiment (O2), we were able to change all the instances without receiving any error messages but we were not able to do so here. What's the difference? This time, we're calling a method using `pt` as a parameter.

Step 4.

```  /**
* Print a point using a particular output object.
*/
public void print(SimpleOutput out, NewPoint pt) {
out.println("(" + pt.getX() + "," + pt.getY() + ")");
out.println("  distance from origin: " +
pt.distanceFromOrigin());
} // print(SimpleOutput, NewPoint)
```

Oh the wonders of overloading! Now I have two methods with the same name that do basically the same thing.

Step 5.

Successful. We needed a `print` method that took a `NewPoint` as a parameter. There wasn't one in step 3, but we added one in step 4.

Step 6.

```(2.0,3.0)
distance from origin: 3.605551275463989
Shifting right by 0.7
(2.7,3.0)
distance from origin: 4.036087214122113
Shifting up by 2.5
(2.7,5.5)
distance from origin: 6.126989472816156
Shifting left by 10.2
(-7.499999999999999,5.5)
```

Looks okay.

Step 7.

It was somewhat painful to have to make a copy of the `print` method, given that `NewPoint` provides exactly the same methods as `Point`. Can't Java figure that out?

### O3.3

Step 1.

```(2.0,3.0)
distance from origin: 3.605551275463989
Shifting right by 0.7
(2.7,3.0)
distance from origin: 4.036087214122113
Shifting up by 2.5
(2.7,5.5)
distance from origin: 6.126989472816156
Shifting left by 10.2
(-7.499999999999999,5.5)
distance from origin: 9.300537618869136
```

This is getting repetitious.

Step 2.

Changed the `import` statement.

```    Point pt = new Point(2,3);
```

```    ExtendedPoint pt = new ExtendedPoint(2,3);
```

Step 3.

It compiled, even though it didn't do so in the previous experiment, and it seems that we did something very similar (replacing `Point` with something that provides the same methods as `Point`).

Step 4.

It seems to be a lot easier to use a subclass than something that just mimics the original class.

## History

Sunday, 19 September 1999

• Created.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.