


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



COS 126 General Computer Science Spring 2006
G C B A E F
Elis Tigers Tigers Big Red
In the constructor, the local variable name shadows the instance variable name, so the instance variable is always initialized to "Elis". The setName() method works as intended. Changing what string x points to will not change team. In Java, strings are immutable, so x.replaceAll() has no effect on x. To change x, you would use x = x.replaceAll("Red", "Green");.
(a)
public class Point { private double x, y;
public Point(double x, double y) { this.x = x; this.y = y; }
public double distanceTo(Point b) { double dx = b.x - x; double dy = b.y - y; return Math.sqrt(dxdx + dydy); }
public String toString() { return "(" + x + ", " + y + ")"; } }
(b)
// find furthest point from a Point furthest = a; while (!StdIn.isEmpty()) { double bx = StdIn.readDouble(); double by = StdIn.readDouble(); Point b = new Point(bx, by); if (a.distanceTo(b) > a.distanceTo(furthest)) furthest = b; }
A common optimization is to store the furthest distance encountered so far in a separate variable to avoid recomputing each time.
gauge.temp[0] = -1.0;
gauge.setTemp(t); t[0] = -1.0;
t = gauge.getTemp(); t[0] = -1.0;
gauge.N = 0; t[0] = -1; gauge.setTemp(t); gauge.N = 86400;
F The undecidability of the halting problem is a statement about Turing machines: it is not applicable to real computers.
T The Church-Turing thesis is a theory about our universe: it cannot be proven mathe- matically, but it can be falsified.
T The Turing machine is a universal model of computation: with a Turing machine we can solve any decision problem that can be solved with a DFA or with a Pentium M running Linux 2.6.16.
T P is the class of yes-no problems for which a polynomial-time Java program could, in principle, be written.
F NP is the class of yes-no problems for which no polynomial time Java program could ever be written.
T NP is the class of yes-no problems for which, in principle, you could write a Java program to check a given proposed solution in polynomial-time.
F If you discover a polynomial-time algorithm for any problem in NP, then all problems in NP are solvable in polynomial-time.
F Most Princeton computer science faculty believe that P = NP.
T Since Factor is a problem in NP, any instance of Factor can be restated as an instance of 3-Sat (3-satisfiability).
(a) x 2 x 1 x 0 f 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 1 1 1 0 1 1 1 1 1 (b) f = x 2 x′ 1 x 0 + x 2 x 1 x′ 0 + x 2 x 1 x 0. (c) Observe that f = x 2 (x 1 + x 0 ) and draw the resulting circuit.