homework6_001

homework6_001 - 15-121 FALL 2009 [CORTINA]HOMEWORK 6 - due...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121 FALL 2009 [CORTINA]HOMEWORK 6 - due Wednesday, October 28 by 11:59PMELECTRONIC HANDIN AVAILABLE THURSDAY BY 5PMPROBLEMS (10 pts)1. (1 pt) The following question deals with iterators used on array lists. a. The variable numsrefers to an ArrayList<Integer>containing the following integers: [17, 8, 20, 44, 36, 40, 25, 28]What is printed by the following code segment? Iterator<Integer> it = nums.iterator();while (it.hasNext()) {if (it.next() % 10 <= 5)it.remove();}System.out.println(nums);b. Let namesbe an ArrayList<String>. Use an enhanced for-loop to print out all of the strings in this array list that start with the letters 'CMU'. You may assume all strings are at least 3 characters long. 2. (2 pts) Write a method removeDuplicatesthat removes duplicate values from the list of integers specified in the parameter. Use a pair of ListIteratorvariables to traverse the specified list. Consider using the methods hasNext, next, nextIndex, and removeand the constructor ListIterator(int index). To avoid a ConcurrentModificationException, you should call remove on the iterator for the outer loop. public static void removeDuplicates(List<Integer> list) {}3. (1 pt) Bart wants to create a subclass of ArrayListcalled BartArrayListthat is an arraylist that has an addoperation that not only adds an element to the end of the list, but it also removes the first element. He decides to override the addmethod as shown below: public boolean add(E element) {add(element); // add to parent array listremove(0); // remove first element from parent array listreturn true; } Explain why this implementation is incorrect. What happens if he tries to run this method? 4. (1 pt) Consider the following recursive function defined for n >= 0: public static int trib(int n) {if (n <= 2) return n;return trib(n-1) + trib(n-2) + trib(n-3); } a. What is the return value for trib(8)? Show your work to illustrate the recursive calls that are made. b. What is the runtime complexity in big-O notation of the tribmethod as a function of n? (HINT: Think about how many recursive calls are made for each method execution.) 5. (1.5 pts) Consider the following recursive mystery method below. public int mystery1(int a, int b) {if (a < b) {return a;} else {return mystery1(a - b, b);}}a. What is the value returned for each of the following method calls? Show your work by tracing the recursive method calls that are generated. mystery1(3, 5);mystery1(9, 6);mystery1(13, 4);mystery1(8, 2);b. State what the mystery1method is computing. 6. (1.5 pts) Consider the following recursive mystery method below. public int mystery2(String str) {if (str.length() < 3) return 0;int x = mystery2(str.substring(1));if (str.charAt(0) == str.charAt(2))return 1 + x;elsereturn x;}a. What is the value returned for each of the following method calls? Show your work by tracing the recursive method calls that are generated....
View Full Document

Page1 / 7

homework6_001 - 15-121 FALL 2009 [CORTINA]HOMEWORK 6 - due...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online