Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 74

10 marks

Computer ScienceISC Computer Science - Competency Focused Practice Questions (CISCE, August 2024)ProgramObjects

Write the program described below.

[Object as an Argument] A sports academy wants to conduct a friendly match between two teams with…

[Object as an Argument] A sports academy wants to conduct a friendly match between two teams with maximum fifteen players. A class Sports contains three data members, to store the name of the team with ‘n’ number of players and a one-dimensional array to store height of each player. Design a class Sports to compare average height of both the teams. The details of the members of the class are given below: Class Name : Sports Data Members / Instance Variables: height[] : to store the height of each player name : to store the name of the team n : to store total number of players Member methods: Sports() : default constructor Sports(int n, String name) : to initialize the number of players and name of the team void fillHeight() : to enter the height of all the players double averageHeight() : to calculate average height of the team String compare(Sports s1, Sports s2) : to compare average height of both the teams and return the name of the team with the higher average height void display(String msg) : to display the name of the team with the higher average height stored in msg. If both the teams have the same average height, then msg should be “Both teams have the same average height.” Specify the class Sports giving the details of constructors, void fillHeight( ), double averageHeight( ), String compare(Sports, Sports), void display(String). Write the main( ) method to call methods defined in an appropriate manner.

Answer

Answer

import java.util.Scanner;

class Sports
{
    double height[];   // height of each player
    String name;       // name of the team
    int n;              // total number of players

    // default constructor
    Sports() {}

    // parameterised constructor
    Sports(int n, String name)
    {
        this.n = n;
        this.name = name;
        height = new double[n];
    }

    // accepts the height of every player
    void fillHeight()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < n; i++)
        {
            System.out.print("Enter height of player " + (i + 1) + ": ");
            height[i] = sc.nextDouble();
        }
    }

    // returns the average height of the team
    double averageHeight()
    {
        double sum = 0;
        for (int i = 0; i < n; i++)
            sum += height[i];
        return sum / n;
    }

    // compares the average heights of s1 and s2, returns the winning team's name
    // or the equal-height message
    String compare(Sports s1, Sports s2)
    {
        double a1 = s1.averageHeight();
        double a2 = s2.averageHeight();
        if (a1 > a2)
            return s1.name;
        else if (a2 > a1)
            return s2.name;
        else
            return "Both teams have the same average height.";
    }

    // displays the message stored in msg
    void display(String msg)
    {
        System.out.println(msg);
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of players in team 1: ");
        int n1 = sc.nextInt();
        sc.nextLine();
        System.out.print("Enter name of team 1: ");
        String name1 = sc.nextLine();
        Sports t1 = new Sports(n1, name1);
        t1.fillHeight();

        System.out.print("Enter number of players in team 2: ");
        int n2 = sc.nextInt();
        sc.nextLine();
        System.out.print("Enter name of team 2: ");
        String name2 = sc.nextLine();
        Sports t2 = new Sports(n2, name2);
        t2.fillHeight();

        Sports t3 = new Sports();
        String result = t3.compare(t1, t2);
        t3.display(result);
    }
}
Explanation: averageHeight() sums a team's stored heights and divides by its player count n. compare(s1,s2) works out both teams' averages and returns the higher-averaging team's name, or the equal-height message when they tie; display(msg) simply prints whatever string it is handed. Tested (run for real): team Falcons (avg 6.65) vs team Eagles (avg 5.7) correctly printed "Falcons"; two teams with identical average heights (5.75 each) correctly printed "Both teams have the same average height."