CompSci 100E, Midterm #1, Oct 4, Fall 2006


Name ____________________  Community Standard Acknowledgment ___________________   





Note that this exam has 10 pages: Check!


Watch your time! You only have 75 minutes. You should not spend too long on any one problem on your first pass.


If you run out of space to do a problem, continue it on the back of the previous page!

Problem Value Points Earned
Prob 1 5              
Prob 2 6              
Prob 3 6              
Prob 4 10              
Prob 5 10              
Prob 6 6              
Prob 7 12              
Prob 8 10              
Prob 9 7              
total 72              


Closed Book


You may refer to reference sheets that have been distributed that are extracted from the Java API. These should provide reminders of some of the standard classes and methods that we have used.









Page 2

Problem 1: Unreal? (5 points)

Complete the method roots whose header is shown below. Given coefficients a, b, c to a quadratic equation, return -1 if the equation has imaginary roots, 1 if the roots are real and 0 if there is only 1 root (i.e., both roots are the same). Note that you are not computing the roots of the equation, just their nature.
Forgot the equation? Ask. int roots(double a, double b, double c) // parameters a > 0, b, and c are the coefficients to a quadratic equation // returns -1 for imaginary roots, +1 for real roots, 0 for 1 root {
Page 3

Problem 2: Trace that Call (6 points)

Below is a complete Java program. Show what output it will produce on the console. (Note the recursion. Also distinguish println from print.) public class Hailstone { public void recHailstone(int n){ System.out.print(" " + n); if (n <= 1) { System.out.println(); return; } if (n % 2 == 0) recHailstone(n / 2); else recHailstone(3 * n + 1); } public static void main(String[] args) { Hailstone h = new Hailstone(); h.recHailstone(3); } }
Page 4

Problem 3: Repetitive? (6 points)

We'll say that a "triple" in a string is a char appearing three times in a row. Return the number of triples in the given string.
countTriple("abcXXXabc") returns 1
countTriple("xxxabyyyycd") returns 3
countTriple("a") returns 0
countTriple("") returns 0
countTriple("XXXXXabc") returns 3
Your method has been started for you below with its header. public int countTriple(String str) {
Page 5

Problem 4: Stand Out (10 points)

Complete the method mode whose header is shown below. mode takes an array of strings and looks for the position in the array of the element that is duplicated most often. It returns the first index of the most frequently occurring element. In case of a tie, the position of the first, most common string is returned.

For example, given an array
{"one", "too", "not", "too", "three", "not"},
mode should return 1, the index of "too"". (Both "too" and "not" occur twice, but "too" came first.)

int mode(String[] text) // pre: text contains text.length words // post: return the index of the most frequently occurring word in text {
Page 6

Problem 5: Clean Up (10 points)

Part A (4 pts)

What does the following method return given the input array:
{"A", "A", "A", "A", "B", "A", "C", "C", "C"}?
static String[] comp4 (String[] sa){ ArrayList<String> d = new ArrayList<String>(Arrays.asList(sa)); for (int k=1; k<d.size(); k++){ if (d.get(k-1).equals(d.get(k))){ d.remove(k-1); } } return d.toArray(new String[0]); }

Part B (6 pts)

Write a program, similar to that above, which returns a new array of strings, with conatins all the unique members of the input array, in the same order, but with duplicate consecutive entries removed. Use the remove() method, and make just one pass over the input. As an example, using the demo array from above, comp2 must return {"A", "B", "A", "C"}.

Start with the header shown below;

public static String[] comp2 (String[] sa){
Page 7

Problem 6: It's about the alpha and the omega. (6 points)

Return true if the group of N numbers at the start and end of the array are the same. For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume that n is in the range 0..nums.length inclusive.
sameEnds({5, 6, 45, 99, 13, 5, 6}, 1) returns false
sameEnds({5, 6, 45, 99, 13, 5, 6}, 2) returns true
sameEnds({5, 6, 45, 99, 13, 5, 6}, 3) returns false
sameEnds({1, 2, 5, 2, 1}, 0) returns true
sameEnds({1, 1, 1}, 3) returns true
sameEnds({}, 0) returns true
Your method has been started for you below with its header public boolean sameEnds(int[] nums, int len) {
Page 8

Problem 7: Oh so Big! (12 points)

For each the following code fragments, determine the appropriate Big Oh. (Remember that it must be a "tight" fit in the sense that something O(N) is also O(N2) and O(N3), etc. In this case, only O(N) would be considered correct.)
Page 9

Problem 8: Going to seed (10 points)

Assume you are working on a class called ACCSchool with the following instance variables:
public int winsACC, lossesACC, winsNC, lossesNC;
public String schoolName;
where the ints give you the number off wins and losses in the ACC and Non-Conference. You plan to sort an array of ACC schools to get the ACC tournament seeding. Remember that the lower the seed, the better the team. On sorting, the best team should end up first in the array.

Write a complete comparator to help you sort an array of ACCSchools. Name it ACCComparator and have it implement Comparator. Remember, it only needs to include a single method.

(If you don't know how to do this, but do know how to make the class ACCSchool comparable, then you may choose that option, but you forfeit 2 points. In that case, write the method needed to make ACCSchool implement Comparable.)

Here are the rules to compare two schools (not the official ones). Base it on:

  1. ACC wins
  2. If tied, use ACC losses (usually the same, but...)
  3. If still tied, use the win ratio for non ACC games: winsNC/(winsNC + lossesNC)
  4. If still tied, use alphabetical order of the school's name.
Write the required code below.
Page 10

Problem 9: A man, a plan, a canal: Panama! (7 points)

Write the method isPalindrome which determines whether or not an integer is unchanged when its digits are reversed.
isPalindrome(121) returns true;
isPalindrome(12321) returns true;
isPalindrome(11) returns true;
isPalindrome(0) returns true;
isPalindrome(1232) returns false;
isPalindrome(2321) returns false;
To earn full credit for this problem you may not use any methods that you do not write and may not use any of the classes and methods in the Java API. You may use an array. You can earn two points of extra credit if you do it without using an array.

Your method has been started for you below with its header

boolean isPalindrome(int n) {