{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

test2s10short

# test2s10short - CompSci 6 PROBLEM 1 Test 2(Loop de Loop(8...

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

CompSci 6 Test 2 Spring 2010 PROBLEM 1 : ( Loop de Loop (8 pts)) Consider the following code. String [] names = {"Tsu Chi", "Fa La", "Do Re Me", "Sue Rod"}; for (int k=0; k<names.length; k++ ) { String temp = names[k]; names[k] = temp.substring(temp.indexOf(" ")+1); } for (String str: names) { System.out.println(str); } } 1. Give the output for this code. 2. Give a meaningful loop invariant for the first for loop. 3. Suppose the array names is instead initialized to String [] names = {"Duke"}; Give the output if the code above is run with this value for names . PROBLEM 2 : ( Basketball country (15 pts)) Consider the following two classes BasketballPlayer and DukeBasketballPlayer and note the word extends in the second class. 1

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

View Full Document
public class BasketballPlayer { private String myName; // name of player private double myHeight; // height in inches private int myStamina; // low number is low stamina // constructor public BasketballPlayer(String name, double height) { myName = name; myHeight = height; myStamina = 10; } public String getName() // return name of player { return myName; } public double getHeight() // return height of player { return myHeight; } public int getStamina() // return stamina of player { return myStamina; } // update stamina by adding num to it public void updateStamina(int num) { myStamina+= num; } public void workout() { System.out.println(getName() + " working out ..."); updateStamina(-2); } public void rest() { System.out.println("resting ..."); updateStamina(4); } } public class DukeBasketballPlayer extends BasketballPlayer{ private String myNickName; // players nickname // constructor public DukeBasketballPlayer(String name, double height, String nick) 2

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

View Full Document