Hey guys I have a quick question.. So I have a class called Team that is a collection of players and managers for a baseball team, and one of my required methods is to calculate the total amount of Home Runs hit by the batters. Now along with the Team class I have an abstract Employee class, an abstract Player class and a Batter class that extends the Player class I thought this would be fairly easy but it is coming out to be pretty hard my code is as follow:
class Team { public int _numPitcher; public int _numBatter; public String _manager; public ArrayList<Player> team; public Team(String manager) { _numPitcher = 0; _numBatter = 0; _manager = manager; team = new ArrayList<>(); } public String getManager() { return _manager; } public int getPitchers() { return _numPitcher; } public int getBatters() { return _numBatter; } public int getNumPlayers() { return _numPitcher + _numBatter; } public Player getPlayerName(Player name) { Player temp = name; if (team.contains(name)){ return temp; } else { return null; } } public void setPlayer(Player player) { team.add(player); } public void setDelete(Player player) { team.remove(player); } public void setManager(String manager) { _manager = manager; } public double teamBattingAvg(int totalHits, int totalBats) { if (totalBats == 0) { return 0; } else { return (double)(totalHits / totalBats); } } public int totalHRS() { int total; for (int i = 0; i < team.size(); i++) { total += team.get(i).getHomeRuns(); } return 0; } public int teamERA(int runsEarned, int totalInnings) { if (totalInnings == 0) { return 0; } else { return runsEarned / totalInnings; } } public int totalStrikeouts() { return 0; } public double totalSalary() { double total = 0; for (int i = 0; i < team.size(); i++) { total += team.get(i).getSalary(); } return total; } public int teamValue() { return 0; } public String toString() { return _numPitcher + " " + _numBatter + " " + _manager + "\n"; } } abstract class Employee { public String _name; public double _salary; public Employee(String name, double salary) { _name = name; _salary = salary; } public String getName() { return _name; } public double getSalary() { return _salary; } public String toString() { return _name + " " + _salary + "\n"; } } abstract class Player extends Employee { public int _numGames; public Player(String name, double salary, int games) { super(name, salary); _numGames = games; } public int getNumGames() { return _numGames; } public int getDefaultHits() { return 0; } public int getDefaultBats() { return 0; } public int getDefaultInnings() { return 0; } public int getDefaultHomeRuns() { return 0; } public int getDefaultRuns() { return 0; } public int getDefaultStrikeouts() { return 0; } public void setNumGames(int games) { _numGames = games; } abstract public void value(); public String toString() { return _numGames + "\n"; } } class Manager extends Employee { public int _careerWins; public int _careerGames; public Manager(String name, double salary, int wins, int games) { super(name, salary); _careerWins = wins; _careerGames = games; } public int getWins() { return _careerWins; } public int getGames() { return _careerGames; } public double winPercent() { if (_careerGames == 0) { return 0; } else { return (double)(_careerWins / _careerGames); } } public String toString() { return _careerWins + " " + _careerGames + "\n"; } } class Batter extends Player { public int _bats; public int _hits; public int _homeRuns; public Batter(String name, double salary, int games, int bats, int hits, int homeRuns) { super(name, salary, games); _bats = bats; _hits = hits; _homeRuns = homeRuns; } public int getBats() { return _bats; } public int getHits() { return _hits; } public int getHomeRuns() { return _homeRuns; } public double getValue() { return 0; } public double battingAve() { if (_bats == 0) { return 0; } else { return (double)(_hits / _bats); } } public void value() { } public String toString() { return _bats + " " + _hits + " " + _homeRuns + "\n"; } }
main area I am having issues with:
public int totalHRS() { int total; for (int i = 0; i < team.size(); i++) { total += team.get(i).getHomeRuns(); } return 0; } public int teamERA(int runsEarned, int totalInnings) { if (totalInnings == 0) { return 0; } else { return runsEarned / totalInnings; } } public int totalStrikeouts() { return 0; } public double totalSalary() { double total = 0; for (int i = 0; i < team.size(); i++) { total += team.get(i).getSalary(); } return total; }
The total salary works awesome but for some reason when I try and call up how many home runs a player has it wont call correctly I get this red line under my code that says: The method getHomeRuns() is undefined for type Player. I am trying to call this from the batter class which is an extension of the Player class... anyway I hope I made sense and thanks in advnace for help