15-110 EXAM 3 PRACTICE QUESTIONS

NOTE: The sample questions below do not necessarily represent the exact questions you will receive on the exam, but they cover similar topics that you are expected to understand.

1. A basketball player has the following properties: height in feet and inches (e.g. 6 feet 9 inches), team name, and total points scored.

(a) Complete the following field definitions for a class BasketballPlayer that models a basketball player:

private __________ feet;
private __________ inches;
private __________ teamName;
private __________ totalPoints;

(b) Write a constructor for the BasketballPlayer class that creates and initializes a basketball player with 0 total points and a height in feet and inches and a team name that are supplied by parameters. If the feet or inches supplied is invalid, use a default height of 6 feet 6 inches instead. (Basketball players must be at least 6 feet 0 inches tall and at most 7 feet 11 inches tall.)

(c) Write an accessor for the BasketballPlayer class that returns the current team name of the basketball player.

(d) Write a mutator for the BasketballPlayer class that adds a number of points to the player's total number of points. The number of points to add is given by a parameter to the method. If the number of points to add is negative, this method should not change the total number of points for the player.

2. Suppose that the class BasketballPlayer from problem 1 implements the Comparable interface.

(a) Show how to complete the following required method for the BasketballPlayer class.
public int compareTo(Object obj)
{
	// Compare the height of this player to another player
	// given by the object reference in the parameter. 
	// Returns a negative integer if this player is shorter than
	// the other player.
	// Returns a positive integer if this player is taller than
	// the other player.
	// Returns 0 if this player is the same height as the other 
	// player.

	BasketballPlayer otherPlayer = _____________________________;

        if (this.feet != otherPlayer.feet)

		return _______________________________________;

	else

		return _______________________________________;

}

(b) Write a method that has two parameters representing two basketball players. The method should return the team of the player that is shorter. If both players are the same height, return the team that comes first alphabetically.

3. This question refers to the BasketballPlayer class described in problem 1.

(a) The BasketballPlayer class is a subclass of what class in Java?

(b) Write a toString method for BasketballPlayer so that it overrides the toString that it inherits. This method should return a string with all four properties of a basketball player.

(c) Assume a text file contains data for a group of basketball players. The first line contains a single integer indicating how many players are stored in the file. Each subsequent line of the file contains the team name followed by the height in feet and inches for an individual player, each separated with spaces as follows:

2
Knicks 6 9
Celtics 6 11

Write a method that has one string parameter representing the name of the text file. Return an array of players initialized to the data given in the file (with 0 total points).


Use the following information for Problems 4 and 5:

Let BasketballTeam be a class that maintains a collection of basketball players as follows:

 
public class BasketballTeam
{
    private BasketballPlayer[] playerArray;
    private int numPlayers;

    public BasketBallTeam()
    {
        playerArray = new BasketballPlayer[1];
        numPlayers = 0;
    }
 
    public void insert(BasketballPlayer newPlayer)
    {
        if (playerArray.length == numPlayers)
            doubleLength();
        playerArray[numPlayers] = newPlayer;
        numPlayers++;
    }

    //  other methods go here
}

  1. (a) Complete the following helper method that doubles the size of the playerArray to make room for additional players to be inserted.
    private void doubleLength()
    {
    
    	// Create a new array that is twice the size of the player array
    
            BasketballPlayer[] newArray = _______________________________________;
    
    	// Copy all of the current players into the new array
    
            for (int i=0; i < ________________________; i++)
            {
                    newArray[i] = _____________________________;
            }
    
    	// Use the new array as our player array now
     
            ______________________________________________;
     
    }
    

    (b) If 20 players are inserted into the basketball team and the array starts with a size of 1 initially, what is the length of playerArray afterwards?

  2. Write a method aboveAverage for this class that returns the number of basketball players in the team that have a total number of points that is greater than the team's average for total number of points. If there are no players on the team, return 0. You may assume there is an accessor in the BasketballPlayer class named getTotalPoints that returns the total points of a basketball player.

    public int aboveAverage()
    {
     
     
     
    }
    

    HINT: Break this problem down into 2 simpler steps. First, find the average for the total number of points of all players in the team using a helper method. Then count the number of players that have a total number of points greater than this average.