Instructions



Student ID number: ______________________________________

Instructions

1. This exam is conducted under the Rice Honor Code. It is a closed-notes, closed-book exam.

2. Fill in your name on every page of the exam.

3. If you forget the name of a Java class or method, make up a name for it and write a brief explanation in the margin.

4. You are expected to know the syntax of defining a class with appropriate fields, methods, and inheritance hierarchy. You will not be penalized on trivial syntax errors, such as missing curly braces, missing semi-colons, etc, but do try to write Java code as syntactically correct as possible. We are more interested in your ability to show us that you understand the concepts than your memorization of syntax!

5. Write your code in the most object-oriented way possible, that is, with the fewest number of control statements and no checking of the states and class types of the objects involved.

6. For each algorithm you are asked to write, 90% of the grade will be for correctness, and 10% will be for efficiency and code clarity.

7. You have two hours and a half to complete the exam.

Please State and Sign your Pledge:

|1.) 20 |2.a.i) 5 |

|package tv; |package tv; |

| | |

|public class Channel1 extends AChannel { |public class Channel2 extends AChannel { |

| | |

|static final Channel1 Singleton = new Channel1(); |static final Channel2 Singleton = new Channel2(); |

| | |

|private Channel1() { } |private Channel2() { } |

| | |

|void one(TVRemote host) { |void one(TVRemote host) { |

|} |host.setChan(Channel1.Singleton); |

| |host.setPrev(this); |

|void two(TVRemote host) { |} |

|host.setChan(Channel2.Singleton); | |

|host.setPrev(this); |void two(TVRemote host) { |

|} |} |

| | |

|void three(TVRemote host) { |void three(TVRemote host) { |

|host.setChan(Channel3.Singleton); |host.setChan(Channel3.Singleton); |

|host.setPrev(this); |host.setPrev(this); |

|} |} |

| | |

|public String toString() { |public String toString() { |

|return "Channel 1"; |return "Channel 2"; |

|} |} |

|} |} |

|package tv; | |

| | |

|class Channel3 extends AChannel { | |

| | |

|static final Channel3 Singleton = new Channel3(); | |

| | |

|private Channel3() { } | |

| | |

|void one(TVRemote host) { | |

|host.setChan(Channel1.Singleton); | |

|host.setPrev(this); | |

|} | |

| | |

|void two(TVRemote host) { | |

|host.setChan(Channel2.Singleton); | |

|host.setPrev(this); | |

|} | |

| | |

|void three(TVRemote host) { | |

|} | |

| | |

|public String toString() { | |

|return "Channel 3"; | |

|} | |

|} | |

a) (10 pts) Draw a complete UML class diagram of the above system, including all fields and methods for every class shown. Be sure to show the proper access specifiers (e.g. public, private, etc). To save time and space, you may omit the names of any implemented methods in the concrete subclasses. Also, simply write the word “abstract” next to anything that should be indicated as such.

Answer:

[pic]

b) (15 pts) What output will the following code produce? Why? Be specific and complete!

|Operations executed in the following order: |Screen Output and reasons for that output: |

|TVRemote tvr = new TVRemote(); |Output: “Channel 1” |

|tvr.showChannel(); |New instance of TVRemote is created. Defaults to channel 1. |

|tvr.three(); |Output: “Channel 3” |

|tvr.showChannel(); | |

| |Internal state (as represented by the chan field) changes to Channel3 |

|tvr.unknown(); |Output: “Channel 1” |

|tvr.showChannel(); |State changes back to previous channel which was Channel1 |

| | |

| | |

|tvr.unknown(); |Output: “Channel 3” |

|tvr.showChannel(); |State changes back to previous channel which was Channel3 |

| | |

|tvr.one(); |Output: “Channel 3” |

|tvr.unknown(); |State changes first to Channel1 then back to previous channel which was Channel3 |

|tvr.showChannel(); | |

|tvr.two(); |Output: “Channel 3” |

|tvr.unknown(); |State changes first to Channel2 then back to previous channel which was Channel3 |

|tvr.showChannel(); | |

|tvr.two(); |Output: “Channel 2” |

|tvr.one(); |State changes first to Channel2 then to Channel1. Hitting Channel1 again does |

|tvr.one(); |nothing, so then back to previous channel which was Channel2. |

|tvr.unknown(); | |

|tvr.showChannel(); | |

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download