Question

In: Computer Science

Create java class with name BaseballPlayer Instructions for BaseballPlayer class: The BaseballPlayer class is modeled after...

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:

  • Only 3 decimal places are displayed
  • Leading zeros before the decimal place are not displayed.

Identifier:

getFormattedSluggingPercentage

Parameters:

Return Value:

String – a formatted slugging percentage

Other:

Return the formatted batting average using the following rules:

  • Only 3 decimal places are displayed
  • Leading zeros before the decimal place are not displayed

Identifier:

getFormattedOnBasePercentage

Parameters:

Return Value:

String – a formatted on base percentage

Other:

Return the formatted batting average using the following rules:

  • Only 3 decimal places are displayed
  • Leading zeros before the decimal place are not displayed.

Solutions

Expert Solution

/**********************************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 :)


Related Solutions

Create a java class with name Cat. Instructions for Cat class: This class is modeled after...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after a Cat. You should have instance variables as follows: The Cat’s name The number of mice caught by the Cat. Whether or not the Cat is secretly plotting to kill you Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods designed to perform simple conversions. Specifically, you will be writing methods to convert temperature between Fahrenheit and Celsius and length between meters and inches and practicing overloading methods. See the API document for the Conversion class for a list of the methods you will write. Also, because all of the methods of the Conversion class will be static, you should ensure that it is...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT