Program Design II – Exam #3
Write your answers on the answer sheets provided. You may refer to
printouts of Java command sheets (without commentary) that were provided in class. No
other outside materials are to be used/referenced during the exam.
class Dog {
private String name;
private double weight;
public Dog(String name, double weight) {
this.name= name;
this.weight= weight;
}
}
class ListNode {
int data;
ListNode next;
}
class MyStack {
// internal implementation not specified, but does have these methods
public void push(char elem) {}
public char pop() {}
public boolean isEmpty() {}
}
- (4 pts) Expand the acronym FIFO. What data structure is it associated with?
- (4 pts) Name two assumptions that must be true for a binary search to be possible.
- Refer to the Dog class defined on the first page of this exam and to
the following function and then answer these questions about containers and
sorting:
public static void sortDogs(Dog [] dogs, int n) {
int i,j;
Dog temp;
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++)
if (dogs[i]<dogs[j]) {
temp= dogs[i];
dogs[i]= dogs[j];
dogs[j]= temp;
}
}
- (2 pts) What is the name of the sort depicted above?
- (2 pts) Upon completion of the sortDogs() function, what order
would the objects in the dogs parameter be placed?
- (6 pts) Write a standard .compareTo() method for the Dog
class that will compare dogs by comparing their weight.
- (3 pts) Show how the if-statement in the sort above would be modified
to use a standard .compareTo() method to cause the sort to list
the dogs in order of weight with heaviest weight first.
- (8 pts) Write a section of code that will declare/create a Java
ArrayList to hold Dog objects. Insert 4 Dog objects into
that ArrayList. Then use a loop to iterate the ArrayList and
display to the screen each dog object. You can assume an appropriate
.toString() method has been defined for the Dog class.
- (6 pts) Show how the following array would be processed if it were
partitioned one time using the partition algorithm that accompanies
Quicksort.
(NOTE: This question is asking you to partition the array once ... not
do an entire quicksort).
- (4 pts) Using words, give a recursive description of how a binary search
finds an element in a sorted list.
- Use the ListNode class given at the beginning of the exam to
to answer these questions.
- (8 pts) Write a method called removeLast that will accept
the head of a linked list (i.e., a ListNode) as a parameter
and will traverse to the end of the list and remove the last element
of the list.
- (6 pts) Write a method called isOrdered that will accept the
head of a linked list as a parameter and will return true if the
elements in the list are arranged from smallest to largest. It should
return false otherwise. If the list is empty it should return true.
- Answer these questions about stacks and the MyStack class given
above.
- (4 pts) Suppose that when a letter is encountered we push it on the
stack and when an asterisk is encountered we pop the stack. Show
the sequence in which letters would be popped for the following string:
RM*RAEE**RNIT***IC***LC*U*****MO*O**
- (4 pts) Contrast an array-based stack implementation with a list-based
stack implementation. Give advantages of each over the other.
- (10 pts) Write a method called matchParens()
that accepts a String parameter containing an expression
with square brackets and braces (e.g., “[[x+y-z]+[a-b]/c]+5-2]”).
The method will return true if the brackets are properly matched
and false if not.
Assume the existence of a working MyStack class
outlined on the first page of this exam. Use the stack to help
determine whether or not the expression is valid.
- Answer these questions about queues.
- (4 pts) Suppose that when a letter is encountered we put it in the
queue and when an asterisk is encountered we remove from the queue.
Show the sequence in which letters would be removed for the following
string:
RM*RAEE**RNIT***IC***LC*U*****MO*O**
- (4 pts) Briefly describe (using words) how to provide an efficient
array-based solution to implementing a queue.
- (4 pts) Which homework assignment in this course was your favorite? Why?
- (4 pts) Which topic in this course was your favorite?