0

Task 1. Design and implement classes SportsClub (abstract class), FootballClub. Classes should include appropriate methods and hold information about name of the club, its location and various statistics about the club. FootballClub should include statistics such as how many wins, draws and defeats an instance of it has achieved in the season, the number of goals received and scored. The number of points that a club currently has, and number of matches played.

Task 2. Implement a class PremierLeagueManager which extends interface LeagueManager. PremierLeagueManager class maintains a number of football clubs which play in the premier league. The class should create a menu based on text input and give the user the choice of: •

  1. Create a new football club and add it in the premier league. •

  2. Delete (relegate) an existing football club from the premier league

  3. Display the various statistics for a selected club.
  4. Display the Premier League Table

I designed abstract class

public abstract class SportsClub {
    int position;
    String name;
    int points;
    int wins;
    int defeats;
    int draws;
    int totalMatches;
    int goalF;
    int goalA;
    int goalD;
    String location;
}

I extended the abstract class I know I can use the get/set method but I used contructor to initialise (correct me if I'm worng)

public class FootballClub extends SportsClub {

     FootballClub(int position, String name, 
                  int points, int wins, 
                  int defeats, int draws, 
                  int totalMatches, int goalF, 
                  int goalA, int goalD) {
         this.position = position;
         this.name = name;
         this.points = points;
         this.wins = wins;
         this.defeats = defeats;
         this.draws = draws;
         this.totalMatches = totalMatches;
         this.goalF = goalF;
         this.goalA = goalA;
         this.goalD = goalD;     
     }
}

Now I need to write an interface which I know how to do it but I'm trying to get my head around that am I declaring the methods inside interface or shall i do it in abstract class. Basically I need a help how I can approach to this whole excersie. Dont know what class I should be using for main method. I'm a new learner and really confused if somebody help me out on that about how should I approach I'm sure I will be able to do it myself. Not asking for code need help if somebody simplify me this whole excerise please

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
Cortez Ninja
  • 97
  • 3
  • 16
  • 1) A `main` method can be in any class. It just has to be set as the entry point to the program. 2) An interface *declares* a method, but it does not implement it. Leave the implementation of the method to the subclasses. 3) Your abstract SportsClub class has too many variables - it is too specific. What if you want to make a `GolfSportsClub`? Golf doesn't have "goals"... variables that are specific to one class should go in that class, not the parent. – Mage Xy Nov 13 '15 at 17:40
  • 1
    Aside from that advice, we need a specific problem to be able to answer your question. As it is written now, your question is too broad. – Mage Xy Nov 13 '15 at 17:41
  • You have placed too many statistics in SportsClub. For instance, points, wins, defeats, draws, goals, etc might be meaningless for some other subclass of SportsClub. Note that the instructions says these should be in FootballClub. – FredK Nov 13 '15 at 17:42
  • An interface, in its simplest form, only defines method heads (i.e. methods without a body). You basically say "Someone, who wants to support my interface must implement this functionality" (for example `push(...)` and `pop()` for a stack). You do not care HOW they are implemented (that is a task for the person, who implements your interface). Maybe [this](http://stackoverflow.com/questions/30784215/what-does-decoupling-two-classes-at-the-interface-level-mean) is of some help. – Turing85 Nov 13 '15 at 17:43

1 Answers1

2

First a comment on the existing code. I would that constructor in the abstract superclass since it initializes the fields from that class.

public abstract class SportsClub {
    SportsClub(int position, String name, int points, int wins, int defeats, int draws, int totalMatches, int goalF, int goalA, int goalD){
        this.position = position
        ...

Then

public class FootballClub extends SportsClub{

   FootballClub(int position, String name, int points, int wins, int defeats, int draws, int totalMatches, int goalF, int goalA, int goalD) : base(position, name, points, wins, defeats, draws, totalMatches, goalF, goalA, goalD) {
   }

That makes it more reusable for the next subclasses. Also as a general rule, whenever I see this many parameter to a method/constructor, I create a struct/class to contain them all (say ClubInfo for example). It's less verbose that way and the API of your classes are easier to read.

Now on to your question. One basic difference between an abstract class and an interface is that the interface is 100% abstract and has no state (i.e. no fields). So since your SportsClub has all these fields, it holds a state, and therefore needs to be a class. Since it isn't a complete implementation (presumably you'll add more methods that the actual sports club need to implement because it's tied to that particular sport), it needs to be abstract.

For example. Any sports club, regardless of the sport, can send an advertisement about a game.

So in SportsClub you add the method: public abstract string AnnounceGame();

Now, how the game is announced varies by sport:

So FootballClub might implement it this way:

public string AnnounceGame() { return "Come join us at the stadium"; }

While BaseballClub might do:

public string AnnounceGame() { return "Come join us at the ball park"; }

The basic idea is that if you're a sport promoter class, for example, you don't care what type of sports club you have, you know that any SportsClub can make announcements.

SportsClub clubA = new FootballClub();
SportsClub clubB = new BaseballClub();

If I call:

clubA.AnnounceGame(); // outputs "Come join us at the stadium"
clubB.AnnounceGame(); // outputs "Come join us at the ball park"

So we've used inheritance to reduce complexity. The promoter doesn't have to care what type of sports club instance it has.

mprivat
  • 21,582
  • 4
  • 54
  • 64