Q1: 24 points Q2: 20 points Q3: 20 points Q4: 16 points Q5: 20 points Total: 100 points
public class Sport {
public void type( ) {
System.out.println("Indoor or Outdoor");}
}
public class Kickball extends Sport {
public void type( ) {
System.out.println("Outdoor"):}
public static void main (String [] args) {
Sport s = new Sport( );
Kickball k = new Kickball( );
s.type( );
k.type( );
s = k;
s.type( );}
}
Integer
objects and then
instantiates them.
int
and an Integer
in Java?
Group<T>
. What instance methods can be run on
an object of type T
in this code?
LinkedStack<T>
generic class that
implements an interface StackInterface<T>
.
Suppose that after completing the project, we want to replace the
linked stack with a homebuilt ArrayStack<T>
generic class that implements the same interface. What changes
should be made in the postfix evaluator?
public interface InterfaceA {
public void methodA( );}
public interface InterfaceB extends InterfaceA {
public void methodB( );}
public class ClassA implements InterfaceA {
public void methodA( ) { }
public void methodB( ) { }
}
public class ClassB extends ClassA implements InterfaceB {
public ClassB( ) { }
... // other methods not shown
}
InterfaceB obj = new ClassA( );
Stack<Object> stack = new Stack<Object>( );
stack.push("hello");
String s = stack.pop( );
public class Dog
{
.
.
.
}
public class DogWithLeash extends Dog
{
.
.
.
}
Dog[ ] calls = new Dog[3];
calls[0] = new Dog( );
calls[1] = (DogWithLeash) calls[0];
calls[2] = new DogWithLeash( );
System.out.println(calls[0] + " " + calls[1] + " " + calls[2]);
Stack<int> a = new Stack<int>( );
Write a method that determines whether its parameter string
is a palindrome. Your method should create and use a
single, bounded stack of characters, although there are
certainly ways to solve this problem without a stack. Use the
Java Stack<T>
class which has the following
six methods: push(T e)
, pop( )
,
pop( )
, peek( )
,
empty( )
, size( )
,
and full( )
.
Remember that pop
here both removes and
returns the top element, unlike the pop
methods in DJW.
The size of your stack should be bounded to n/2, where n is the length of the input string.
public boolean isPalindrome (String s) {
true
if two linked lists of Integers contain the
same Integers in the same order (the actual values, not the
objects). Your method may be recursive if you like, though
this is not required. After your method has run, the two
lists should be unchanged. Note that the method you are to
write is not in the LLNode<T>
class.
public class LLNode<T> {
private LLNode<T> next;
private T contents;
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;}
}
public static boolean lTest
(LLNode<Integer> list1, LLNode<Integer> list2) {
public static int countit (int n) {
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < 10; j++)
count += 1;
return count;
}
for (c = 0; c < (n - 1); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) {
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;}}}
public void clear (Stack<Integer> s) {
if (!s.empty( )) {
s.pop( );
clear(s);}}
String
objects in alphabetical order, and you want
to insert a new String
into the list so that the
list remains in alphabetical order. Assume that the strings are
each of length O(1). Briefly describe your algorithm to do this,
and give its big-O worst-case time complexity.
public class LLNode<T> {
private LLNode<T> next;
private T contents;
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;}
public LLNode(T x, LLNode<T> s) {
contents = x; next = t;}
}
public class LList<T> {
private LLNode<T> head;
public LList( ) {head = null;}
.
.
.
}
LList<T>
class
public int count (T x)
that returns the number of times, if any, that the element
occurs in the calling list. By "x occurs in the list", we
mean that the contents of a node are equal to x according
to the .equals
method of the class
T
. Your method should leave the list
unchanged.
LList<T>
class
public void reverse( )
that will replace the calling list with a new
LList
that contains the same elements as the
old one, but in reverse order.
LList<T>
class
public void deleteLast( )
that will remove the last element in the calling list (the element furthest from the head). The remaining elements, if any, should be left in the same order they were in. If the calling list is empty the method should leave the list empty and not throw an exception. (Hint: There are several ways to do this, and we will accept any method that has the correct behavior. You might find it useful to create one or more additional lists, or call the method from Question 5b, or both.)
Last modified 10 October 2014