homework6 - 15-121 FALL 2009[CORTINA/REID-MILLER]HOMEWORK 6...

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/REID-MILLER]HOMEWORK 6 - due Wednesday, October 28 by 11:59PM (Corrected)PROBLEMS (10 pts)1. (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? 2. (1 pt) Consider the following partialimplementation of the Teamclass, which stores the names of the people on a team. public class Team implements Cloneable{private String names;public String getName(int index) {return names[index];}public void setName(int index, String name) {names[index] = name;}public Object clone() {try {Team copy = (Team)super.clone();// INSERT CODE HERE FOR PART (b)return copy;}catch(CloneNotSupportedException e) {//should never get herethrow new RuntimeException(e);}}}a. The variable teamis initialized to refer to a new Team, and then the following code segment is executed: team.setName(0, "Ward");team.setName(1, "Roethlisberger");Team backup = (Team)team.clone();team.setName(0, "Polamalu");System.out.println(team.getName(0));System.out.println(team.getName(1));System.out.println(backup.getName(0));System.out.println(backup.getName(1));What is printed by this code segment based on the code above? b. What code must be inserted in clonemethod of the Teamclass so that it correctly returns a deep copy? 3. (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. 4. (2 pts) Write a method removeDuplicatesthat removes duplicate values from the list of integers specified in the parameter. Use nested loops that 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 of the outer loop. 5. (1 pt) Consider the following recursive mystery method below....
View Full Document

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Page1 / 8

homework6 - 15-121 FALL 2009[CORTINA/REID-MILLER]HOMEWORK 6...

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