1 / 43

Midterm Test Overview

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.

adia
Download Presentation

Midterm Test Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Midterm Test Overview CS221 – 3/23/09

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

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

  4. Javadoc Key Points • /** notation • Description of the method • @param • @return • @throws

  5. 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 */

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

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

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

  9. 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; }

  10. Quark public class Quark { public Bonzoquarker(Bonzox, Bonzoy) { if (x.size < y.size) return x; else return y; } }

  11. Quark Key Points • CompareTo • Return e • Take eparams • Declare class e • Require e to extend comparable

  12. 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; } }

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

  14. Bonzo Key Points • Implements Comparable • compareTo method

  15. 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; } } }

  16. 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); } }

  17. Main Key Points • Must declare type when creating Quark instance

  18. 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); } }

  19. 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; } }

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

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

  22. 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; }

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

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

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

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

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

  28. Selection Sort

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

  30. Insertion Sort

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

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

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

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

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

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

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

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

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

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

  41. 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?

More Related