Question: Use the Java hierarchy you posted in week 5 (corrected based on any feedback you may have received). Add a user-defined exception that can be

Use the Java hierarchy you posted in week 5 (corrected based on any feedback you may have received). 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 { public static void main(String[] args) { // 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;

}

}

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!