CSC234 - Exam 1

Assigned Feb 18th 1pm
Due Feb 20th, 1pm


Read all questions before beginning. You will have until 1pm Friday February 20th, 2009 to complete this exam. You are not allowed to use your notes or textbook or the internet or code from the course website, except what is provided in this exam. You may use the computer to compile and test your Java code. You are bound by the Honor Code for this exam, which you need to sign below. Show your work on all problems for full credit.

Create a directory on the cs.centenary.edu server called exam1, and copy your solutions there. For question 4, create a question4.txt file that includes your answers.

Question 1

Implement a version of the Queue<E> interface using two ArrayStack<E> as your only data members. You can use the files Queue.java, Stack.java and ArrayStack.java from the book code. You should turn in a file called StackQueue.java, of which the basic template is provided for you.

Question 2

We wish to add two methods to the ArrayList.java implementation of the List.java interface.

First, write a method count() that takes one argument, an element target. It returns the number of elements in this ArrayList which are equal to target. Debug the code I have written for this method.

Second, write a method indexOf() that takes one argument, an element target, and returns the index of the first occurence of target in this ArrayList, or -1 if target does not appear.

Third, write a main method to demonstrate that these two new methods work.

Question 3

Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Write a class ClumpDetector with a public static int method called countClumps that will output the number of clumps in the given array. With the input below, it should return the associated output.

countClumps({1, 2, 2, 3, 4, 4}) -> 2
countClumps({1, 1, 2, 1, 1}) -> 2
countClumps({1, 1, 1, 1, 1}) -> 1

Question 4

Describe the following concepts of Java programming, and give an example in code where they might be used: