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
queue = temp;
return check;
}
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.
3. Explain in your own words the concept and process behind the binary search algorithm. When
would you use this algorithm?
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