CS 117, Winter 2002, Exam #2 (100 points)	Name:_____________________________

1. (a) Suppose that an array contains 64 elements. What are the best case and worst case numbers of comparisons for a successful search using sequential search? (12 points)

1. (b) What are the best case and worst case numbers of comparisons for a successful search using binary search? (13 points)

2. What is output from the following program? Show your work to receive partial credit, and show the output in the box. (25 points)

class TestClass
{
    public static int mystery(int w, int b, int many[])
    {
        w = 9;
        b = -31;
        int z = 29;
        int y = 64;
        many[1] = 99;
        many = new int[3];
        many[0] = 5;
        many[1] = 10;
        many[2] = 15;
        return w;
    }

    public static void main(String[] args) {
        int w = 3;
        int x = -97;
        int y = 42;
        int[] list = {3, 10, -8};
        int z = mystery(w,x,list);
        System.out.println(w);
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        for (int i=0; i < list.length; i++)
            System.out.println(list[i]);
    }
}

3. Write a method to read a file of strings, sort them, then print them to the screen. The very first line of the input file contains the number of strings in the file. For example, the input file might look like this: (25 points)

4
Hope
You
Enjoy
This!

The Java Console and File/Input Output Cheat Sheet from the class web page is attached to the back of this exam. Feel free to use it as a reference. You should not use basicio.

Here is the method header to get you started. Indicate in a comment which sorting algorithm you are coding.

public void readSortAndPrint(String filename)
{

4. Consider the following classes:

class Node
{
  public int data;
  public Node next;
}

class CS117LinkedList
{

  // Data values
  private Node head;

  public void enigma(int number) 
  {
    Node node = new Node();
    node.data = number;
    node.next = head;
    head = node;
  }
}

(a) What is the purpose of enigma (10 points)?

(b) Write the code for another method in CS117LinkedList called display that will print out all the numbers contained in the linked list. Here is the header to get you started. (15 points)

public void display()
{

Java Console and File Input/Output Cheat Sheet
Console Output
System.out.print("Hello ");
System.out.println("world");
Console Input
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String text = in.readLine();
File Output
File dstFile = new File("K:\\location\\outputfile.txt");
PrintWriter out = new PrintWriter
         (new BufferedWriter(new FileWriter(dstFile)));
out.print("Hello ");
out.println("world");
out.close();
File Input
File srcFile = new File("K:\\location\\inputfile.txt");
BufferedReader in = new BufferedReader(new FileReader(srcFile));
String text = in.readLine();
in.close();
Converting input data
String text = in.readLine();
int x = Integer.parseInt(text);
double y = Double.parseDouble(text);
Reading until EOF
while (in.ready()) {
 text = in.readLine();
 System.out.println(text);
}
Pulling apart delimited items on a line
String text = "Beggars in Spain*Nancy Kress*1992";
StringTokenizer tokenizer = new StringTokenizer(text,"*");
String title = tokenizer.nextToken();
String author = tokenizer.nextToken();
String year = tokenizer.nextToken();

String letters = "a b c d e f g h i j";
StringTokenizer tokenizer = new StringTokenizer(text," ");
String allText[] = new String[10];
int pos = 0;
while (tokenizer.hasMoreTokens())
     allText[pos++] = tokenizer.nextToken();