Results 1 to 11 of 11
  1. #1
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default Extending Classes and What is Necessary

    Good evening, everyone. Here's my first post with an interesting predicament.
    I'm taking a Comp Sci 1337 course at my University and have been given an assignment in which I must use one class, use another that extends the previous class, then use an input class. I must test the input and be able to organize some data alphabetically.
    The first class I must build is a 'Players' class, in which I have to, in essense, accept a name and a score, along with accessor methods. Not too hard, and I've gotten that taken care of.
    The second class, that must extend 'Players' is 'Team'. This class must accept a team name and must accept five Players. That is one of the two problems I am having. I am curious whether I should simply use a 'super' and just repeat, or if I should set up five Player methods that refer to the 'Player' class.
    Attatched will be the two classes of Players and Team.
    The second problem I am having is in the use of the seperate class of 'Input'. It saved the input as a String array and has a method of 'getNextString' that returns each piece separately. Any assistance would be very greatly appreciated.

    Player:
    Java Code:
    package assignment3;
    
    public class Player {
        private String PlayerName;
        private String Score;
        public Player()
        {
            this("Blah");
        }
        public Player(String PlayerName)
        {
            this.PlayerName = PlayerName;
        }
        public String getPlayerName()
        {
            return PlayerName;
        }
        public void setScore(String Score)
        {
            this.Score = Score+", ";
        }
        public String getScore()
        {
            return Score;
        }
    }

    Team (I am quite positive this needs to be changed):
    Java Code:
    package assignment3;
    
    public class Team extends Player{
        private String TeamName;
        private String FirstPlayerName;
        private String SecondPlayerName;
        private String ThirdPlayerName;
        private String FourthPlayerName;
        private String FifthPlayerName;
        public Team()
        {
            this("Packers");
        }
        public Team(String TeamName)
        {
            this.TeamName = TeamName;
        }
        public void setFirstPlayerName(String FirstPlayerName)
        {
            this.FirstPlayerName = FirstPlayerName;
        }
        public String getFirstPlayerName()
        {
            return FirstPlayerName;
        }
        public void setSecondPlayerName(String SecondPlayerName)
        {
            this.SecondPlayerName = SecondPlayerName;
        }
        public String getSecondPlayerName()
        {
            return SecondPlayerName;
        }
        public void setThirdPlayerName(String ThirdPlayerName)
        {
            this.ThirdPlayerName = ThirdPlayerName;
        }
        public String getThirdPlayerName()
        {
            return ThirdPlayerName;
        }
        public void setFourthPlayerName(String FourthPlayerName)
        {
            this.FourthPlayerName = FourthPlayerName;
        }
        public String getFourthPlayerName()
        {
            return FourthPlayerName;
        }
        public void setFifthPlayerName(String FifthPlayerName)
        {
            this.FifthPlayerName = FifthPlayerName;
        }
        public String getFifthPlayerName()
        {
            return FifthPlayerName;
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by GavinCash View Post
    Good evening, everyone. Here's my first post with an interesting predicament.
    Hello and welcome to java-forums.org!

    ...I must use one class, use another that extends the previous class, then use an input class. I must test the input and be able to organize some data alphabetically.
    The first class I must build is a 'Players' class, in which I have to, in essense, accept a name and a score, along with accessor methods. Not too hard, and I've gotten that taken care of.
    Good.

    The second class, that must extend 'Players' is 'Team'.
    OK, this I'm having a tough time accepting. Are you sure that you must make a class called Team and it must extend Player? Because a Team can have players in it, but it isn't in its most basic nature a player itself. It would be similar to creating a class Zoo and having it extend Animal -- a zoo is not an animal. More logical in the latter example would be to have specific animals extend Animal, such as Giraffe extends Animal, Zebra extends Animal, and Lion extend Animal, since they all ARE animals, but have Zoo not extend Animal but rather hold a collection of Animals.

    Likewise perhaps you should create a subclass of Player such as Goalie, Halfback, Fullback, and have these guys extend Player. Then your Team can hold Player objects or a collection of Players such as an ArrayList or an array.

  3. #3
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks very much for the reply and the welcome, Fubarable!

    That's the problem I'm having with the conceptual bit. I've coded a few things before, and base almost all of my programs on algorithms and what have you. But this one... It makes no sense to have a team extend players in my eyes, as Team could be a class all by itself and contain the various player items as needed, but good ol' professor has given us very specific instructions... Well, not quite specific...

    The Team class consists of at least 6 elements -- the name of the team and five players from the Player class. Include in the class appropriate accessor and mutator methods for each element in the class. You may have other attributes if needed.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    On review of this:
    The Team class consists of at least 6 elements -- the name of the team and five players from the Player class. Include in the class appropriate accessor and mutator methods for each element in the class. You may have other attributes if needed.
    I don't see mention anywhere that Team must extend Player. I still stand by what I stated above.

  5. #5
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    *cough, cough* Excuse me. I apparently am unable to read. And will gladly assume the most improbable and strange circumstances. And I can't blame it on lack of glasses... maybe they need to be cleaned. Forgive me.

    That... makes a lot of things easier! Now, the only problem I think I'll be running into is this: accepting input from one class into another. Again, there's a method that returns each string separately and that helps greatly, but I've no clue how to access that in a testing class.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by GavinCash View Post
    Now, the only problem I think I'll be running into is this: accepting input from one class into another. Again, there's a method that returns each string separately and that helps greatly, but I've no clue how to access that in a testing class.
    Going by the instructions:
    Include in the class appropriate accessor and mutator methods for each element in the class.
    I'm assuming that you'll have getter and setter methods, but not for Strings, rather for the Players oh and one String, the Team name.

  7. #7
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    In this case, our Input class was already given and pre-made. I'll go ahead and post what it is now:
    Java Code:
    package assignment3;
    /** Input.java */
    public class Input3 
    {
    	private String[] input = new String[40];
    	private static int StringNum = -1;
    	public static final int NUMBER_OF_TEAMS = 3;
    	public static final int NUMBER_OF_PLAYERS = 5;
    	
    	public Input3() 
    	{
    		//The first six inputs will be for the first team
    		input[0] = "LAKERS";
    		input[1] = "Kobe Bryant";
    		input[2] = "Derek Fisher";
    		input[3] = "Shaquille O'Neal";	
    		input[4] = "Karl Malone";
    		input[5] = "Brian Cook";
    
    		//The next six inputs will be for the second team
    		input[6] = "MAVERICKS";
    		input[7] = "Antoine Walker";
    		input[8] = "Dirk Nowitzki";
    		input[9] = "Tony Delk";
    		input[10] = "Shawn Bradley";
    		input[11] = "Travis Best";
    
    		//The next six inputs will be for the third team
    		input[12] = "KNICKS";
    		input[13] = "Mike Sweetney";
    		input[14] = "Allan Houston";
    		input[15] = "Howard Eisley";
    		input[16] = "Kurt Thomas";
    		input[17] = "Shanon Anderson";
    
    		
    	}
    
    	//This method returns the strings one after the other.
    
    	public String getNextString()
    	{
    		StringNum++;
    		return input[StringNum];
    		
    	}
    
    	
    }
    Now, it's not meant to be set, as it's already containing data. Would I just call up a Scanner and use that class as the Scanner, or could I just use the getNextString method somehow? That's the part I'm having just a tad bit o' trouble with.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your Test class would create Input3.NUMBER_OF_TEAMS teams, perhaps in an array, then create an Input3 object and call getNextString() on this object to get a team name and the 5 players for all teams.

  9. #9
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Thank you so much, Fubarable! I'm glad to be a part of this board for sure, now. I'll have to start lending my knowledge of the somewhat simpler stuff out when I can. And, one day, when I can actually program properly (and read just as well...) I'll help out as you've helped me! Many thanks.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    Comp Sci 1337 course, huh? Is that really the number?

  11. #11
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Comp Sci 1337 course, huh? Is that really the number?
    Yep, that's the class number. I was amazed when I saw it myself. Nothing like a 1337 class for a 1337 subject.

Similar Threads

  1. Got confused with extending classes.
    By nethz13 in forum New To Java
    Replies: 7
    Last Post: 04-19-2010, 12:19 AM
  2. OOP Question re. private variables and extending classes
    By ImplicitCharm in forum New To Java
    Replies: 7
    Last Post: 07-28-2009, 03:46 PM
  3. [SOLVED] Problem with extending classes...
    By Bizmark in forum New To Java
    Replies: 4
    Last Post: 04-07-2008, 11:21 PM
  4. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:49 AM
  5. EXTENDING the string class
    By ferno in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 09:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •