test2s10short

test2s10short - CompSci 6 Test 2 Spring 2010 PROBLEM 1 : (...

Info iconThis preview shows pages 1–5. 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

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: 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 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 { super(name, height); myNickName = nick; updateStamina(10); } public String getName() // return name of player { return "NCAA champ: " + super.getName(); } public void workout() { updateStamina(-1); System.out.println(getName() + " super workout ..."); } public void praise() { System.out.println("Great Game " + myNickName); } } PROBLEM Consider the following code segments. There are no syntax errors such as missing semicolon, but there may be errors related to how inheritance should work. For each code segment, if there is no error, then give the output. If there is an error then explain which line gives the error and why. 1. BasketballPlayer gordon = new BasketballPlayer("Hayward", 81.0); gordon.workout(); gordon.rest(); System.out.println(gordon.getName() + " " + gordon.getStamina()); 2. DukeBasketballPlayer brian = new DukeBasketballPlayer("Zoubek", 85.0,"Zoob"); brian.workout(); brian.rest(); brian.praise(); System.out.println(brian.getName() + " " + brian.getStamina()); 3. BasketballPlayer jon = new DukeBasketballPlayer("Scheyer", 77.0, "Mr. Basketball"); jon.workout(); jon.rest(); System.out.println(jon.getName() + " " + jon.getStamina()); jon.praise(); 4. DukeBasketballPlayer kyle = new BasketballPlayer("Singler", 80.0); kyle.workout(); kyle.rest(); System.out.println(kyle.getName() + " " + kyle.getStamina()); kyle.praise(); 3 5.5....
View Full Document

This document was uploaded on 01/17/2012.

Page1 / 12

test2s10short - CompSci 6 Test 2 Spring 2010 PROBLEM 1 : (...

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

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