SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Record and Rank Inheritance Program in Java and Python

19 September 2013

ISC 2011 inheritance solution using superclass Record and subclass Rank with algorithm, explanation, Java program and simple Python program.

Question:

A superclass Record has been defined to store the names and ranks of 50 students. Define a subclass Rank to find the highest rank along with the name. The details of both classes are given below:

Class name: Record Data members / instance variables: name[] : to store the names of students rnk[] : to store the ranks of students Member functions: Record() : constructor to initialize data members void readvalues() : to store names and ranks void display() : displays the names and the corresponding ranks Class name: Rank Data members / instance variables: index : integer to store the index of the topmost rank Member functions: Rank() : constructor to invoke the base class constructor and initialize index to 0 void highest() : finds the index location of the topmost rank and stores it in index without sorting the array void display() : displays the names and ranks along with the name having the topmost rank

Specify the class Record giving details of the constructor, void readvalues() and void display(). Using the concept of inheritance, specify the class Rank giving details of the constructor, void highest() and void display().

The main function and algorithm need not be written in the original theory answer, but a main method is included below to show how the classes can be executed.

Algorithm:

Step 1: Start.

Step 2: Define a superclass named Record.

Step 3: Declare name[] to store student names and rnk[] to store ranks.

Step 4: In the Record constructor, create both arrays of size 50.

Step 5: In readvalues(), use a loop to accept the name and rank of every student.

Step 6: In display() of Record, print every name with its corresponding rank.

Step 7: Define a subclass named Rank that extends Record.

Step 8: In the Rank constructor, call the superclass constructor using super() and initialize index to 0.

Step 9: In highest(), assume the first rank is the topmost rank and store it as min.

Step 10: Traverse all ranks. Whenever a smaller rank is found, update min and store that position in index.

Step 11: In display() of Rank, call super.display() to show all records.

Step 12: Call highest() and then display the topmost rank and the corresponding student name.

Step 13: Stop.

Explanation:

This program demonstrates inheritance through two related classes. The superclass Record stores the common data: names of students and their ranks. Since this information is needed by the subclass also, it is placed in the superclass. The subclass Rank then reuses these inherited arrays and adds one extra data member, index, which stores the position of the student with the topmost rank.

The constructor of Record creates two arrays of size 50. One array stores names and the other stores ranks. The method readvalues() fills these arrays by accepting the name and rank of each student. The method display() prints the stored records. These actions belong naturally to Record because they deal with general student record storage, not with finding the highest rank.

The subclass Rank extends Record, so it automatically gets access to the arrays name and rnk. Its constructor calls super(), which invokes the constructor of Record and initializes the inherited arrays. Then it initializes index to 0. This means the program first assumes that the first student has the topmost rank until a better rank is found.

In rank-based lists, the topmost rank is represented by the smallest number. Rank 1 is better than rank 2, rank 2 is better than rank 3, and so on. Therefore, the highest() method searches for the minimum value in the rank array. It does not sort the array because sorting is not required. It simply scans all ranks once. Whenever a smaller rank is found, both min and index are updated. Finally, the subclass display() method first calls super.display() to show all records, then prints the topmost rank and the name stored at the saved index.

Java Program:

Java
/**
* The superclass Record stores the names and ranks of 50 students.
* The subclass Rank finds the topmost rank along with the student name.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2011 Question 11
*/

import java.util.Scanner;

class Record
{
    Scanner sc = new Scanner(System.in);
    String name[];
    int rnk[];

    Record()
    {
        name = new String[50];
        rnk = new int[50];
    }

    void readvalues()
    {
        System.out.println("*** Inputting The Names And Ranks ***");

        for(int i = 0; i < 50; i++)
        {
            System.out.print("Enter name of student " + (i + 1) + ": ");
            name[i] = sc.nextLine();

            System.out.print("Enter rank: ");
            rnk[i] = sc.nextInt();
            sc.nextLine(); // Clear the newline left after reading the rank.
        }
    }

    void display()
    {
        System.out.println("Name\t\tRank");
        System.out.println("-------\t\t-------");

        for(int i = 0; i < 50; i++)
        {
            System.out.println(name[i] + "\t\t" + rnk[i]);
        }
    }
}

class Rank extends Record
{
    int index;

    Rank()
    {
        super();
        index = 0;
    }

    void highest()
    {
        int min = rnk[0];

        /*
        * The topmost rank is the smallest rank value.
        * The array is scanned once without sorting.
        */
        for(int i = 0; i < 50; i++)
        {
            if(rnk[i] < min)
            {
                min = rnk[i];
                index = i;
            }
        }
    }

    void display()
    {
        super.display();
        highest();

        System.out.println("\nTopmost rank = " + rnk[index]);
        System.out.println("Student with topmost rank = " + name[index]);
    }
}

public class Question11_ISC2011
{
    public static void main(String args[])
    {
        Rank ob = new Rank();
        ob.readvalues();

        System.out.println("*** Output ***");
        ob.display();
    }
}

Equivalent Python Program:

Python
class Record:
    def __init__(self):
        # Lists are created for 50 students.
        self.name = [""] * 50
        self.rnk = [0] * 50

    def readvalues(self):
        print("*** Inputting The Names And Ranks ***")

        for i in range(0, 50):
            self.name[i] = input("Enter name of student " + str(i + 1) + ": ")
            self.rnk[i] = int(input("Enter rank: "))

    def display(self):
        print("Name\t\tRank")
        print("-------\t\t-------")

        for i in range(0, 50):
            print(self.name[i], "\t\t", self.rnk[i])


class Rank(Record):
    def __init__(self):
        super().__init__()
        self.index = 0

    def highest(self):
        minimum = self.rnk[0]

        # The smallest rank value is the topmost rank.
        for i in range(0, 50):
            if self.rnk[i] < minimum:
                minimum = self.rnk[i]
                self.index = i

    def display(self):
        super().display()
        self.highest()

        print()
        print("Topmost rank =", self.rnk[self.index])
        print("Student with topmost rank =", self.name[self.index])


ob = Rank()
ob.readvalues()

print("*** Output ***")
ob.display()

Output:

Sample Output shown for 5 students: *** Inputting The Names And Ranks *** Enter name of student 1: Aamir Enter rank: 5 Enter name of student 2: Zakir Enter rank: 2 Enter name of student 3: Saalim Enter rank: 7 Enter name of student 4: Samir Enter rank: 3 Enter name of student 5: Saif Enter rank: 6 *** Output *** Name Rank ------- ------- Aamir 5 Zakir 2 Saalim 7 Samir 3 Saif 6 Topmost rank = 2 Student with topmost rank = Zakir

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →