43
Midterm Test Overview CS221 – 3/23/09

Midterm Test Overview

  • 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

Page 1: Midterm Test  Overview

Midterm Test Overview

CS221 – 3/23/09

Page 2: Midterm Test  Overview

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

Page 3: Midterm Test  Overview

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

Page 4: Midterm Test  Overview

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.

Page 5: Midterm Test  Overview

Javadoc Key Points

• /** notation• Description of the method• @param• @return• @throws

Page 6: Midterm Test  Overview

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

Page 7: Midterm Test  Overview

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

Page 8: Midterm Test  Overview

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.

Page 9: Midterm Test  Overview

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

Page 10: Midterm Test  Overview

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

Page 11: Midterm Test  Overview

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

Page 12: Midterm Test  Overview

Quark Key Points

• CompareTo• Return e• Take e params• Declare class e• Require e to extend comparable

Page 13: Midterm Test  Overview

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

Page 14: Midterm Test  Overview

Bonzo

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

Page 15: Midterm Test  Overview

Bonzo Key Points

• Implements Comparable• compareTo method

Page 16: Midterm Test  Overview

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

}}

Page 17: Midterm Test  Overview

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

Page 18: Midterm Test  Overview

Main Key Points

• Must declare type when creating Quark instance

Page 19: Midterm Test  Overview

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

Page 20: Midterm Test  Overview

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

}}

Page 21: Midterm Test  Overview

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

Page 22: Midterm Test  Overview

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.

Page 23: Midterm Test  Overview

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

Page 24: Midterm Test  Overview

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

Page 25: Midterm Test  Overview

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

Page 26: Midterm Test  Overview

Time Complexity

• Statement count is 2(n-1)+1• Remove all constants to get big O time

complexity

• Time Complexity is O(n)

Page 27: Midterm Test  Overview

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

Page 28: Midterm Test  Overview

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

Page 29: Midterm Test  Overview

Selection Sort

Page 30: Midterm Test  Overview

Common Mistakes

• Many people forgot the swap and inserted instead

• Should be: 35197->15397• Not: 35197->13597

Page 31: Midterm Test  Overview

Insertion Sort

Page 32: Midterm Test  Overview

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

Page 33: Midterm Test  Overview

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.

Page 34: Midterm Test  Overview

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!

Page 35: Midterm Test  Overview

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

Page 36: Midterm Test  Overview

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.

Page 37: Midterm Test  Overview

Use Recursion

• If it simplifies your solution • If the problem can be broken into sub-

problems that combine to a larger solution

Page 38: Midterm Test  Overview

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.

Page 39: Midterm Test  Overview

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

}

Page 40: Midterm Test  Overview

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

Page 41: Midterm Test  Overview

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

Page 42: Midterm Test  Overview

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?

Page 43: Midterm Test  Overview