430 likes | 590 Views
Midterm Test Overview. CS221 – 3/23/09. Test Curve. Current median is a 71 or C- Median will be adjusted to an 81 or B- All test scores will be +10 on my final grade sheet If you got a 101 it is a 111 If you got a 75 it is an 85 . Question 1.
E N D
Midterm Test Overview CS221 – 3/23/09
Test Curve • Current median is a 71 or C- • Median will be adjusted to an 81 or B- • All test scores will be +10 on my final grade sheet • If you got a 101 it is a 111 • If you got a 75 it is an 85
Question 1 Supply an appropriate Javadoc header for the following method that calculates the larger of the two roots of the quadratic equation: ax^2 + bx + c = 0. Comments in the body of the method are unnecessary.
Javadoc Key Points • /** notation • Description of the method • @param • @return • @throws
Answer /** *Given a, b, and c in the equation ax^2+bx+c=0, *return the largest of the two roots. *@param a The variable A in ax^2+bx+c=0 *@paramb The variable B in ax^2+bx+c=0 *@paramc The variable C in ax^2+bx+c=0 *@return The larger of the two roots *@throws Exception if the discriminant less than 0 or if A is 0 */
Common Mistakes • Most people got a description and many got the correct /** notation. • Many of you got description + @param • Lack of @throws and/or @return was the most common mistake
Question 2 • What are generics in Java? • Why are generics useful? • Modify the code below to make the Quark class generic and allow the rest of the program to use the generic class.
Generics • Allow a class to use any type of object while retaining the advantages of strong typing • A single class can be used by any number of strongly typed data. For instance a search class could be used on Integers or a custom Customer class Note: generics only work on objects not primitive data types! • For instance you could use Integer but not int
Question 2 Code public class Main { public static void main(String[] args) { Bonzo myBonz1= new Bonzo(6,"bozo"); Bonzo myBonz2= new Bonzo(5,"boza"); Quark quarkPot = new Quark(); System.out.println(quarkPot.quarker(myBonz1, myBonz2).name); } } public class Bonzo { public int size; public String name; public Bonzo(intgivenSize, String givenName) { size = givenSize; name = givenName; } } public class Quark { public Bonzoquarker(Bonzox, Bonzoy) { if (x.size < y.size) return x; else return y; }
Quark public class Quark { public Bonzoquarker(Bonzox, Bonzoy) { if (x.size < y.size) return x; else return y; } }
Quark Key Points • CompareTo • Return e • Take eparams • Declare class e • Require e to extend comparable
Generic Quark public class Quark<E extends Comparable<Object>> { public E quarker(Ex, E y) { if (x.compareTo(y) == -1) return x; else return y; } }
Bonzo public class Bonzo { public int size; public String name; public Bonzo(intgivenSize, String givenName) { size = givenSize; name = givenName; } }
Bonzo Key Points • Implements Comparable • compareTo method
Generic Bonzo public class Bonzo implements Comparable<Object> { public int size; public String name; public Bonzo(intgivenSize, String givenName) { size = givenSize; name = givenName; } public intcompareTo(Objectobj) { Bonzobonz = (Bonzo) obj; if (size < bonz.size) { return -1; } else if (size == bonz.size) { return 0; } else { return 1; } } }
Main public class Main { public static void main(String[] args) { Bonzo myBonz1= new Bonzo(6,"bozo"); Bonzo myBonz2= new Bonzo(5,"boza"); Quark quarkPot = new Quark(); System.out.println(quarkPot.quarker(myBonz1, myBonz2).name); } }
Main Key Points • Must declare type when creating Quark instance
Generic Main public class Main { public static void main(String[] args) { Bonzo myBonz1= new Bonzo(6,"bozo"); Bonzo myBonz2= new Bonzo(5,"boza"); Quark<Bonzo> quarkPot = new Quark<Bonzo>(); System.out.println(quarkPot.quarker(myBonz1, myBonz2).name); } }
All Together public class Bonzo implements Comparable<Object> { public int size; public String name; public Bonzo(intgivenSize, String givenName) { size = givenSize; name = givenName; } public intcompareTo(Objectobj) { Bonzobonz = (Bonzo) obj; • if (size < bonz.size) { return -1; } • else if (size == bonz.size) { return 0; } else { return 1; } } } public class Main { public static void main(String[] args) { Bonzo myBonz1= new Bonzo(6,"bozo"); Bonzo myBonz2= new Bonzo(5,"boza"); Quark<Bonzo> quarkPot = new Quark<Bonzo>(); System.out.println(quarkPot.quarker(myBonz1, myBonz2).name); } } public class Quark<E extends Comparable<Object>> { public E quarker(Ex, E y) { if (x.compareTo(y) == -1) return x; else return y; } }
Common Mistakes • Most of you answered (at least partially) what generics are and why they are useful • The implementation was portion was rough, only a couple of you got it 100% • Most common was to forget the need for comparable, adding few <e> here and there
Question 3 • Explain what the mystery method below does. • Let n be the length of the data array. What is the worst case statement count of mystery in terms of n? Explain. • What is the time complexity of the statement count that you derived in part b? Explain your answer.
Question 3 Code public <T extends Comparable <T>> booleanmystery(T [] data) { for (inti = 0; i < data.length - 1; i++) { if (data[i].compareTo(data[i+1]) > 0) { return false; } } return true; }
What does the code do? • Compares each item in the array to the following item • If the following item is greater than the current item return false • Checks if a generic array is sorted in ascending order
Statement Count • For loop = n-1 • If statement is n-1 • Return (true or false) is 1 • Worst case total is 2(n-1)+1
Time Complexity • Statement count is 2(n-1)+1 • Remove all constants to get big O time complexity • Time Complexity is O(n)
Common Mistakes • Very few got the statement count right. But if you made a good effort I gave you full credit. • A number of people misunderstood what the code was trying to do. • 1 point for minor errors (ascending vs. descending) • 2 points for more major errors
Question 4 • Draw pictures that capture the main high level steps of how selection sort would sort the following data into ascending order: 3 5 1 9 7 • Draw pictures that capture the main high level steps of how insertion sort would sort the following data into descending order: mantle berraruthgehrig ford
Common Mistakes • Many people forgot the swap and inserted instead • Should be: 35197->15397 • Not: 35197->13597
Common Mistakes • More people got this one right • Most common mistake was to sort ascending instead of descending – I gave full credit for this • Another common mistake was to sort the characters instead of the words • Some mixed insertion with selection sort – find lowest and then insert
Question 5 Sort Implementation. Look at the following method skeleton for sort. Finish this method so that it returns a sorted integer array. You must code your own sort implementation, do not use any of the built-in Java sort routines. The order of the integers in x is unknown, but it is known that x contains at least one element. You can use any sort algorithm you’d like. Neither import statements nor comments are necessary.
Sort Code • Look in past lectures, posted code and text book for common sort implementations • Any working sort algorithm resulted in full credit • Some of you wrote text-book correct code, others improvised on the spot – cool to see!
Common Mistakes • Off by one errors in for loops • Forgetting to nest loops on n^2 algorithms like bubble sort • Various logic errors that resulted in unsorted lists • In general I was relaxed in grading this: • 2 for minor mistakes • 5 for logical errors • 10 for multiple or very large logical errors
Question 6 • Explain under what conditions you should use recursion. • Explain the potential pitfalls of recursion. • Write down what the program below will output to the console when run.
Use Recursion • If it simplifies your solution • If the problem can be broken into sub-problems that combine to a larger solution
Recursion Pitfalls • Uses additional memory on the call stack • Has the performance overheard of method calls • Can potentially complicate your implementation. • Can result in stack overflow exceptions.
Question 6 Code public void testRecursivePower() { intn = 5; intx = 2; int result = -1; intexpectedResult = 32; result = Recursion.recursivePower(x, n); System.out.println("RecursivePower returned: " + result); } public static intrecursivePower(intx, intn) { if (n == 0) { return 1; } else { System.out.println("n = " + n); int result = x * recursivePower(x, n-1); System.out.println("result = " + result); return result; } }
Output n=5 n=4 n=3 n=2 n=1 result=2 result=4 result=8 result=16 result=32 RecursivePower returned: 32
Common Mistakes • Math errors – I was generous with these • Interleaving n and result, e.g. • n=5 • result=2 • n=4 • result=4 • … • Omitting n or result output entirely
Extra Credit • What happens if you call recursive power with x=2 and n=Integer.Max_Value? Why? • What happens if you call recursive power with n=5 and x=Integer.Max_Value/4? Why?