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

advertisement
IC211: Object-Oriented Programming
Spring AY2015 — 12-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 100 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. [8pts] Consider the class MySet documented below:
MySet Documentation (not the complete definition)
public class MySet
{
// adds x to set - ignores if x already present
public void add(int x);
// removes x from set - ignores if x not in set
public void rem(int x);
// returns iterator initialized to beginning of set
public Iter iterator();
public class Iter
{
// true if there is another "next" available
public boolean hasNext();
}
}
// returns "next" int in set & moves forward one in set
public int next();
Define the method sumall that takes a MySet s and returns the sum of all the int's in s.
public int sumall(MySet s)
{
}
2. [4pts] Briefly explain why we have iterators. To be concrete: in our omnipresent Queue class,
why did we we introduce iterators, as opposed to simply adding a method that returned the
"head" node in the linked list of Queue data?
name: ________________________________________ alpha: _________________ page 2
3. [28pts] Consider te following code:
Ex2.java
Apesha.java
import java.util.*;
public class Apesha
public class Ex2
{
{
public int idesa() { return 0; }
public static Random r = new Random();
}
public static void main(String[] args)
Ectra.java
{
public class Ectra extends Apesha
Apesha a = new Rita();
{
Apesha b = new Aresqua();
public int idesa() { return 4; }
Apesha c = r.nextBoolean() ? new Ectra() : new Rita();
public String abela() { return "R"; }
Apesha d = r.nextBoolean() ? new Ectra() : new Aresqua();
}
Ectra e = r.nextBoolean() ? new Ectra() : new Aresqua();
Rita.java
// Here!
public class Rita extends Apesha
{
// Note: the ? : construct is an "if" as an expression.
public int idesa() { return 3; }
// so r.nextBoolean() ? new Ectra() : new Aresqua() will
public String abela() { return "T"; }
// evaluate to a new Ectra 50% of the time, and a new
}
// Aresqua the other 50% of the time.
Aresqua.java
}
public class Aresqua extends Ectra
}
{
public String abela() { return "S"; }
}
For each of the following expressions give either the value produced by the expression, or "error"
if there is an error, or "can't tell" if there is no error, but it is not possible to determine
the exact value.
a. a.idesa() _________________________
f. a.abela() _________________________
b. b.idesa() _________________________
g. b.abela() _________________________
c. c.idesa() _________________________
h. c.abela() _________________________
d. d.idesa() _________________________
i. d.abela() _________________________
e. e.idesa() _________________________
j. e.abela() _________________________
for the following statements write "OK" if the statement is OK, and either "compile time error" or
"runtime error" as appropriate if there is an error.
k. e = c; _________________________
l. e = d; _________________________
m. a = e; _________________________
n. b = e; _________________________
name: ________________________________________ alpha: _________________ page 3
4. [20pts] The following program is written in an entirely procedural fashion. On the following
page, rewrite this program in a properly object oriented fashion. Show clearly how code is
packaged into files by drawing boxes around code in the same file.
Ex1.java
public class Ex1
{
public static void setToLowerLeft(Pos p)
{
if (p.kind == 0)
{ p.row = 11; p.col = 0; }
else if (p.kind == 1) { p.x = 0; p.col = 0; }
else
{ p.row = 1; p.c = 'a'; }
}
Pos.java
// Positions on a
// 12x12 chessboard
public class Pos
{
public int row;
public int col;
public int x;
public int y;
public char c;
public static String toString(Pos p)
public int kind;
{
// kind 0 = row,col
if (p.kind == 0) return "row=" + p.row + ", col=" + p.col; // kind 1=x,y
else if (p.kind == 1) return "x=" + p.x + ", y=" + p.y;
// kind 2=chess-style
else return "pos=" + p.c + p.row;
}
}
}
public static void main(String[] args)
{
Pos[] A = new Pos[3];
A[0] = new Pos(); A[0].kind = 0;
A[1] = new Pos(); A[1].kind = 1;
A[2] = new Pos(); A[2].kind = 2;
for(int i = 0; i < 3; i++)
setToLowerLeft(A[i]);
for(int i = 0; i < 3; i++)
System.out.println(toString(A[i]));
}
Not that it really matters, but in case you're not clear on the different schemes used in the
program for labelling the squares on a chess board, here they are:
row,col numbering
x,y numbering
0 1 2 3 4 5 6 7 8 9 10 11
chess-style numbering
0 1 2 3 4 5 6 7 8 9 10 11
a b c d e f g h i j k l
0
11
0
1
10
1
2
9
2
3
8
3
4
7
4
5
6
5
6
5
6
7
4
7
8
3
8
9
2
9
10
1
10
11
0
11
name: ________________________________________ alpha: _________________ page 4
name: ________________________________________ alpha: _________________ page 5
5. [16pts]
public class SchoolException extends Exception
{
private String name;
public SchoolException(String msg, String name) { super(msg); this.name = name; }
public String getName() { return name; }
}
public class NavyException extends SchoolException
{
public NavyException() { super("Good choice! Navy requested.","USNA"); }
}
public class AirforceException extends SchoolException
{
public AirforceException() { super("Warning! Airforce requested.","USAFA"); }
}
public class ArmyException extends RuntimeException
{
public ArmyException() { super("Error! Army requested"); }
}
import java.util.*;
public class Ex3
{
public static String getResponse() throws SchoolException
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if (s.equals("USMA")) throw new ArmyException();
if (s.equals("USNA")) throw new NavyException();
if (s.equals("USAFA")) throw new AirforceException();
if (s.equals("Notre Dame"))
throw new SchoolException("Warning! Questionable choice.",s);
return s;
}
public static void main(String[] args)
{
System.out.print("Enter school: ");
String school = null;
try {
school = getResponse();
}
catch(NavyException e) { school = "USNA"; }
catch(SchoolException e) { System.out.println(e.getMessage()); school = e.getName(); }
System.out.println("You chose " + school);
}
}
In the following runs of the program, complete the output. If the output is more than two lines,
you can describe it rather than write it out literally.
a. complete output for
b. complete output for
~/$ java Ex3
~/$ java Ex3
Enter school: USAFA
Enter school: USNA
c. complete output for
~/$ java Ex3
Enter school: USMA
d. complete output for
~/$ java Ex3
Enter school: Notre Dame
name: ________________________________________ alpha: _________________ page 6
6. [8pts] The question "Does Java support multiple inheritance?" does not have a straight forward
yes/no answer. Give a thorough and nuanced response to it.
7. [16pts]
Ex1.java
Pos.java
public class TwoSidesA<T>
public class TwoSidesB
{
{
private T up;
private Object up;
private int down;
private int down;
public TwoSidesA(T u, int d)
public TwoSidesB(Object u, int d)
{
{
up = u;
up = u;
down = d;
down = d;
}
}
public T getUp() { return up; }
public Object getUp() { return up; }
public int getDown() { return down; } public int getDown() { return down; }
}
}
Assume the declarations:
TwoSidesA<String> x;
TwoSidesB y;
a. Write the statement that sets x to a new TwoSides with up-value "navy" and down-value 255.
b. Write the statement that sets y to a new TwoSides with up-value "navy" and down-value 255.
c. Assuming that x has been set to point to some TwoSidesA<String> (not necessarily the one
you created above) write the statement that would print out the length (i.e. number of
characters) of the up-value of x.
d. Assuming that y has been set to point to some TwoSidesB (not necessarily the one you
created above) write the statement that would print out the length (i.e. number of
characters) of the up-value of y.
Download