Friday, February 8, 2008

Easy Homework Assignment

Instructions

The following questions will help us assess your Java Programming skills. In all questions below, please make sure that the code you write is syntactically and semantically correct. Ideally, we should be able to compile and execute your code. Psuedo-code is not acceptable. Please read all instructions and directions before proceeding.

1. Is there a memory leak in the code below? If yes, can you spot the memory leak and fix the code to not leak?

public class Stack {
private Object[] elements;
private int size = 0;

public Stack(int initialCapacity) {
this.elements = new Object[initialCapacity];
}

public void push(Object e) {
ensureCapacity();
elements[size ++] = e;
}

public Object pop() {
if(size == 0)
throw new EmptyStackException();
return elements[--size];
}

public void ensureCapacity() {
if(elements.length == size) {
Object[] oldElements = elements;
elements = new Object[2 * elements.length + 1];
System.arraycopy(oldElements, 0, elements, 0, size);
}
}
}

If the stack grows and then shrinks, the objects will not be garbage collected that are being popped off the stack. The method should be re-written as:

public Object pop() {
If (size > 0) {
Object returnVal = elements[--size];
elements[size] = null;
return returnVal;
}
return null;
}


2. Write a class StringUtil that has a method reverseElements that accepts a String array and which returns the SAME String array with the array elements in reverse order. The signature of reverseElements should be

Void reverseElements(String[] strArr)


If strArr is { “dog”, “cat”, “mouse”, “cow” } before calling reverseElements, it should be {“cow”, “mouse”, “cat”, “dog”} after the call.

Using any other extra collection/array should also be avoided. We want you to manipulate the contents of the strArr itself.

public class StringUtil {
public StringUtil() { }

public void reverseElements(String[] strArr) {
int left, right;

for (left=0, right=strArr.length-1; left < right; left++, right--) {
String temp = strArr[left];
strArr[left] = strArr[right];
strArr[right] = temp;
}
}
}

3. Write a unit test for StringUtil#reverseElements.

public void testReverseElements() {
String myStrArr[] = { "dog", "cat", "mouse", "cow" };
StringUtil mySU = new StringUtil();
mySU.reverseElements(myStrArr);
assertEquals("cow", myStrArr[0]);
assertEquals("mouse", myStrArr[1]);
assertEquals("cat", myStrArr[2]);
assertEquals("dog", myStrArr[3]);
}


4. Define class Foo such that there can at any time be exactly one instance of Foo.

public class Foo {
private static Foo myInstance;

private Foo() { }

public static Foo getInstance() {
if (myInstance == null)
myInstance = new Foo();

return myInstance;
}
}


5. class Bar {
private String _name;
private int _age;

Bar(String inName, int inAge) {
_name = inName;
_age = inAge;
}
}

Identify the base class of Bar. Bar is its own base class
Determine the method signature of equals in Bar’s base class equals(String, int)
Override and re-implement it such that the equals method returns true only if name and age match. In other words we want to be able to compare two Bar instances.

class FooBar extends Bar {
FooBar(String inName, int inAge) {
super(inName, inAge);
}

@Override
public boolean equals(String inName, int inAge) {
return super.equals(inName, inAge);
}
}


6. In the question above, if you override the equals method, is there any other method of class Object that needs to be overridden? If so, which one?

It is a general rule that if you override the equals() method you always override the hashCode() method also.

No comments: