Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dry Run Practice Use Methods with an Insertion Sort

Similar presentations


Presentation on theme: "Dry Run Practice Use Methods with an Insertion Sort"— Presentation transcript:

1 Dry Run Practice Use Methods with an Insertion Sort
AP Java 1/11/2018 Dry Run Practice Use Methods with an Insertion Sort

2 Learning Targets Be able to read programs with methods
Be able to create a program that uses methods and the Insertion Sort.

3 Multiple Choice 1) CTA9 (none). Consider the following static method public static double getSomething(int val) { val = 2 * val; val = val + 2 * val; return val; } Which of the following could be used to replace the body of getSomething so that the modified version will return the same result as the original version for all values of the parameter val. A. return 2 * val; B. return 3 * val; C. return 4 * val; D. return 5 * val; E. return 6 * val; Answer = E

4 2) CTA10 (A1.15). Consider the following static method public static double getSomething(int val) { val = 2 + val; val = val + 3*val; return val; } Which of the following could be used to replace the body of getSomething so that the modified version will return the same result as the original version for all values of the parameter val. A. return 4*val + 2; B. return 4*val + 6; C. return 4*val + 8; D. return 7*val + 6; E. return 7*val + 8; Multiple Choice Answer = C

5 Note how an array is passed to a method.
3) public static void sort(String [] list) { for(int start = 0; start < list.length-1; start++) int index = start; for(int k = start + 1; k < list.length; k++) if(list[k].compareTo(list[index]) > 0) index = k; } String temp = list[start]; list[start] = list[index]; list[index] = temp; Assume the String array words is initialized as shown below. word What will the array word look like after the third pass through the outer loop in the call sort(word)? Note how an array is passed to a method. Bill Joe Sue Ann Mel Zeb C. Zeb Sue Mel Ann Joe Bill

6 Warm up #4 R10 (A3.8) Consider the following method. // precondition: val > 0 public int zoom(int val) { if(val >= 100) return 2 * val; else return zoom(2*val); } What will be returned by the call zoom(30)? A. 60 B. 120 C. 180 D. 240 e. Nothing will be returned because of an infinite recursion.

7 Warm-up #5 R12 (A3.33) Consider the following method. public void printSomething(String str) { if(str.length() <= 1) System.out.print(str); else printSomething(str.subString(1)); System.out.print(str.subString(0, 1)); } What will be printed as a result of the call printSomething("abcd")? A. a B. d C. aaaa D. abcd E. dcba

8 Insertion Sort // a is the name of the array
// nElems stores the number of elements being sorted // This example is for sorting an array of ints int in, out; for(out=1; out<nElems; out++) // out is dividing line { int dummy = a[out]; // dummy Need to modify for sorting different types in = out; // start shifts at out while(in>0 && a[in-1] >= dummy) // until one is smaller, What if sorting Strings? a[in] = a[in-1]; // Slide: shift item right, in--; // go left one position } a[in] = dummy; // Back: insert marked item } // end for

9 Second Insertion Sort Program
Main Body Get 9 scores Call methods Show results Scores in order Low to High Mean, median Push: Mode, standard deviation Push: Handle getting an unknown number (but less than 100) of values. Methods Insertion Sort Mean Median


Download ppt "Dry Run Practice Use Methods with an Insertion Sort"

Similar presentations


Ads by Google