Loading
notes intro/old submit AP Problems the dump links
 

Topics

 

 

Chapter 4 - Classes and methods - return statements, local vs global variables, getters/setters, private var

Chapter 5 - static, references (passing by value, passing by reference), this, null == or .equals

Chapter 6 - one dimensional int arrays

 

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. Create a method add5toEvens that will take in an array of nums and it will add 5 to each even number.

 

3. Challenge: Create a method findSecondHighest that will take in an array of nums, and return the second highest value.

3.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;

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. So if addAB(2) is sent, numDoubles goes up by one and numAtBats does as well. If addAB(0) - this means they didnt get on base, numAtBats goes up by one only.  
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)

In another class, create a baseball player named ryan, 1st base.

  • give him 1 double, 2 strikeouts (send down 0 for base)
  • print out his info (call toString() )