In: Computer Science
Create java class with name BaseballPlayer
Instructions for BaseballPlayer class:
The BaseballPlayer class is modeled after a BaseballPlayer and will contain methods to calculate various statistics based on the stats of a player.
For this class, you will want to use a static variable for storing a DecimalFormat object.
See the API document for the BaseballPlayer class for a list of methods you will write.
API Document
Constructors:
Identifier: |
BaseballPlayer(String name, int number, int singles, int doubles, int triples, int homeRuns, int atBats, int walks, int sacrificeFlies) |
Parameters: |
name – A String representing the player’s name number – An int representing the player’s number singles – An int representing the number of singles the player has hit doubles – An int representing the number of doubles the player has hit triples – An int representing the number of triples the player has hit homeRuns – An int representing the number of homeRuns the player has hit atBats – An int representing the number of times the player has been at bat walks – An int representing the number of times the player has walked sacrificeFlies – An int representing the number of sacrifice fly outs made |
Return Value: |
|
Other: |
Methods:
Identifier: |
getBattingAverage |
Parameters: |
|
Return Value: |
double – the batting average of a player. |
Other: |
Return the batting average for a BaseballPlayer which is calculated as the number of hits divided by the number of at bats. |
Identifier: |
getSluggingPercentage |
Parameters: |
|
Return Value: |
double – the slugging percentage of a player. |
Other: |
Return the slugging percentage for a BaseballPlayer which is calculated with the following formula: ((1 * # of singles) + (2 * # of doubles) + (3 * # of triples) + (4 * # of homeruns)) / # of at bats |
Identifier: |
getOnBasePercentage |
Parameters: |
|
Return Value: |
double – the on base percentage of a player. |
Other: |
Return the on-base percentage for a BaseballPlayer which is calculated with the following formula: (# of hits + # of walks) / (# of at bats + # of walks + # of sacrifice flies) |
Identifier: |
getFormattedBattingAverage |
Parameters: |
|
Return Value: |
String – a formatted batting average |
Other: |
Return the formatted batting average using the following rules:
|
Identifier: |
getFormattedSluggingPercentage |
Parameters: |
|
Return Value: |
String – a formatted slugging percentage |
Other: |
Return the formatted batting average using the following rules:
|
Identifier: |
getFormattedOnBasePercentage |
Parameters: |
|
Return Value: |
String – a formatted on base percentage |
Other: |
Return the formatted batting average using the following rules:
|
/**********************************BaseballPlayer.java***************************/
// TODO: Auto-generated Javadoc
/**
* The Class BaseballPlayer.
*/
public class BaseballPlayer {
/** The name. */
private String name;
/** The number. */
private int number;
/** The singles. */
private int singles;
/** The doubles. */
private int doubles;
/** The triples. */
private int triples;
/** The home runs. */
private int homeRuns;
/** The at bats. */
private int atBats;
/** The walks. */
private int walks;
/** The sacrifice flies. */
private int sacrificeFlies;
/**
* Instantiates a new baseball player.
*
* @param name the name
* @param number the number
* @param singles the singles
* @param doubles the doubles
* @param triples the triples
* @param homeRuns the home runs
* @param atBats the at bats
* @param walks the walks
* @param sacrificeFlies the sacrifice flies
*/
public BaseballPlayer(String name, int number, int
singles, int doubles, int triples, int homeRuns, int atBats,
int walks, int
sacrificeFlies) {
super();
this.name = name;
this.number = number;
this.singles = singles;
this.doubles = doubles;
this.triples = triples;
this.homeRuns = homeRuns;
this.atBats = atBats;
this.walks = walks;
this.sacrificeFlies =
sacrificeFlies;
}
/**
* Gets the batting average.
*
* @return the batting average
*/
public double getBattingAverage() {
return (singles + doubles +
triples + homeRuns) / atBats;
}
/**
* Gets the slugging percentage.
*
* @return the slugging percentage
*/
public double getSluggingPercentage() {
return (singles + doubles * 2 +
triples * 3 + homeRuns * 4) / atBats;
}
/**
* Gets the on base percentage.
*
* @return the on base percentage
*/
public double getOnBasePercentage() {
return (singles + doubles +
triples + homeRuns + walks) / (atBats + walks +
sacrificeFlies);
}
/**
* Gets the formatted batting average.
*
* @return the formatted batting average
*/
public String getFormattedBattingAverage() {
String s = String.format("%.3f",
getBattingAverage());
return s;
}
/**
* Gets the formatted slugging percentage.
*
* @return the formatted slugging percentage
*/
public String getFormattedSluggingPercentage() {
String s = String.format("%.3f",
getSluggingPercentage());
return s;
}
/**
* Gets the formatted on base percentage.
*
* @return the formatted on base percentage
*/
public String getFormattedOnBasePercentage() {
String s = String.format("%.3f",
getOnBasePercentage());
return s;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Gets the number.
*
* @return the number
*/
public int getNumber() {
return number;
}
}
Please let me know if you have any doubt or modify the answer, Thanks :)