Text Share Online

2.Java: Sport Inheritance

Question

Design two classes, Cricket and Football, that implement the Sport interface:

  1. Cricket class:
    • Include variable:
      • int[] playerIDs: Array where the 1-based index represents the player ID
    • Include methods:
      • Cricket(): Constructor that initializes the playerIDs array with 11 elements set to 1 and prints “A new cricket team has been formed”
      • void calculateAvgAge(int[] age): Calculates and prints average age of the team to two decimal places as “The average age of the team is {avgAge}”
      • void retirePlayer(int id): Sets playerIDs[id] to -1 and prints “Player with id: {id} has retired”, or prints “Player has already retired” if already retired
  2. Football class:
    • Include variable:
      • int[] playerIDs: Array where the 1-based index represents the player ID
    • Include methods:
      • Football(): Constructor that initializes playerIDs array with 11 elements set to 1 and prints “A new football team has been formed”
      • void calculateAvgAge(int[] age): Calculates and prints average age of team to two decimal places as “The average age of the team is {avgAge}”
      • void retirePlayer(int id): Sets playerIDs[id] to -1 and prints “Player with id: {id} has retired”, or prints “Player has already retired” if already retired
      • void playerTransfer(int fee, int id): If player is active, sets playerIDs[id] to 1 and prints “Player with id: {id} has been transferred with a fee of {fee}”, or prints “Player has already retired” if already retired

Use inheritance and encapsulation to reduce code duplication. The locked code stub provides the interface Sport and validates the implementation of the Cricket and Football classes.

 

Constraints

  • 20 ≤ age ≤ 40
Share This: