CS 1711 Recitation Laboratory 9:
Review for Exam 3

Review for Exam 3

There is nothing to turn in for this recitation. You will get full credit for it if you show up and make an honest attempt to do the review problems given here.

The review problems in this recitation are a sample of some of the types of problems you should expect on the exam. The exam may also contain other types of problems as listed in the Week 11 Lecture Review.

Some of the things not covered here include completing partially written class definitions and diagramming the state of variables. Also not covered are tracing and writing the selection and insertion sorts, but you will need to be able to do these on the exam.

The presenter may not finish all of the problems given in the lab. If this is the case, you should finish them on your own.

  1. _ Write a public method, linearSearch, that uses a linear search to find the first position in an array of Strings of a given String. Return -1 if it is not found.
  2. _ Write a public method, linearSearch, that uses a linear search to find the first position in an ArrayList of Strings of a given String. Return -1 if it is not found.
  3. _ Write a public method, binarySearch, that uses a binary search to find a position in an array of Strings of a given String. Return -1 if it is not found. Assume the array is sorted in increasing alphabetical order.
  4. _ Write a public method, binarySearch, that uses a binary search to find a position in an ArrayList of Strings of a given String. Return -1 if it is not found. Assume the ArrayList is sorted in increasing alphabetical order.
  5. _
    1. It takes about 5 seconds to search a given array using a linear search. How long would you expect it to take to search an array of twice the size?
    2. It takes about 5 seconds to sort a different array using a selection sort. How long would you expect it to take to sort an array of twice the size?
    3. It takes about 5 seconds to search a large sorted array using a binary search. How long would you expect it to take to search an array of twice the size?
  6. _ Write a method, findLargest5, that takes a single array of integers as a parameter and returns the value of the largest entry in the array that is divisible by 5. If there are no such values, return -1. Be sure that your method works even if the array only contains negative values.
  7. _ Recall that one method of reading from a file is to make a Scanner from a file name:
    new Scanner(new FileInputStream("myfile"));
    Write a method, readLines, that takes a file name as a parameter and returns an ArrayList containing the lines of the file. Return null if there is an error. You must handle any errors that may occur. The method should never print anything, even if there is an error.
  8. _ Recall that one method of reading from a file is to make a Scanner from a file name:
    new Scanner(new FileInputStream("myfile"));
    Write a method, readLines, that takes a file name as a parameter and returns an array of Strings containing the lines of the file. Return null if there is an error. You must handle any errors that may occur. The method should never print anything, even if there is an error.
  9. _ Recall that one way to create a file is to use a PrintWriter:
    PrintWriter out = new PrintWriter(new PrintWriter(filename,true));
    Write a method, writeLines, that takes two parameters, an array of int and a String. The second parameter is a name of a file to be created. The method will create a file with the given name containing one line for each element of the array. Each line will contain the array index followed by a colon, followed by space, followed by the correspondong element of the array. The method will return true if successful and false otherwise. Handle any possible errors, but do not have the method print anything.
  10. _
    1. Write a method that takes two parameters, a char and an int. It prints the first parameter the number of times given by the second parameter.
    2. Use the method from part a) to write a code segment to output a triangular pattern of asterisks like the one below. The first row has no leading blanks and the last row contains 2 asterisks. Each row has two fewer asterisks than the preceding row. The variable rows contains the number of rows. For rows = 5
      **********
       ********
        ******
         ****
          **