test2printS08

# test2printS08 - Compsci 6 PROBLEM 1 Test 2(Repeat Repeat...

This preview shows pages 1–4. Sign up to view the full content.

Compsci 6 Test 2 Spring 2008 PROBLEM 1 : ( Repeat Repeat Repeat: (10 pts)) PART A (5 pts) : Assume an ArrayList named values contains the following eight numbers: 4 5 22 7 15 31 40 24 int c = 0; for (int k=0; k < values.size(); k++) { if (values.get(k) % 5 == 0) { c = c + values.get(k); } } a. Given the ArrayList values above, what is the value of c when the loop ends? b. Give a meaningful loop invariant for this code. PART B (5 pts) : Assume an ArrayList named words contains the following eight strings: "computer" "science" "ipod" "music" "go" "fun" "dance" "no" For this ArrayList, consider the following code. int c = 0; for (int k=0; k < words.size(); k++) { c += words.get(k).length(); } a. What is the value of c after the loop ends? b. Give a meaningful loop invariant for this code. PROBLEM 2 : ( Put Prof. Rodger Behind a Window (10 pts)) Write the execute method of Window whose header is given below. This method takes a color Pixmap image and modiﬁes the picture to put thick black horizontal and vertical lines about 20 pixels wide in the middle of the picture to look like a window frame. 1

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

View Full Document
For example, in the ﬁgure below, a color picture is shown on the left and the same image is shown on the right after the Window method has executed. Note that the color Black is when the red, green and blue all have values of zero. public class Window extends Command { // code not shown that is not needed public void execute (Pixmap target) { Dimension bounds = target.getSize(); // size of pixmap // TODO: complete method below } } PROBLEM 3 : ( A Zoo full of Animals (10 pts)) This problem refers to strings of animal types such as "Bear" . Write the method NumberUniqueTypes that is given an ArrayList of TreeSets of animal types and returns the unique number of animal types in the ArrayList. Consider the following ArrayList of sets called zoos that contains the following 4 sets. Set 1: "Bear" "Crocodile" "Fox" Set 2: "Rhino" "Elephant" "Zebra" "Bear" Set 3: "Fox" "Elephant" "Zebra" "Ostrich" Set 4: "Rhino" "Elephant" "Crocodile" "Kangaroo" For example, the call NumberUniqueTypes(zoo) would return 8 as there are 8 unique animals through the four sets. // Given an ArrayList of Sets of animal types - return the number of 2
// unique animals over all the sets. public int NumberUniqueTypes (ArrayList <TreeSet <String> > zoolists) { PROBLEM 4 : ( Where do you live? (50 pts)) Consider the following problem about housing groups on a college campus, where the in- formation stored about each member of the house is their name and the type of car they have. PART A: (8 pts) Consider the Member class to store information about one member of the housing group, in particular their name and the type of car they drive. Fill in the missing code for all the methods in the class.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

test2printS08 - Compsci 6 PROBLEM 1 Test 2(Repeat Repeat...

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

View Full Document
Ask a homework question - tutors are online