Q1: 24 points Q2: 20 points Q3: 20 points Q4: 16 points Q5: 20 points Total: 100 points
Questions 2 and 5 refer to the following class whose objects are nodes in a linked list -- this is the same class used on the first midterm:
public class LLNode<T> {
private LLNode<T> next;
private T contents;
public LLNode (T x)
{contents = x; next = null;}
public void setContents (T x) {contents = x;}
public T getContents( ) {return contents;}
public void setNext (LLNode<T> t) {next = t;}
public LLNode<T> getNext( ) {return next;}
}
mystery(45)
produce given the method below? (Note: Copyrighted example taken
from
the CSE 143 Web at the University of Washington Department of
Computer
Science and Engineering.)
public static void mystery (int n) {
if (n <= 1) {
System.out.print(": ");}
else {
System.out.print((n % 2) + " ");
mystery (n/2);
System.out.print(n + " ");
}
}
Barking class below
produce any output? If so, how many lines of output?
public class Cardie implements Runnable {
public void run ( ) {
while (true) System.out.println("Woof!");}}
public class Barking {
public static void main (String [ ] args) {
Runnable r = new Cardie( );
Thread t = new Thread(r);
t.start( );}}
==, equals, and
compareTo. What are the differnences among these
three types of comparison?
ArraySortedList to make the contains
and get methods each run in O(log n) time instead of
O(n) time? (Note: The version of this question given in the
actual test also incorrectly said that remove could
be similarly sped up.)
LLNode<T> class defined
above)
public boolean hasT (LLNode<T> p, T elem) {
// returns true if any element equal to elem
// is in the linked list headed by p
if (p == null) return false;
if (p == elem) return true;
return hasT (p,getNext( ), elem);
}
public T dequeue( ) {
if (isEmpty( ))
throw new QUE ("dequeue from empty");
else {
T toReturn = queue[front];
queue[front] = null;
front++;
numElements--;
return toReturn;
}
}
public class Animal {
public void bar( ) {
System.out.println("bar");}}
public class Dog extends Animal {
public void foo( ) {
System.out.println("foo");
super.foo( );}}
LLNode<T> class defined above)
public T antepenult( ) {
// returns contents of third node from last
// in linked list headed by this node
// returns null if there is no such node
if (this == null) return null;
if (this.getNext( ) == null) return null;
if (this.getNext( ).getNext( ) == null) return null;
if (this.getNext( ).getNext( ).getNext( ) == null)
return this.getContents( );
return antepenult (this.getNext( ));
}
int largestDigit(long
n) that accepts an integet parameter and returns the
largest digit value that appears in the decimal string
representing that integer. (Note: This problem also taken
(slightly adapted) from UW CSE's CSE 143 Web.)
If a number's string contains only a single digit, that digit's value is by definition the largest. Here are some examples of desired results:
largestDigit (14263203) returns 6
largestDigit (845) returns 8
largestDigit (52649) returns 9
largestDigit (3) returns 3
largestDigit (0) returns 0
largestDigit (-573026) returns 7
largestDigit (-2) returns 2
For full credit, obey the following restrictions in your solution:
String,
Scanner, array, or any data structure such as a
list or stack.
Hint: In Java, 345 % 10 is 5 and 345
/ 10 is 34. However, -42 / 10 is -4 and
-42 % 10 is -2. If you don't like these latter
facts, don't use % on negative numbers (it's
pretty easy to avoid doing so).
public static int largestDigit (long n) {
Stack<String> object, that simulates this
recursive method. Your method should have the same output as
the recursive one for any int input. It is
possible to design a simple iterative algorithm that gives
this output without using a stack. But we want you to
use a stack, ideally to directly simulate the action of the
recursuve method.
public static String triangle (int n) {
if (n == 0) return ("");
String out = "";
out = "*" + triangle (n - 1);
System.out.println (out);
return out;
}
public static String iterativeTriangle (int n) {
int fun (int n) {
if (n <= 0) return 1;
return fun (n-1) + fun(n-1);
}
int anotherFun (int n) {
if (n <= 0) return 0;
return anotherFun (n/2) + n;
}
get and
set methods of ArrayList each take
O(1) time.)
ArrayList<Integer> m = new ArrayList<Integer>( );
int fib(int n) {
if (n <= 1) {
m.set(n, n);
return m.get(n);}
Integer f = m.get(n);
if (f == null) {
f = fib(n-1) + fib (n-2);
m.set (n, f);}
return f;
}
LinkedUnbndQueue<T> that implements a
queue using a linked list, made up of nodes from the
LLNode<T> class defined above. All
methods must run in O(1) worst-case time. You may assume
that the exception class
QueueUnderflowException has alredy been
defined.
public class LinkedUnbndQueue<T>
implements UnboundedQueueInterface<T> {
protected LLNode<T> front, rear;
// four methods below will go here
}
public LinkedUnbndQueue ( ) {
public boolean isEmpty ( ) {
public void enqueue (T element) {
public T dequeue ( ) {
Last modified 10 November 2014.