![]() |
|
Write your name or username here:______________________
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 example2. (10 points) Assume the code fragment below:In any event the law we're using is this:!(x <= 10 || ! (x > 5))So that gives!(a || b) == (!a && !b)which in turn gives!(x <= 10) && !(x <= 5))which givesx > 10 && x > 5which is the answer we were looking for.x > 10
Does anything change in the wayif (x > y) z = z + 1; else if (x <= y) z = z + 2;
z
gets updated
if we remove the else
keyword?
The second
That is, it's
Also, changing Therefore the answer is NO. This question has been discussed
in lecture.
3. (10 points)
Assume the code fragment below:
if
statement is redundant. else
(not shown) will never be run. z
does not affect x
and
y
.
Does anything change in the wayif (! (x > y)) z = z + 2; else z = z + 1;
z
gets updated
if we remove the else
keyword?
If I remove the
That means the
The answer this time is: YES.
4. (10 points)
Consider the following nested if statement. else
the second assignment
becomes unconditional. if
statement becomes:
So we see that the if (! (x > y)) {
z = z + 2;
z = z + 1;
} else {
z = z + 1;
}
then
branch changes.
Ifif (x > 3) if (x <= 5) y = 1; else if (x != 6) y = 2; else y = 3; else y = 4;
y
has the value 2
after executing the above program fragment
what do you know about x
?
The answer is
There are four paths through the program and and only one of
them results in x >= 7
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
Ifif (x > 3) { if (x <= 5) y = 1; else if (x != 6) y = 2; } else y = 3;
x
is 1
before the fragment gets executed
what's the value of y
after the fragment is executed?
Because of the curly braces we immediately reach
the
But watch out for
This question is extremely easy.
6. (10 points) Now erase the curly braces.
y = 3;
statement.
As we shall see, x
we reach the same statement
x < 3
no longer takes us there.
What value mustif (x > 3) if (x <= 5) y = 1; else if (x != 6) y = 2; else y = 3;
x
have before the fragment gets
executed, for y
to be 3
at the end
of the fragment?
If
All the other paths set
For
That means
Which makes the final answer
Quick question: what's the value of
The answer is that
7. (10 points) Consider the following x
must be 6
. x
is less than or equal to 3
then we
don't know what y
ends up as. y
to a different value. y
to be
3
we need
x
strictly greater than 3
,
!(x <= 5)
,
!(x != 6)
as well.
(x >3 )
and (x > 5)
and x == 6
. x == 6
. (x > 5 && x < 3)
?
Rectangle
class.
Add a method that computes theclass 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; } }
area()
of a Rectangle
.
8. (10 points) To the class above add a method that determines if aclass 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; } }
Point
is inside or outside of a
Rectangle
. Here's
an example for Circle
s (to give you a model,
should you need any):
Assumeclass 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; } }
Point
as developed in class:
Essentially we need to check forclass 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); } }
x
and y
to fall within the given range. So we could have:
9. (20 points) Redefine classclass 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; } } }
Circle
so it has two instance
variables inside: Point
(for the center
), and
double
) radius
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; double radius; boolean inside(Point target) { if (target.distanceTo(center) >= radius) return false; else return true; } Circle(Point center, double radius) { this.center = center; this.radius = radius; } void move(Point newLocation) { this.center = newLocation; } double area() { return Math.PI * radius * radius; } } 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