Upload
adia
View
40
Download
0
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
Midterm Test Overview
CS221 – 3/23/09
0 20 40 60 80 100 1200
0.5
1
1.5
2
2.5
3
3.5
Scatter Chart of Midterm GradesHigh - 101
Low - 2Mean - 68
Median - 71
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*@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*/
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
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.
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
Question 2 Codepublic 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; }
Quark public class Quark{ public Bonzo quarker(Bonzo x, Bonzo y) { if (x.size < y.size) return x; else return y; }}
Quark Key Points
• CompareTo• Return e• Take e params• Declare class e• Require e to extend comparable
Generic Quark
public class Quark<E extends Comparable<Object>>{ public E quarker(E x, E y) { if (x.compareTo(y) == -1) return x; else return y; }}
Bonzo
public class Bonzo{ public int size; public String name; public Bonzo(int givenSize, String givenName) { size = givenSize; name = givenName; }}
Bonzo Key Points
• Implements Comparable• compareTo method
Generic Bonzopublic class Bonzo implements Comparable<Object>{ 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;}
}}
Mainpublic 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 Mainpublic 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 Togetherpublic 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(E x, E y) { if (x.compareTo(y) == -1) return x; else return y; }}
public class Bonzo implements Comparable<Object>{ 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;}
}}
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
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.
Question 3 Code public <T extends Comparable <T>> 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; }
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
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
Selection Sort
Common Mistakes
• Many people forgot the swap and inserted instead
• Should be: 35197->15397• Not: 35197->13597
Insertion Sort
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
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.
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 Codepublic 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;}
}
Outputn=5n=4n=3n=2n=1result=2result=4result=8result=16result=32RecursivePower 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?