Assignment 7, due Oct 16Solutions
Part of
the homework for CS:2820, Fall 2015
|
class D { class C { private int i = 0; public int i = 0; public int getI() { return i; } } public int setI(int v) { i = v; } } C cc = new C(); D dd = new D();
These seem equal, in the sense that cc.i=5 is comparable to dd.setI(5) and x=cc.i is comparable to x=dd.getI(). Our goal is to create an interface specification, such that one of these classes can be declared to be an implementation of that interface.
a) One of these does not permit such an interface to be constructed, explain why. (0.2 points)
Class C cannot be replaced by an interface because interfaces can only contain abstract methods, not fields or method implementations.
b) Give the interface definition that one of these can be declared to implement. (0.4 points)
interface Dinterface { int getI(); int setI(int v); }
c) Rewrite one of these so that it explicitly declares itself as an implementation of that interface. (0.4 points)
class D implements Dinterface { private int i = 0; public int getI() { return i; } public int setI(int v) { i = v; } } D dd = new D();
The section entitled Inner classes & control frameworks comes close, pages 375 to 382.
import java.util.Random; class PRNG { /** Pseudo-Random Number Generator for simulation model. */ private static Random r = new Random(); public static randInt( int n ) { /** Draw an integer in the range 0 <= i < n from the stream. * But first, draw a random number of entries form the stream * in order to make the result more random. */ for (int i = r.nextInt( 10 ); i > 0; i--) r.nextInt(); return r.nextInt( n ); } }
A question: Does this indeed create a result that is more random than the built-in Java pseudo-random number stream? Give reasoning to support your conclusion. (0.5 points)
No! It is the same stream of random numbers as the built-in stream, except that we are using some members of that stream to select other members of that stream to be skipped. If someone intent on cheating on the lottery can predict the next random number that will be drawn from the original stream, they can use that prediction equally well to predict the new stream.
A question: For each operation above, document the corresponding Java linked list method. (0.5 points)
insert-head(i) | addFirst() |
insert-tail(i) | addLast() |
get-head() | removeFirst() |
get-tail() | removeLast() |
A question: What are the key events on the classes for wires and gates in a logic simulator? This question is to getting you started on MP3. (0.5 points)
Output of gate g changes to new value x.
Input to wire w changes to new value x. This can be directly called from the above.
Output of wire w changes to new value x.
Input i of gate g changes to new value x. This can be directly called from the above.