Unformatted text preview:

Compsci 6 Test 2 Spring 2007PROBLEM 1 : (Loop de loop: (10 pts))Assume an ArrayList named values contains the following numbers:5 8 1 4 3 10 2PART A (5 pts):Assume item has been declared an integer and assigned a value.int count = 0;for (int k=0; k<values.size(); k++){if (values.get(k) < item){count++;}}a. If item is 7, what is the value of count when the loop ends?b. Give a meaningful loop invariant for this code.PART B (5 pts):For the same ArrayList above, consider the following code.for (int k=1; k < values.size(); k++){values.set(k, values.get(k-1) + values.get(k));}a. List the elements in the ArrayList values after the code executes.b. Give a meaningful loop invariant for this code.PROBLEM 2 : (Rotate Prof. Rodger (10 pts))Write the execute method of Rotate whose header is given below. This method takes acolor Pixmap image and a number (typed in a dialog box) and rotates the picture that manypixels to the right (wrapping around). NOTE: The variable x change is the rotate amount.For example, in the figure below, a color picture is shown on the left and the same image isshown on the right after the Rotate method has been pressed and 150 has been entered.1public class Rotate extends Command{// code not shown that is not neededpublic void execute (Pixmap target){// code not shown that is not neededint x_change = scale.width; // amount to rotateDimension bounds = target.getSize(); // size of pixmapPixmap copy = new Pixmap(target); // Keep a copy to Rotate// TODO: complete method below}}PROBLEM 3 : (Take Me Out to the Ballgame (36 pts))Consider the following abstract Team class for a sports team.public abstract class Team {private String myCoach; // name of coachprivate int myNumGames; // number of games to play for seasonprivate int myNumWins; // number of winsprivate ArrayList<String> myStarters; // list of players to startpublic Team(String coach, int numGames) // constructor{myCoach = coach;myNumGames = numGames;myNumWins = 0;myStarters = new ArrayList<String>();}// Plays a game and returns true if win, returns false if lose2public abstract boolean PlayGame();// selects starting player names and puts them in myStarterspublic abstract void CreateStartingLineup();// returns name of coachpublic String getCoach() { } // code not shown// sets name of coachpublic void setCoach(String coach) { } // code not shown// returns total number of games to playpublic int getNumGames() { } // code not shown// sets number of games to playpublic void setNumGames(int number) { } // code not shown// adds one more win to win totalpublic void AddWin(){myNumWins++;}// returns number of winspublic int getNumWins() { } // code not shown// add a player to the starting lineuppublic void AddPlayerStartingLineup(String name) { } \\ code not shown// clear out the starting lineuppublic void ClearStartingLineup(){myStarters.clear(); // emptys ArrayList}}PART A ( 20 pts):1. Name the method(s) that must be created by a subclass.2. Name the method(s) that are accessor methods3. Name the state variables for the class34. Fill in the code for the following methods from the Team class.// returns name of coachpublic String getCoach(){}// sets name of coachpublic void setCoach(String coach){}// returns total number of games to playpublic int getNumGames(){}// sets number of games to playpublic void setNumGames(int number){}// returns number of winspublic int getNumWins(){}// add a player to the starting lineuppublic void AddPlayerStartingLineup(String name){}PART B (30 pts):4We would now like to create the class BaseballTeam, which uses the class BaseballPlayer.public class BaseballPlayer {private String myName; // name of playerprivate String myPosition; // position to playprivate double myBatAverage; // batting averagepublic BaseballPlayer(String name, String position, double average){myName = name;myPosition = position;myBatAverage = average;}// returns name of playerpublic String getName() { } // code not shown// returns position for player to playpublic String getPosition() { } // code not shown// returns batting average for playerpublic double getBattingAverage() { } // code not shown}public class BaseballTeam extends Team {private ArrayList<BaseballPlayer> myPlayers; // players on the team// constructorpublic BaseballTeam(Scanner input) { } // code not shown// play one game and return true if this team wins, false otherwisepublic boolean PlayGame() { } // code not shown// create a starting lineuppublic void CreateStartingLineup() { } // code not shown// returns an ArrayList of all the players who play this positionpublic ArrayList<String> possiblePlayers(String position) { } // code not shown// returns a set of all the possible positions on this teampublic TreeSet<String> uniquePositions() { } // code not shown}PART B.1 (8 pts):5Complete the constructor for the BaseballTeam class. You should read from a file that hasthe coaches name (one word), the number of games to play (an integer) and a list of players.Each player has a name (one word), a position to play (one word) and a batting average(double). You do not know how many players are on the team.A sample file might be the following. Note in this example, Rodger is the coach, 16 is thenumber of games to play and there are six players on the team.Rodger 16Todisco pitcher 0.250Stecher firstbase .450Stallworth pitcher .185Hauptman catcher .380Nicholson firstbase .410Pavlova firstbase .317Note the parameter input is of type Scanner and has already been connected to a file and isready for reading. You do not need to create a File or Scanner.public BaseballTeam(Scanner input){super("", 0); // must call constructor of super class first// will need to reset these values after reading from input}PART B.2 (8 pts):Complete the BaseballTeam method uniquePositions. This method should return a set ofall the unique positions the players on the team can play.For example, using the previous data file, uniquePositions would return a set with threepositions: pitcher, firstbase, and catcher.Complete uniquePositions below.public TreeSet<String> uniquePositions(){}PART B.3 (8 pts):Complete the BaseballTeam method possiblePlayers. This method is given a type ofposition to play and should return an ArrayList of all the players who can play this position.6For example, using the previous data file, possiblePlayers(”pitcher”) returns an ArrayList oftwo names, Todisco and Stallworth.Complete possiblePlayers below.// returns an ArrayList of all the players who play this


View Full Document

Duke CPS 006 - Test 2

Download Test 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Test 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Test 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?