|
Comp201: Principles of Object-Oriented Programming I
|
Check the time stamp at the bottom of the page to be sure that you have the latest version with any typographical corrections!
Pledge of Honor |
1.a 10 pts | 1.b 25 pts | 2.a 10 pts | 2.b 15 pts | 2.c 15 pts | 3.a 10 pts | 3.b 15 pts | 3.c 5 pts
extra |
3.d 10 pts
extra |
Total: 100 pts
+ 15 pts extra |
You must supply both the electronic and paper copies of your exam in order to for you exam to be graded!
In general, since we are ignoring the "generics" features of Java, DrJava will occasionally complain with an "unchecked type warning" (or something similar). Ignore these warnings -- they can be permanently suppressed by un-checking the appropriate box under "Preferences/Compiler Options" in DrJava.)
When Dr. Nguyen and Dr. Wong presented the sorting framework at a conference in 2001, an audience member challenged them to write the "Shaker Sort" in the framework. So, in the best of object-oriented traditions, we delegate that task off to you!
The idea of a Shaker Sort is to attempt to make Bubble Sort faster by alternately bubbling the smallest value to the top and then bubbling the largest value to the bottom and then back to bubbling the smallest to the top. This bubbling back and forth gives the algorithm its name.
See the online demo that includes the ShakerSort.
Let's first take a look at BubbleSorter, whose code is supplied to you:
The ShakerSorter code needs to do the following:
Problem 1.a (10 pts): In comments at the top of the supplied stub code for ShakerSorter.java, explain the following:
The supplied project in the Prob1 directory is already set up to run the main method of SorterApp when F4 is pressed. Be sure to check the "Graphical Ordering" and "Graphic Split/Join" check boxes so you can see the sorting process better.
Remember that lo and hi are inclusive, that is, lo is the index of the first element of the sub-array to be sorted and hi is the last element of the sub-array to be sorted.
In this problem, you are to write the code for the three methods in a class called ArrayUtils. The supplied stub code and JUnit test code for this problem is located in the subdirectory Prob2.
If all else fails: Here is a the class file for the ArrayUtils_Soln class, which is identical to ArrayUtils except in class name. If you need one of the methods below, but cannot get it to work properly, you may use ArrayUtils_Soln instead. Simply place this class file in the Prob2\classes\array directory and call the corresponding method on ArrayUtils_Soln.Singleton that you need.
Problem 2.a (10 pts): Use a for loop to write the code for the method countElements as specified by the Javadoc comments below and by the JUnit test method test_countElements in the JUnit test class TestArrayUtils.
Hint: Given Object[][] m = {{1, 2}, {3}, {4, 5, 6}}, what is m.length and what does it represent? How do you get the number of elements for each row in the matrix m?
public class ArrayUtils { public static final ArrayUtils Singleton = new ArrayUtils(); private ArrayUtils() { }/** * Counts the number of elements in a 2-dimensional array. * @param m != null * @return the maximum number of elements that can be stored in m. */ public int countElements(Object[][] m) { // STUDENT TO COMPLETE }
// OTHER METHODS ELIDED }
Problem 2.b (15 pts): Use a for loop to write the code for the method copyArray as specified by the Javadoc comments below and by the JUnit test method test_copyArray in the JUnit test class TestArrayUtils.
Hints:
public class ArrayUtils { public static final ArrayUtils Singleton = new ArrayUtils(); private ArrayUtils() { } /** * Copy a number of consecutive elements from a 1-dimensional array starting * from a given index to another 1-dimensional array starting at a given index. * * @param src != null, the source array. * * @param srcLo the starting index of in the src array from where the copying begins; * 0 <= srcLo <= src.length - 1 must be true, otherwise, * throw an IllegalArgumentException. * * @param maxElts >=0, the maximum number of elements in the src array to * be copied to the dest array at dstLo. The actual number of elements * copied is the minimum of maxElts, the number of elements remaining * in src starting at srcLo, and the number of elements remaining in dest * starting at dstLo. If maxElts < 0 do nothing. * * @param dest !=null, the destination array. * * @param dstLo the starting index of the dest array. * 0 <= dstLo <= dst.length - 1 must be true, otherwise, * throw an IllegalArgumentException. * * @exception IllegalArgumentException whenever any of the conditions of srcLo * and dstLo as stated in the above are violated. */ public void copyArray(Object[] src, int srcLo, int maxElts, Object[] dest, int dstLo) { //STUDENT TO COMPLETE }// OTHER METHODS ELIDED }
Problem 2.c (15 pts): Use a for loop to write the code for the method matrix2array as specified by the Javadoc comments below and by the JUnit test method test_matrix2array in the JUnit test class TestArrayUtils.
Hints:
package array;public class ArrayUtils { public static final ArrayUtils Singleton = new ArrayUtils(); private ArrayUtils() { } // OTHER METHODS ELIDED.../** * Converts a 2-dimensional array m to a 1-dimensional array whose size is * the number of elements in the input matrix m. * @param m != null. * @return a 1-dimensional array that is the concatenation of all the row arrays * in the matrix from the lowest row index to the highest row index. */ public Object[] matrix2array(Object[][] m) { // STUDENT TO COMPLETE } }
RAC's are essentially memory devices. They store information and retrieve it later on. The order in which the data is "remembered" depends on what type of RAC is used.
A tree traversal is a process that traces a path through the tree (see Lab #13). The elements of the tree are processed in the order that they are encountered during the traversal process. Thus we can talk about a tree traversal in terms of processing the elements of the tree in some "remembered" order through the tree.
RAC's and tree traversals are made for each other.
Consider the following BiTree visitor, called RACTraverse, that expresses a tree traversal process using a RAC as the input parameter:
Empty Case:
- Return the given RAC
Non-Empty Case:
- Put the root data into the given RAC.
- Recur to the left.
- Recur to the right.
- Return the given RAC.
The data collected in the RAC during the traversal process can be retrieved either one element at a time or by using its elements method, which returns a list of its elements in their proper order. We will be considering the net effect on the order of the data as it is retrieved from the RAC.
Problem 3.a (10 pts): Stacks vs. Queues Discussion
What do you think the difference will be when a stack vs. a queue is used as the RAC in the above traversal algorithm? That is, what is the difference in order of the data retrieved from the RAC after the traversal is completed?
Write your answer as a comment at the top of the RACTraverse.java file.
Problem 3.b (15 pts): Implementation of RACTraverse
Finish the implementation of RACTraverse as per the above description. The test code has been provided.
Once you have completed the regular portion of the exam, you may take an additional 30 minutes beyond the 4 hour limit to complete the following extra credit problems.
Problem 3.c (5 pts extra): Tree traversal in order with respect to value -- discussion of theory
Assume that the BiTree contains objects which are either Comparable or for which a Comparator can be defined (that is, a binary comparison can be used to define a total ordering). How would you go about creating a tree traversal such that the elements of the tree came out in order with respect to their values, not their positions in the tree?
Write you answer as a comment at the top of the Test_RACTraversal_Extra.java file.
Problem 3.c (10 pts extra): Tree traversal in order with respect to value -- implementation of code
Write any code necessary to implement your theory on how to produce a traversal of a tree ordered in terms of value (either "lowest" to "highest" or "highest" to "lowest"). Write the test code to thoroughly test you algorithm in the Test_RACTraversal_Extra.java file. You will be graded on the completeness and thoroughness of your test code.
Note: Integers, Doubles and Strings are all Comparable objects.
Last Revised Thursday, 03-Jun-2010 09:50:25 CDT
©2008 Stephen Wong and Dung Nguyen