Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Record to Rank (OOP)

Updated 21 Oct 2026

A class teacher has the names and marks of some students and wants a rank list, with the highest marks at rank 1. This version uses a parameterised constructor with super(size), and the derived class Rank overrides process() and display(). In main, a reference of the base class type Record points to a Rank object, so the calls r.process() and r.display() run the Rank versions (dynamic binding). The classic version with default constructors is under the Classic button.

Definition

A rank list arranges students in descending order of marks and numbers them. Students with equal marks get the same rank and the next rank is skipped. For example, marks $90, 85, 85, 70$ get the ranks $1, 2, 2, 4$.

Method overriding means the derived class redefines a base class method with the same name and parameters. Polymorphism (dynamic binding) means that when a base class reference holds a derived class object, a call to an overridden method runs the derived class version, decided at run time. super(size) calls the base class constructor.

The rules used in this program are stated below.

  • The number of students n must be from 1 to 20, otherwise the program prints a message and stops.
  • Marks must be from 0 to 100.
  • Names are single words, as they are read with next().
  • Tie rule: equal marks get the same rank and the next rank skips (1, 2, 2, 4). Students with equal marks stay in the order in which they were entered.

Algorithm

  1. main (in the driver class Test) reads n with a Scanner. If n is not from 1 to 20 it prints the invalid number message and stops.
  2. main creates Record r = new Rank(n). The constructor of Rank calls super(size), which sets n and creates name[n] and marks[n]; then Rank creates rank[n].
  3. r.accept(sc) reads each name and its marks and returns false with a message if any marks are outside 0 to 100. If false is returned, main stops.
  4. r.process() runs the overridden method of Rank (the one in Record does nothing). It calls:
    • sort(): bubble sort in descending order. For i from 0 to n - 2 and j from 0 to n - 2 - i, if marks[j] < marks[j + 1] the two marks and the two names are swapped using temporary variables. Equal marks are never swapped;
    • assignRank(): if i > 0 and marks[i] == marks[i - 1] then rank[i] = rank[i - 1], otherwise rank[i] = i + 1.
  5. r.display() runs Rank's overridden display(), which prints the heading and, for every student, the rank followed by the name and marks printed by show(i) of Record.

Program

Record holds the data and the base versions of process() and display(); Rank adds the rank array, the sort and the ranking and overrides both methods; Test holds main. The Scanner is created in main and passed to accept().

import java.util.Scanner;

/* Class Record: the base class. It holds the names and marks of n students. */
class Record
{
    int n;                               // number of students (1 to 20)
    String name[];                       // names of the students (one word each)
    int marks[];                         // marks of the students (0 to 100)

    /* Parameterised constructor: sets n and creates the arrays.
       Parameter size: the number of students, already checked to be from 1 to 20. */
    Record(int size)
    {
        n = size;
        name = new String[n];
        marks = new int[n];
    }

    /* Reads the names and the marks. Prints a message and returns false if any marks are
       not from 0 to 100; returns true if all are valid.
       Parameter sc: the Scanner created in main. */
    boolean accept(Scanner sc)
    {
        for (int i = 0; i < n; i++)
        {
            System.out.print("Enter the name of student " + (i + 1) + ": ");
            name[i] = sc.next();
            System.out.print("Enter the marks of " + name[i] + ": ");
            marks[i] = sc.nextInt();
            if (marks[i] < 0 || marks[i] > 100)
            {
                System.out.println("Invalid marks. Marks must be from 0 to 100.");
                return false;
            }
        }
        return true;
    }

    /* Does the processing of the records. The base class has nothing to do;
       Rank overrides this method. */
    void process()
    {
    }

    /* Prints the name and the marks of the student at position i on one line.
       Parameter i: the index of the student. */
    void show(int i)
    {
        System.out.println(name[i] + "\t" + marks[i]);
    }

    /* Prints the names and marks of all the students in the present order. */
    void display()
    {
        System.out.println("Name\tMarks");
        for (int i = 0; i < n; i++)
            show(i);
    }
}

/* Class Rank: derived class. It sorts the students by marks and gives each a rank. */
class Rank extends Record
{
    int rank[];                          // rank of the student at the same position

    /* Parameterised constructor: passes size to the base class with super(size)
       and creates the rank array. */
    Rank(int size)
    {
        super(size);
        rank = new int[size];
    }

    /* Sorts the students in descending order of marks by bubble sort.
       The names are swapped along with the marks. */
    void sort()
    {
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = 0; j < n - 1 - i; j++)
            {
                if (marks[j] < marks[j + 1])
                {
                    int t = marks[j];            // temporary store for swapping the marks
                    marks[j] = marks[j + 1];
                    marks[j + 1] = t;
                    String s = name[j];          // temporary store for swapping the names
                    name[j] = name[j + 1];
                    name[j + 1] = s;
                }
            }
        }
    }

    /* Gives ranks to the sorted list. Equal marks get the same rank and the next
       rank is skipped (for example 1, 2, 2, 4). */
    void assignRank()
    {
        for (int i = 0; i < n; i++)
        {
            if (i > 0 && marks[i] == marks[i - 1])
                rank[i] = rank[i - 1];           // a tie: same rank as the student above
            else
                rank[i] = i + 1;                 // otherwise the rank is the position
        }
    }

    /* Overrides process(): sorts the students and then gives the ranks. */
    void process()
    {
        sort();
        assignRank();
    }

    /* Overrides display(): prints the rank list: rank, name and marks of every student. */
    void display()
    {
        System.out.println("Rank list:");
        System.out.println("Rank\tName\tMarks");
        for (int i = 0; i < n; i++)
        {
            System.out.print(rank[i] + "\t");
            show(i);
        }
    }
}

/* Class Test: the driver class that holds main. */
public class Test
{
    /* Reads and checks n, creates a Rank object through a Record reference,
       reads the records, ranks the students and prints the rank list. */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);   // reads the typed values
        System.out.print("Enter the number of students (1 to 20): ");
        int n = sc.nextInt();                  // number of students
        if (n < 1 || n > 20)
        {
            System.out.println("Invalid number of students. It must be from 1 to 20.");
            return;
        }
        Record r = new Rank(n);                // base class reference to a Rank object
        if (r.accept(sc))
        {
            r.process();                       // calls Rank's process()
            r.display();                       // calls Rank's display()
        }
    }
}

Variable description table

VariableData TypePurpose
nintData member of Record: number of students (1 to 20); also the local in main that holds the typed number
nameString[]Data member of Record: names of the students
marksint[]Data member of Record: marks of the students
sizeintParameter of the constructors of Record and Rank: number of students
scScannerParameter of accept() and local in main: reads everything typed by the user
iintLoop counter for the student; also the parameter of show(), the index of the student to print
rankint[]Data member of Rank: rank of the student at the same position
jintInner loop counter of the bubble sort
tintTemporary variable for swapping two marks
sStringTemporary variable for swapping two names
rRecordBase class reference in main, pointing to a Rank object
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter the number of students (1 to 20): 5
Enter the name of student 1: Ravi
Enter the marks of Ravi: 78
Enter the name of student 2: Asha
Enter the marks of Asha: 92
Enter the name of student 3: Meena
Enter the marks of Meena: 78
Enter the name of student 4: Karan
Enter the marks of Karan: 65
Enter the name of student 5: Zoya
Enter the marks of Zoya: 85
Rank list:
Rank	Name	Marks
1	Asha	92
2	Zoya	85
3	Ravi	78
3	Meena	78
5	Karan	65

Sample 2

Enter the number of students (1 to 20): 1
Enter the name of student 1: Tina
Enter the marks of Tina: 0
Rank list:
Rank	Name	Marks
1	Tina	0

Sample 3

Enter the number of students (1 to 20): 3
Enter the name of student 1: A
Enter the marks of A: 50
Enter the name of student 2: B
Enter the marks of B: 50
Enter the name of student 3: C
Enter the marks of C: 50
Rank list:
Rank	Name	Marks
1	A	50
1	B	50
1	C	50

Sample 4

Enter the number of students (1 to 20): 21
Invalid number of students. It must be from 1 to 20.

Sample 5

Enter the number of students (1 to 20): 2
Enter the name of student 1: Ravi
Enter the marks of Ravi: 80
Enter the name of student 2: Asha
Enter the marks of Asha: 101
Invalid marks. Marks must be from 0 to 100.

One value per line

Dry run

Take the first sample: Ravi 78, Asha 92, Meena 78, Karan 65, Zoya 85 ($n = 5$). r.process() calls sort(), whose state after each pass is:

Pass (i)Swaps made (j)List after the pass
0j = 0 (78 < 92), j = 3 (65 < 85)Asha 92, Ravi 78, Meena 78, Zoya 85, Karan 65
1j = 2 (78 < 85)Asha 92, Ravi 78, Zoya 85, Meena 78, Karan 65
2j = 1 (78 < 85)Asha 92, Zoya 85, Ravi 78, Meena 78, Karan 65
3noneAsha 92, Zoya 85, Ravi 78, Meena 78, Karan 65

Then assignRank() works on the sorted list:

imarks[i]marks[i - 1]Equal?rank[i]
092--1
18592no2
27885no3
37878yes3
46578no5

r.display() runs Rank's method and prints these ranks with the names and marks, as in the first sample run: the two students with 78 share rank 3 and the next rank is 5.

Variations

  1. Ascending order "Sort the students in ascending order of marks and give ranks from the lowest."
  2. Topper "Print the names of the students who have the highest marks."
  3. Selection sort "Write the sort in class Rank using selection sort."