This is a makeup of the first midterm exam. You have two hours (120 minutes) to complete the test. The test is open-book.

Directions:

1. (10 points) Use De Morgan's law to simplify the following Boolean expression.

`!(x <= 10 || x <= 5)`
A better form for this would have been, for example
`!(x <= 10 || ! (x > 5))`
In any event the law we're using is this:
`!(a || b) == (!a && !b)`
So that gives
`!(x <= 10) && !(x <= 5))`
which in turn gives
`x > 10 && x > 5`
which gives
`x > 10`
which is the answer we were looking for.
2. (10 points) Assume the code fragment below:
`if (x > y) z = z + 1; else if (x <= y) z = z + 2;`
Does anything change in the way `z` gets updated if we remove the `else` keyword?

This question has been discussed in lecture.

The second `if` statement is redundant.

That is, it's `else` (not shown) will never be run.

Also, changing `z` does not affect `x` and `y`.

3. (10 points) Assume the code fragment below:
`if (! (x > y)) z = z + 2; else z = z + 1;`
Does anything change in the way `z` gets updated if we remove the `else` keyword?

The answer this time is: YES.

If I remove the `else` the second assignment becomes unconditional.

That means the `if` statement becomes:

```if (! (x > y)) {
z = z + 2;
z = z + 1;
} else {
z = z + 1;
} ```
So we see that the `then` branch changes.

4. (10 points) Consider the following nested if statement.

```if (x > 3) if (x <= 5) y = 1;
else if (x != 6) y = 2;
else y = 3;
else y = 4;```
If `y` has the value `2` after executing the above program fragment what do you know about `x`?

`x >= 7`
There are four paths through the program and and only one of them results in `y` getting a value of `2`. To follow that path we need `x > 3` and `!(x <= 5)` and `x != 6` and we know that `x` is an integer. So `x` must be strictly bigger than `3` and `x` must be strictly bigger than `5` and `x` is not `6`.

For the next two questions assume that `x` and `y` are `int`eger variables.

5. (10 points) Consider the following code fragment

```if (x > 3) { if (x <= 5) y = 1;
else if (x != 6) y = 2;
}
else y = 3;```
If `x` is `1` before the fragment gets executed what's the value of `y` after the fragment is executed?

This question is extremely easy.

Because of the curly braces we immediately reach the `y = 3;` statement.

But watch out for

• what values of `x` we reach the same statement
• without curly braces (in the next question).
As we shall see, `x < 3` no longer takes us there.

6. (10 points) Now erase the curly braces.
```if (x > 3) if (x <= 5) y = 1;
else if (x != 6) y = 2;
else y = 3;```
What value must `x` have before the fragment gets executed, for `y` to be `3` at the end of the fragment?

The answer is that `x` must be `6`.

If `x` is less than or equal to `3` then we don't know what `y` ends up as.

All the other paths set `y` to a different value.

For `y` to be `3` we need

• an `x` strictly greater than `3`,
• such that `!(x <= 5)`,
• and such that `!(x != 6)` as well.

That means `(x >3 )` and `(x > 5)` and `x == 6`.

Which makes the final answer `x == 6`.

Quick question: what's the value of `(x > 5 && x < 3)`?

7. (10 points) Consider the following `Rectangle` class.

```class Rectangle{ double x, y, w, h;
Rectangle (double x, double y, double w, double h) {
this.x = x; this.y = y; this.w = w; this.h = h;
}
} ```
Add a method that computes the `area()` of a `Rectangle`.

```class Rectangle{
double x, y, w, h;
Rectangle (double x, double y, double w, double h) {
this.x = x; this.y = y; this.w = w; this.h = h;
}
double area() {
return this.w * this.h;
}
} ```
8. (10 points) To the class above add a method that determines if a `Point` is inside or outside of a `Rectangle`. Here's an example for `Circle`s (to give you a model, should you need any):

```class Circle { double x, y, radius;
boolean inside(Point target) {
Point center = new Point(x, y);
if (target.distanceTo(center) >= radius) return false;
else return true;
}
} ```
Assume `Point` as developed in class:
```class Point {
double x, y;
Point(double a, double b) { this.x = a; this.y = b; }
double distanceTo(Point other) {
double dX = this.x - other.x,
dY = this.y - other.y;
return Math.sqrt( dX * dX + dY * dY);
}
}```
Essentially we need to check for `x` and `y` to fall within the given range.

So we could have:

```class Rectangle{
double x, y, w, h;
Rectangle (double x, double y, double w, double h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
double area() {
return this.w * this.h;
}
boolean inside(Point a) {
if (a.x <= this.x && a.x <= (this.x + this.w) &&
a.y <= this.y && a.y <= (this.y + this.h)) {
return true;
} else {
return false;
}
}
} ```
9. (20 points) Redefine class `Circle` so it has two instance variables inside:
1. a `Point` (for the `center`), and
2. a (`double`) `radius`
Provide at least a constructor. Add a method to calculate the `area()` of the circle. Add a method to move the `Circle` to a new location. Write a small test `main` program that shows how you would be using your `Circle` objects.

```class Circle {
Point center;
boolean inside(Point target) {
return false;
else
return true;
}
this.center = center;
}
void move(Point newLocation) {
this.center = newLocation;
}
double area() {
}
}

class Experiment {
public static void main(String[] args) {
Circle a = new Circle(new Point(2, 3), 5);
System.out.println(a.area());
Point b = new Point(10, 10);
System.out.println(a.inside(b));
a.move(9, 9);
System.out.println(a.inside(b));
}
}```

`Morrison Hall 007 7-9pm Feb 26 2003 A201/A597/I210 Midterm Exam One `