Consider the following UML class diagram BaseballPlayer -name: String position :
ID: 3718082 • Letter: C
Question
Consider the following UML class diagram BaseballPlayer -name: String position : String - numAtBats: - numSingles: Integer - numDoubles: Integer - numTriples: Integer - numHomeRuns : Integer - battingAverage: Double Integer + computeBattingAverage0 Implement the class with the single method computeBattingAverage) which computes the player's batting average as a double value. A player's batting average is the total number of hits (singles, double, triples, home runs) divided by the number of total number of at bats. The result should be stored into the appropriate field of the class. Make sure your solution would not cause a division by e error. Paragraph upExplanation / Answer
// Creating the class with name BaseballPlayer, as written on top of UML class diagram
public class BaseballPlayer {
//Creating variables
//They will be private because of a negative sign in front of them.
private String name;
private String position;
private int numAtBats; // I am assuming it is of type int and not Integer. They both are different in java.
private int numSingles;
private int numDoubles;
private int numTriples;
private int numHomeRuns;
private double battingAverage; //I am assuming it is of type double and not Double. They both are different in java.
public void computeBattingAverage() {
//implementing a try catch block
try {
//writing logic for computing battingAverage
battingAverage = (numSingles + numDoubles + numTriples + numHomeRuns)/ numAtBats;
}
catch(ArithmeticException e){
System.out.println("Division by 0 not possible");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.