PRASHNIKAप्रश्निका
Back to the paper

A super class Record contains names and marks of the students in two different single dimensional…

Computer Science20195 marksProgram
A super class Record contains names and marks of the students in two different single dimensional arrays. Define a sub class Highest to display the names of the students obtaining the highest mark. The details of the members of both the classes are given below: Class name : Record Data member/instance variable: n[ ] : array to store names m[ ] : array to store marks size : to store the number of students Member functions/methods: Record(int cap) : parameterized constructor to initialize the data member size = cap void readarray() : to enter elements in both the arrays void display( ) : displays the array elements Class name: Highest Data member/instance variable: ind : to store the index Member functions/methods: Highest(…) : parameterized constructor to initialize the data members of both the classes void find( ) : finds the index of the student obtaining the highest mark and assign it to ‘ind’ void display( ) : displays the array elements along with the names and marks of the students who have obtained the highest mark Assume that the super class Record has been defined. Using the concept of inheritance, specify the class Highest giving the details of the constructor(…),void find( ) and void display( ). The super class, main function and algorithm need NOT be written.

Answer

Answer

AI
class Highest extends Record
{
    int ind;

    Highest(int cap)
    {
        super(cap);
        ind = 0;
    }

    void find()
    {
        ind = 0;
        for (int i = 1; i < size; i++)
        {
            if (m[i] > m[ind])
                ind = i;
        }
    }

    void display()
    {
        super.display();
        find();
        System.out.println("Student(s) with the highest mark:");
        for (int i = 0; i < size; i++)
        {
            if (m[i] == m[ind])
                System.out.println(n[i] + "\t" + m[i]);
        }
    }
}
Explanation: the parameterised constructor passes cap up to the superclass via super(cap) so that Record's constructor allocates the n[] and m[] arrays of the given size, then initialises ind. find() scans the inherited marks array m[] (indices 0 to size-1) and stores in ind the index of the largest mark. display() first calls super.display() to reuse the superclass's printing of all the array elements, then calls find() and prints the name(s) and mark of the student(s) with the highest mark (handling ties, i.e. more than one student sharing the highest mark). Tested (run for real, with a small Record superclass and main added only for testing) with names/marks {AMIT:78, SUMIT:95, ROHIT:95, MOHIT:60}: the program correctly identified and printed both SUMIT and ROHIT (95) as the students with the highest mark.
Inheritance, Interfaces and Polymorphism

From ISC 2019 Computer Science Paper 1, question 10.