{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

test1printS08

# test1printS08 - Compsci 6 PROBLEM 1 Test 1(Mystery Repeat...

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

Compsci 6 Test 1 Spring 2008 PROBLEM 1 : ( Mystery Repeat Repeat Repeat: (22 pts)) PART A (12 pts) : Consider the following Mystery method. public int Mystery (String phrase) { int pos = phrase.indexOf("e"); int pos2 = phrase.indexOf("e",pos+1); System.out.println(phrase.substring(pos,pos+3)); return phrase.substring(pos2).length(); } A . What type is the return value for the method Mystery ? B . How many parameters are there? C . For the call Mystery(‘‘GoeDukeiea’’) , list first what is printed as output and list second the return value. D . For the call Mystery(‘‘eeeee’’) , list first what is printed as output and list second the return value. E . Describe in words what the method Mystery does. F . Give an example value for phrase that will cause the function Mystery to crash. Explain why it crashes. PART B (10 pts) : Consider the following Mystery2 method. public int Mystery2(ArrayList<Integer> numbers) { int x = 0; for (Integer num: numbers) { if (num < 6) { x += num; } } return x; } A . List the names of the local variables in Mystery2 . B . What is the return type of Mystery2 ? 1

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

View Full Document
C . Suppose values is an ArrayList < Integer > and has the values 8, 4, 9, 2 and 3 stored in this order from position 0 to position 4. What is the return value of the call Mystery2(values) ? D . Describe in words what the method Mystery2 does. E . Explain why the the ArrayList is of type Integer instead of type int. PROBLEM 2 : ( Don’t forget the middle: (8 pts)) Complete the method InsertMiddle that is given two string parameters. The first string is a name consisting of a first name and a last name separated by one blank. The second string is a middle name. This method returns the name with the middle name inserted between the first and last name. For example, InsertMiddle(“Sarah Forth”, ”Go”) would return the string ”Sarah Go Forth”. You can assume that there is exactly one blank in name , between the first name and last name. public String InsertMiddle(String name, String middle) { } PROBLEM 3 : ( Living on Campus: (10 pts)) Consider the following two classes. public class Dorm { private String myName; private int myNumRooms; private int myNumFloors; public Dorm(String name, int numRooms, int numFloors) { myName = name; myNumRooms = numRooms; myNumFloors = numFloors; } public String getName() { return myName; } 2
public int getNumRooms() { return myNumRooms; } public int getNumFloors() { return myNumFloors; } } public class AthleticDorm extends Dorm { private String mySport; public AthleticDorm(String name, int numRooms, int numFloors, String sport) { super(name, numRooms, numFloors); mySport = sport; } public String getName() { return super.getName() + ": the " + getSport() + " dorm"; } public String getSport() {

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