Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Midterm Test Overview CS221 – 3/23/09

2

3 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

4 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.

5 Javadoc Key Points /** notation Description of the method @param @return @throws

6 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 *@param b The variable B in ax^2+bx+c=0 *@param c 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 */

7 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

8 Question 2 a)What are generics in Java? b)Why are generics useful? c)Modify the code below to make the Quark class generic and allow the rest of the program to use the generic class.

9 Generics a)Allow a class to use any type of object while retaining the advantages of strong typing b)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

10 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(int givenSize, String givenName) { size = givenSize; name = givenName; } public class Quark { public Bonzo quarker(Bonzo x, Bonzo y) { if (x.size < y.size) return x; else return y; }

11 Quark public class Quark { public Bonzo quarker(Bonzo x, Bonzo y) { if (x.size < y.size) return x; else return y; }

12 Quark Key Points CompareTo Return e Take e params Declare class e Require e to extend comparable

13 Generic Quark public class Quark > { public E quarker(E x, E y) { if (x.compareTo(y) == -1) return x; else return y; }

14 Bonzo public class Bonzo { public int size; public String name; public Bonzo(int givenSize, String givenName) { size = givenSize; name = givenName; }

15 Bonzo Key Points Implements Comparable compareTo method

16 Generic Bonzo public class Bonzo implements Comparable { public int size; public String name; public Bonzo(int givenSize, String givenName) { size = givenSize; name = givenName; } public int compareTo(Object obj) { Bonzo bonz = (Bonzo) obj; if (size < bonz.size) { return -1; } else if (size == bonz.size) { return 0; } else { return 1; }

17 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); }

18 Main Key Points Must declare type when creating Quark instance

19 Generic 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); }

20 All Together 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 Quark > { public E quarker(E x, E y) { if (x.compareTo(y) == -1) return x; else return y; } public class Bonzo implements Comparable { public int size; public String name; public Bonzo(int givenSize, String givenName) { size = givenSize; name = givenName; } public int compareTo(Object obj) { Bonzo bonz = (Bonzo) obj; if (size < bonz.size) { return -1; } else if (size == bonz.size) { return 0; } else { return 1; }

21 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 here and there

22 Question 3 a)Explain what the mystery method below does. b)Let n be the length of the data array. What is the worst case statement count of mystery in terms of n? Explain. c)What is the time complexity of the statement count that you derived in part b? Explain your answer.

23 Question 3 Code public > boolean mystery(T [] data) { for (int i = 0; i < data.length - 1; i++) { if (data[i].compareTo(data[i+1]) > 0) { return false; } return true; }

24 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

25 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

26 Time Complexity Statement count is 2(n-1)+1 Remove all constants to get big O time complexity Time Complexity is O(n)

27 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

28 Question 4 a)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 b)Draw pictures that capture the main high level steps of how insertion sort would sort the following data into descending order: mantle berra ruth gehrig ford

29 Selection Sort

30 Common Mistakes Many people forgot the swap and inserted instead Should be: 35197->15397 Not: 35197->13597

31 Insertion Sort

32 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

33 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.

34 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!

35 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

36 Question 6 a)Explain under what conditions you should use recursion. b)Explain the potential pitfalls of recursion. c)Write down what the program below will output to the console when run.

37 Use Recursion If it simplifies your solution If the problem can be broken into sub- problems that combine to a larger solution

38 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.

39 Question 6 Code public void testRecursivePower() { int n = 5; int x = 2; int result = -1; int expectedResult = 32; result = Recursion.recursivePower(x, n); System.out.println("RecursivePower returned: " + result); } public static int recursivePower(int x, int n) { 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; }

40 Output n=5 n=4 n=3 n=2 n=1 result=2 result=4 result=8 result=16 result=32 RecursivePower returned: 32

41 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

42 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?

43


Download ppt "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."

Similar presentations


Ads by Google