IC211: Object-Oriented Programming Spring AY2015 — 6-Week Exam

advertisement
IC211: Object-Oriented Programming
Spring AY2015 — 6-Week Exam
Individual work.
Closed book. Closed notes.
You may not use any electronic device.
Your answers must be legible to receive credit.
On the front of every sheet, legibly write your
Name: _____________________________________, Alpha: ___________, Section Number: _________
There are 115 points on this exam!
ASCII Table for Printable Characters
Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char
32 20
46 2e .
60 3c <
74 4a J
88 58 X
33 21 !
47 2f /
61 3d =
75 4b K
89 59 Y
34 22 "
48 30 0
62 3e >
76 4c L
90 5a Z
35 23 #
49 31 1
63 3f ?
77 4d M
91 5b [
36 24 $
50 32 2
64 40 @
78 4e N
92 5c \
37 25 %
51 33 3
65 41 A
79 4f O
93 5d ]
38 26 &
52 34 4
66 42 B
80 50 P
94 5e ^
39 27 '
53 35 5
67 43 C
81 51 Q
95 5f _
40 28 (
54 36 6
68 44 D
82 52 R
96 60 `
41 29 )
55 37 7
69 45 E
83 53 S
97 61 a
42 2a *
56 38 8
70 46 F
84 54 T
98 62 b
43 2b +
57 39 9
71 47 G
85 55 U
99 63 c
44 2c ,
58 3a :
72 48 H
86 56 V
100 64 d
45 2d 59 3b ;
73 49 I
87 57 W
101 65 e
Dec Hex Char
102 66 f
103 67 g
104 68 h
105 69 i
106 6a j
107 6b k
108 6c l
109 6d m
110 6e n
111 6f o
112 70 p
113 71 q
114 72 r
115 73 s
Dec Hex Char
116 74 t
117 75 u
118 76 v
119 77 w
120 78 x
121 79 y
122 7a z
123 7b {
124 7c |
125 7d }
126 7e ~
name: ________________________________________ alpha: _________________ page 1
1. [9pts] Consider the simple Java program:
public class Hiya
{
public static void main(String[] args)
{
System.out.println("Hiya");
}
}
a. What name would you give the file containing this source code?
b. What shell command would you give to compile this program?
c. What shell command would you give to run this program?
2. [6pts] Is Java a compiled or interpreted language? Explain your answer thoroughly!
3. [18pts] Consider the class Rectangle defined below:
public class Rectangle
{
private double height, width;
public Rectangle(int h, int w)
{
height = h;
width = w;
}
}
public boolean similar(Rectangle r)
{
return width*r.height == r.width*height;
}
public static boolean similar(Rectangle r1, Rectangle r2)
{
return r1.width*r2.height == r2.width*r1.height;
}
a. Assuming we are outside of the class Rectangle, show how to create Rectangle instances A
with width=5, height=2 and B with width=1, height=12.
b. Assuming we are outside of the class Rectangle, show how to call the first version of
similar to determine whether A and B are similar.
c. Assuming we are outside of the class Rectangle, show how to call the second version of
similar to determine whether A and B are similar.
name: ________________________________________ alpha: _________________ page 2
4. [18pts] Consider the following class definitions. Mark each of the expressions below as: "OK",
"access error", or "syntax error". Note that if there's a syntax error, there cannot be an
access error. I.e. until the syntax is correct, you cannot even talk about whether access is
allowed.
Steve.java
Bob.java
Joe.java
public class Steve
public class Bob
public class Joe
{
{
{
public static void run() private static int c = 0; private static String in = "yo";
{
public int a;
private String s;
Joe x = new Joe(1.0);
private void reset()
public double x;
Bob y = new Bob();
{
public Joe(double z)
// <-- A
a = 0;
{
c++;
s = in;
}
}
x = z;
}
public static Bob make()
}
{
public String get()
Bob b = new Bob();
{
b.a = c;
s = s + x;
return b;
return s;
}
}
public int get(Joe j)
public static void doit(Bob b)
{
{
// <--- B
// <--- C
}
return a;
}
}
}
at location A
at location B
at location C
a. Bob.c
d. get()
g. in
______
______
______
b. y.get(x) ______
e. reset() ______
h. Bob.make() ______
c. make()
f. make() ______
i. s
______
______
name: ________________________________________ alpha: _________________ page 3
5. [16pts] Consider the class Pos defined below:
public class Pos
{
private int row, col;
public Pos(int r, int c)
{
row = r; col = c;
}
}
public void reflect()
{
int t = row; row = col; col = t;
}
I would like to add a method dist to the class Pos that would allow one to calculate the Manhattan
distance between two Pos objects, which is the absolute value of the difference in row
coordinates plus the absolute value of the difference in column coordinates. Note: you want
Math.abs( ).
a. Define a dist as a non-static method. (Note: I mean body as well as prototype!).
b. Define a dist as a static method. (Note: I mean body as well as prototype!).
6. [6pts] Continuing with the same class Pos as above, suppose I have (in another class) a
function whose definition starts like this:
void foo(Pos p)
{
p.reflect();
...
If I make the function call foo(q) with argument q having row=3,col=4, when the function call
returns will the Pos referred to by q have row=4,col=3 or will it still have row=3,col=4?
Explain!
name: ________________________________________ alpha: _________________ page 4
7. [24pts] Consider the following class definitions.
Feline.java
Cat.java
Tabby.java
public class Feline
public class Cat
public class Tabby extends Cat
{
{
{
public static void main(String[] args) public static String eat()
public static String eat()
{
{
{
Cat c = new Cat();
return "herbivore";
return "fancy feast";
Tabby t = new Tabby();
}
}
// <-- A
public String sound()
public String sound()
}
{
{
}
return "roar";
return "meow";
}
}
public String sound(char c) public String activity()
{
{
return "purrrrr" + c;
return "sleep";
}
}
}
public void stuff()
{
//<--- B
}
}
For each of the following expressions, give the String that would be returned, or "error" if there
is a syntax error in the expression:
at location A
at location B
a. c.sound()
______
e. sound()
______
b. c.sound('!') ______
f. sound('!')
______
c. c.activity() ______
g. activity()
______
d. t.sound()
h. super.sound()
______
______
e. t.sound('!') ______
i. super.sound('!' ) ______
f. t.activity() ______
j. super.activity() ______
name: ________________________________________ alpha: _________________ page 5
8. [18pts] Consider the (poorly written!) program below, which simulates a figure on a grid moving
randomly until it lands at grid position 10,10, then prints out how many steps it took to get
the collision.
Pos.java
Mover.java
public class Pos
public class Mover
{
{
private int row, col;
public int row, col, dir;
public Pos(int r, int c)
public Mover(int r, int c, int d)
{
{
row = r; col = c;
row = r; col = c; dir = d;
}
}
public void move(int dir)
public void move(int dir)
{ // N = 0, E = 1, S = 2, W = 3
{ // N = 0, E = 1, S = 2, W = 3
if (dir % 2 == 0)
if (dir % 2 == 0)
col += (dir - 1);
col += (dir - 1);
else
else
row -= (dir - 2);
row -= (dir - 2);
}
}
public int getRow() { return row; }}
public int getCol() { return col; }Collision.java
public boolean match(Pos p)
import java.util.*;
{
public class Collision
return row == p.row &&
{
col == p.col;
public static void randStep(Mover m, Random r)
}
{
}
m.dir = r.nextInt(4);
m.move(m.dir);
}
}
public static void main(String[] args)
{
int count = 0;
Random r = new Random(System.currentTimeMillis());
Pos barrier = new Pos(7,3);
Mover m = new Mover(10,10,0);
while(!(barrier.getRow() == m.row &&
barrier.getCol() == m.col))
{
count++;
randStep(m,r);
}
System.out.println(count + " steps to collide!");
}
a. How does this program fail to properly use information hiding?
b. How does the randStep function violate encapsulation?
c. How could inheritance have been used to write this program better?
Download