-
Name:_______________________
Covers Chapters 8-10
CSCI
1302 OO Programming
Armstrong Atlantic
State University
50 min
Instructor: Y. Daniel Liang
Part I. (2 pts each)
1.
What is wrong in the
following code?
class
Test {
public static void
main(String[] args) {
A a =
new
A();
();
}
}
class
A {
String s;
A(String s) {
this
.s = s;
}
public void
print() {
(s);
}
}
2.
What is the
printout of the following code?
public class
Foo {
private boolean
x;
public static void
main(String[] args) {
Foo foo = new Foo();
n(foo.x);
}
}
3.
Show the output of the following
program:
public
class
Test {
public static void
main(String[] args) {
public
class
Count {
public int
count;
Count myCount =
new
Count();
int
times =
0
;
Count(
int
c) {
count = c;
for
(
int
i =
0
; i <
100
; i++)
}
increment(myCount, times);
Count() {
n(
+ );
count =
1
;
n(
+ times);
}
}
}
public static
void
increment(Count c,
int
times) {
++;
times++;
}
}
1
4. Show the output of the following
program when invoked
using
java Test I have a dream
public class
Test {
public static
void
main(String[] args) {
n(
+ );
for
(
int
i =
0
; i < i++)
n(args[i]);
}
}
Part II:
a. (15)
(Geometry: The MyRectangle2D class)
Define the
MyRectangle2D class that
contains:
?
Two
double data fields named x and y that specify the
center of the rectangle with get and
set methods.
(Assume that the rectangle
sides are parallel to x- or
y- axes.)
?
The double data
fields width and height with get and
set methods.
?
A no-arg constructor that creates a
default rectangle
with (0, 0) for (x,
y) and 1 for both width and
height.
?
A constructor
that creates a rectangle with the
specified x, y, width, and height.
?
A method
getArea() that returns the area of the
rectangle.
?
A method getPerimeter() that returns
the perimeter of
the rectangle.
?
A method
contains(double x, double y) that returns
true if the specified point (x, y) is
inside this
rectangle. See Figure 1(a).
?
A method
contains(MyRectangle2D r) that returns true
if the specified rectangle is inside
this rectangle.
See Figure 1(b).
2
?
A method overlaps(MyRectangle2D r) that
returns true
if the specified rectangle
overlaps with this
rectangle. See
Figure 1(c).
p
(a) (b) (c)
Figure 1
(a) A point is
inside the rectangle. (b) A rectangle
is inside another rectangle. (c) A
rectangle overlaps
another rectangle.
Draw the UML diagram for
the class.
Implement the
method getArea(), getPerimeter(),
contains(double x, double y),
contains(MyRectangle2D
r), and
overlaps(MyRectangle2D r).
3
This page is left blank
intentionally.
4
2. (10 pts)
(
Occurrences of a specified
character
) Write
a method
that finds the number of occurrences of a
specified character in the string using
the following
header:
public static int
count(String str,
char
a)
For example,
count(
program that prompts the user to
enter a string
and a character and
displays the number of
occurrences of
the character in the string.
5
Part III:
Multiple Choice Questions: (1 pts each)
(Take the multiple-choice questions
online from LiveLab.
Log in and click
Take Instructor Assigned Quiz. You have to
take it before it is due. You have to
submit within the
specified time
interval.)
6
1.
Analyze the following
code:
public class Test {
private int t;
public static void main(String[]
args) {
int x;
n(t);
}
}
A.
The program compiles and runs fine.
B.
The variable t is private and therefore cannot be
accessed in the main method.
C. The
variable x is not initialized and therefore causes
errors.
D. t is non-static and it
cannot be referenced in a static context in the
main method.
E. The variable t is not
initialized and therefore causes errors.
Key:D
#
2.
________ is invoked to
create an object.
A. The main method
B. A method with a return type
C. A method with the void return type
D. A constructor
Key:D
#
3
. Given the declaration
Circle x = new Circle(), which of the following
statement is most
accurate.
A. x contains an object of the Circle
type.
B. You can assign an int value
to x.
C. x contains a reference to a
Circle object.
D. x contains an int
value.
Key:C
#
4.
Analyze the following
code.
public class Test {
int x;
public Test(String t) {
n(
}
public
static void main(String[] args) {
7