DOCX

advertisement
COP 3503 Fall 2012 Exam 2 Sample Questions
1. Write a method called hasInteger which takes in a Queue<Integer> and an Integer. It returns true
or false depending on whether the queue contains that Integer or not. The Queue should remain
unchanged after the method is called.
NOTE: You only have access to the methods enqueue, dequeue and peek. And you can only use
Queues, not any other data structures.
Solution:
public boolean hasInteger(Queue<Integer> queue, Integer num) {
// temporary Queue
Queue<Integer> temp = new Queue<Integer>();
boolean check = false;
while (queue.peek() != null) {
Integer n = queue.dequeue();
if (n == num)
check = true;
temp.enqueue(n);
}
// now the temporary queue holds all the values,so make that the queue
queue = temp;
return check;
}
How it works:
We keep removing from the queue and putting the values in another temporary
queue. As we remove, we compare each value with the one which was passed in,
turning the “check” flag on if we see it. At the end the temporary queue now
holds all the Integers in the proper order, so we just set the queue to that
one.
2. Implement the method void size() for the Linked List class SLL<T>. The method should return the
size of the linked list.
NOTE: You do not have access to any of the other methods inside SLL<T>
Solution:
Look at lecture notes on Data Structures. Solution is not explicitly provided since you have to
implement it for Project 5, but steps are shown in the lecture notes.
3. Explain in your own words the concept and process behind the binary search algorithm. When
would you use this algorithm?
Solution:
The binary search algorithm is used for searching a sorted array.
Since the array is sorted, we start off by comparing the element we are
searching for to the element in the middle of the array. This comparison
lets us know if the element is in the lower-half or the upper-half of the
array. The search space is halved in one comparison. We keep repeating
the process for the sub-arrays by changing the indices we are searching
in. This allows us to quickly arrive at the proper index of the element
we are searching for (if it exists). Binary search thus works extremely
fast compared to linear search.
4. Implement the method void bubbleSort(int[] numbers), which will sort the input array in ascending
order using the Bubble Sort algorithm.
Solution:
Look at lecture notes on Bubble Sort
Use these classes for the following question:
class GeometricObject {
public String color;
public GeometricObject(String color);
}
class Circle extends GeometricObject {
public double radius;
public Circle(String color, double radius);
}
class Rectangle extends GeometricObject {
public double width, height;
public Rectangle(String color, double width, double height);
}
Use the following file format for storing GeometricObjects:
For Circles:
Circle,Color: (color),Radius: (radius)
For Rectangles:
Rectangle,Color: (color),Width: (width),Height: (height)
Example File:
Circle,Color: Red,Radius: 2.5
Rectangle,Color: Black,Width: 10.0,Height: 20.0
Rectangle,Color: Blue,Width: 5.0,Height: 12.5
5. Implement the method public ArrayList<GeometricObject> readObjects(String filename). The
method should read from the filename passed in and return a GeometricObject ArrayList with all
the GeometricObjects from the file in it. The file format is as described in the previous page. (Make
sure you capture all errors)
public ArrayList<GeometricObject> readObjects(String filename) {
File f = new File(filename);
ArrayList<GeometricObject> al = new ArrayList<GeometricObject>();
try {
Scanner s = new Scanner(f);
while (s.hasNextLine()) {
Scanner s2 = new Scanner(s.nextLine(), “,”);
// type
String type = s2.next();
// color
Scanner s3 = new Scanner(s2.next());
s3.next();
String color = s3.next();
// double1
s3 = new Scanner(s2.next());
s3.next();
double num1 = s3.nextDouble();
GeometricObject go = null;
if (type.equals(“Circle”)) {
go = new Circle(color, num1);
}
else {
s3 = new Scanner(s2.next());
s3.next();
double height = s3.nextDouble();
go = new Rectangle(color, num1, height);
}
al.add(go);
}
} catch (IOException e) {
System.out.println(“Some error reading file”);
}
return al;
}
Download