Public Boolean Hasinteger(Queue<Integer> Queue, Integer Num)
Total Page:16
File Type:pdf, Size:1020Kb
COP 3503 Fall 2012 Exam 2 Sample Questions
1. Write a method called hasInteger which takes in a Queue
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
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
NOTE: You do not have access to any of the other methods inside SLL
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
ArrayList
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; }