Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: subclass and abstracts

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default subclass and abstracts

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,530
    My Mood
    Mellow
    Thanks
    157
    Thanked 615 Times in 602 Posts

    Default Re: subclass and abstracts

    Aren't you showing an error on this line?:

    team = new ArrayList<>();

    Edit: Then find the method getHomeRuns() and determine if it really is a Player method as you think it is.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: subclass and abstracts

    maybe should I have something in there? I did have it like this:

    team = new ArrayList<Player>();

    but netbeans said it was redundant to have the Player in the <>

    --- Update ---

    figured out on my own.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,530
    My Mood
    Mellow
    Thanks
    157
    Thanked 615 Times in 602 Posts

    Default Re: subclass and abstracts

    Well, not exactly, but good job solving the problem.

Similar Threads

  1. Java abstracts
    By maple1100 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 26th, 2013, 03:38 PM
  2. How to end a subclass?
    By Purple01 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 21st, 2012, 01:26 AM
  3. [SOLVED] how to extend a subclass?!
    By migongotar in forum Object Oriented Programming
    Replies: 4
    Last Post: August 2nd, 2011, 03:22 PM
  4. subclass that implements
    By speedycerv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2011, 07:35 AM