Question: Add a user-defined exception that can be thrown by one of the methods as part of the validation or error checking. The main method should
Add a user-defined exception that can be thrown by one of the methods as part of the validation or error checking. The main method should then create an instance of the class and call the method in such a way that the exception is thrown (e.g. invalid input or state of the system).
class BaseballPlayer extends Player { // constant for storing the limit for determining status public static final double STATUS_THRESHOLD = 0.25; // extra attributes private int numberOfHits; private int numberOfTimesAtBat; // def constructor public BaseballPlayer() { super(); numberOfHits=0;numberOfTimesAtBat=0; }
public BaseballPlayer(int id, String playerName, String teamName, String position, double salary, double commisionRate, int numberOfHits, int numberOfTimesAtBat) { /** * passing values to super class */ super(id, playerName, teamName, position, salary, commisionRate); this.numberOfHits = numberOfHits; this.numberOfTimesAtBat = numberOfTimesAtBat; }
/* getters and setters */ public int getNumberOfHits() { return numberOfHits; }
public void setNumberOfHits(int numberOfHits) { this.numberOfHits = numberOfHits; }
public int getNumberOfTimesAtBat() { return numberOfTimesAtBat; }
public void setNumberOfTimesAtBat(int numberOfTimesAtBat) { this.numberOfTimesAtBat = numberOfTimesAtBat; }
/** * calculates and return the stats */
public double calculateStatistics() {
if (numberOfTimesAtBat == 0) {
/** * * in case if number of times batted is 0, returning 0, or else it * * will cause an ArithmeticException when divided by zero * */
return 0; }
return (double) numberOfHits / numberOfTimesAtBat; }
/** * * returns true of stats > limit , else false * */ public boolean determineStatus() { if (calculateStatistics() > STATUS_THRESHOLD) { return true; } else { return false; } } }
class BasketballPlayer extends Player { // constant for storing the limit for determining status public static final double STATUS_THRESHOLD = 0.32; // extra attributes private int numberOfShotsMade; private int numberOfShotsAttempted;
public BasketballPlayer() { super(); numberOfShotsMade = 0; numberOfShotsAttempted = 0; }
public BasketballPlayer(int id, String playerName, String teamName, String position, double salary, double commisionRate, int numberOfShotsMade, int numberOfShotsAttempted) { /** * * passing data to super class * */ super(id, playerName, teamName, position, salary, commisionRate);
this.numberOfShotsAttempted = numberOfShotsAttempted;
this.numberOfShotsMade = numberOfShotsMade;
}
public int getNumberOfShotsMade() {
return numberOfShotsMade;
}
public void setNumberOfShotsMade(int numberOfShotsMade) {
this.numberOfShotsMade = numberOfShotsMade;
}
public int getNumberOfShotsAttempted() {
return numberOfShotsAttempted;
}
public void setNumberOfShotsAttempted(int numberOfShotsAttempted) {
this.numberOfShotsAttempted = numberOfShotsAttempted;
}
/** * * calculates and return the stats * */
public double calculateStatistics() {
if (numberOfShotsAttempted == 0)
{
/** * * in case if number of shots attempted is 0, returning 0, or else it * * will cause an ArithmeticException when divided by zero * */ return 0;
}
return (double) numberOfShotsMade / numberOfShotsAttempted;
}
/** * * returns true of stats > limit , else false * */
public boolean determineStatus() {
if (calculateStatistics() > STATUS_THRESHOLD) {
return true;
}
else { return false;
}
}
}
class FootballPlayer extends Player {
// constant for storing the limit for determining status
public static final double STATUS_THRESHOLD = 3.5;
// extra attributes
private int numberOfYards;
private int numberOfRushes;
public FootballPlayer() {
super();
numberOfYards = 0;
numberOfRushes = 0;
}
public FootballPlayer(int id, String playerName, String teamName, String position, double salary, double commisionRate, int numberOfYards, int numberOfRushes) {
/** * * passing data to super class * */
super(id, playerName, teamName, position, salary, commisionRate);
this.numberOfRushes = numberOfRushes;
this.numberOfYards = numberOfYards;
}
public int getNumberOfYards() {
return numberOfYards;
}
public void setNumberOfYards(int numberOfYards) {
this.numberOfYards = numberOfYards;
}
public int getNumberOfRushes() {
return numberOfRushes;
}
public void setNumberOfRushes(int numberOfRushes) {
this.numberOfRushes = numberOfRushes;
}
/** * * calculates and return the stats * */
public double calculateStatistics() {
if (numberOfRushes == 0) {
/** * * in case if number of rushes is 0, returning 0, or else it * * will cause an ArithmeticException when divided by zero * */
return 0;
}
return (double) numberOfYards / numberOfRushes;
}
/** * * returns true of stats > limit , else false * */
public boolean determineStatus() {
if (calculateStatistics() > STATUS_THRESHOLD) {
return true;
}
else { return false;
}
} //overiding example @Override public double calculateCommision() { return super.calculateCommision();
}
}
class Player {
// attributes
private int id;
private String playerName;
private String teamName;
private String position;
private double salary;
private double commisionRate;
// def constructor
public Player() {
id = 0; playerName = "";
teamName = "";
position = "";
salary = 0;
commisionRate = 0;
}
// parameterized constructor
public Player(int id, String playerName, String teamName, String position, double salary, double commisionRate) {
this.id = id;
this.playerName = playerName;
this.teamName = teamName;
this.position = position;
this.salary = salary;
this.commisionRate = commisionRate;
}
// getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() { return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getCommisionRate() {
return commisionRate;
}
public void setCommisionRate(double commisionRate) {
this.commisionRate = commisionRate;
}
/** * * calculates and return the commision * * @return * */
public double calculateCommision() { return salary * commisionRate;
} //overloading exmaple public double calculateCommision(int x) { return (salary * commisionRate)+x;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
