Loading
notes intro/old submit AP Problems the dump links
 

Topics

 

Chapter 3 - For , while loops

Chapter 4 - Classes - return, local vs global, getters/setters, private var

Chapter 5 - static interface (comparable), references, this, null == or .equals

 

Sample free response questions:

 

1. Create a method called findAverage that takes in no parameters and return a double. Prompt the the user for numbers repeatedly, until they hit -1. Use a method (declared elsewhere) readInt() that will read an integer (no need to use scanner). When -1 (the sentinel) is pressed, display and return the average. Be careful to avoid the divide by 0 error.

2.For the question(s) below, write the requested portions of a class called BaseballPlayer. This class contains the following instance variables:

public class BaseballPlayer


private String name;
private String position;
private int numAtBats;
private int numSingles;
private int numDoubles;
private int numTriples;
private int numHomeRuns;
private double battingAverage;

 

public BaseballPlayer(arguments)

{

//code

}

 

public void addAB(int base)

{

}

 

public double computeAvg()

{

}

 

public String toString()

{

}

 

}


a)  Write the constructor, which is passed the player's name and position.  

b)  Write a method that computes the player's batting average, which is the total number of hits (singles, double, triples, home runs) divided by the number of at bats. Make sure that your method does not cause a division by zero error!  

c)  Write a toString method that returns the player's name, position and batting average  
d) Write a method void addAB(int base) where the base the player reaches (either 0-4) is sent down, and the appropriate variables are incremented.  
e) Create a static variable to keep track of the number of allHomeRuns hit by all players.  
f) Create a static method called getAllHomeRuns that will return num of all homeruns hit  
g) Make it implement the interface comparable; put in all necessary methods and code (it should compare based on average  
h)

Here is another interface

public interface AllStar

{

___public boolean isAllStar();

}

implement this interface and the requirement methods. A player is an allstar if their average is above .300