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

ISC Practicals and Theory - Record to Rank

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 note solves it by inheritance: a base class Record stores the names and marks, and the derived class Rank sorts them and assigns the ranks. This is the classic form with default constructors and accept() and display() methods, where Rank.accept() calls super.accept(). A second version with parameterised constructors and method overriding is under the OOP 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$.

Inheritance lets a derived class acquire the members of a base class using extends; super() calls the base class constructor and super.method() calls the base class method. Here Rank extends Record, so a Rank object has n, name and marks from Record and its own rank array.

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) creates a Rank object r.
  2. r.accept() first calls super.accept(), which reads n and returns false with a message if n is not from 1 to 20. Otherwise it creates name[n] and marks[n] and reads each name and its marks, returning false with a message if any marks are outside 0 to 100. Then Rank.accept() creates the array rank[n]. If false is returned, main stops.
  3. r.sort() sorts by 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 are swapped, and the two names are swapped along with them using a temporary variable;
    • because the swap happens only when the marks are strictly less, equal marks are never swapped.
  4. r.assignRank() goes through the sorted list: if i > 0 and marks[i] == marks[i - 1] then rank[i] = rank[i - 1], otherwise rank[i] = i + 1.
  5. r.display() prints the heading and then, for every student, the rank, followed by the name and the marks printed by show(i) of Record.

Program

Record reads and shows the data, Rank sorts and ranks, and Test holds main. The classes share one static Scanner declared in Record.

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)
    static Scanner sc = new Scanner(System.in);   // one Scanner shared by both classes

    /* Default constructor: sets the number of students to 0. */
    Record()
    {
        n = 0;
    }

    /* Reads n, the names and the marks. Prints a message and returns false if n is not
       from 1 to 20 or any marks are not from 0 to 100; returns true if all are valid. */
    boolean accept()
    {
        System.out.print("Enter the number of students (1 to 20): ");
        n = sc.nextInt();
        if (n < 1 || n > 20)
        {
            System.out.println("Invalid number of students. It must be from 1 to 20.");
            return false;
        }
        name = new String[n];
        marks = new int[n];
        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;
    }

    /* 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

    /* Default constructor: calls the base class constructor. */
    Rank()
    {
        super();
    }

    /* Reads the records with super.accept() and then creates the rank array.
       Returns false if the base class found the input invalid. */
    boolean accept()
    {
        if (!super.accept())
            return false;
        rank = new int[n];
        return true;
    }

    /* 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
        }
    }

    /* 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
{
    /* Creates a Rank object, reads the records, ranks the students and prints the rank list. */
    public static void main(String args[])
    {
        Rank r = new Rank();             // object of the derived class
        if (r.accept())
        {
            r.sort();
            r.assignRank();
            r.display();
        }
    }
}

Variable description table

VariableData TypePurpose
nintData member of Record: number of students (1 to 20)
nameString[]Data member of Record: names of the students
marksint[]Data member of Record: marks of the students
scScannerStatic data member of Record: 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
rRankObject of the derived class Rank created in main
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$). The state of the list after each pass of sort() 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

display() 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."