August 1999
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 3

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 3

The Singapore Football League requires a program to maintain information about the football league. Each team in the league has a name (a character string), a current league position (an integer) and the number of games they have played (an integer).

 

(a) Give a declaration of the class Team which contains
  • The private data items mentioned above;
  • A constructor which takes the team name as a parameter;
  • A method which returns the number of games they have played;
  • A method which returns a pointer to a copy of the team name;
  • A method which returns the teams league position.

 

[5]
A sample definition of Team follows:

Class Team {
public:
    Team(char* team_name);
    int Return_Played( );
    int Return_position( );
    char* Return_Name( );

private:
    char* team_name;
    int position;
    int points;
    int played;
} ;

 

(b) A league is a collection of 20 teams. Give a definition of a class League, which contains an array of 20 teams. [2]
A sample definition of League follows:

class League {
public:
    Team team[20]

} ;

 

(c) Since each team in the league plays each other team, the total number of games played for all teams is an even number. Write a function, called check_fixture, which sums all of the games played by all teams in the league, and returns 0 if it is an even number, or -1 otherwise. [6]
A sample definition of check_fixture follows:

int League : :check_fixture( ) {
   int i;
   int count= 0;

   for(i= 0; i< 20; i++)
      count= count+ team[i]. Return_Played( );

   return (count%2);
}

 

(d) Write a function display_league, which takes an object of type League,prints the names of the teams in that league in descending order of position, from position 1 to 20. You should use a simple sequential search to locate the teams. [7]
A sample definition of display_league follows:

void printout_league(League division) {
int i, j;
   for(i= 1; i<= 20; i++) {
      for(j= 0; j< 20; j++) {
         if(division.team[j]. Return_Position( )== i)
           cout << division.team[j].Return_Name( );
     }
   }
}