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
nmust 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
main(in the driver classTest) creates aRankobjectr.r.accept()first callssuper.accept(), which readsnand returnsfalsewith a message ifnis not from 1 to 20. Otherwise it createsname[n]andmarks[n]and reads each name and its marks, returningfalsewith a message if any marks are outside 0 to 100. ThenRank.accept()creates the arrayrank[n]. Iffalseis returned,mainstops.r.sort()sorts by bubble sort in descending order:- for
ifrom 0 ton - 2andjfrom 0 ton - 2 - i, ifmarks[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.
- for
r.assignRank()goes through the sorted list: ifi > 0andmarks[i] == marks[i - 1]thenrank[i] = rank[i - 1], otherwiserank[i] = i + 1.r.display()prints the heading and then, for every student, the rank, followed by the name and the marks printed byshow(i)ofRecord.
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
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member of Record: number of students (1 to 20) |
name | String[] | Data member of Record: names of the students |
marks | int[] | Data member of Record: marks of the students |
sc | Scanner | Static data member of Record: reads everything typed by the user |
i | int | Loop counter for the student; also the parameter of show(), the index of the student to print |
rank | int[] | Data member of Rank: rank of the student at the same position |
j | int | Inner loop counter of the bubble sort |
t | int | Temporary variable for swapping two marks |
s | String | Temporary variable for swapping two names |
r | Rank | Object of the derived class Rank created in main |
args | String[] | 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 65Sample 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 0Sample 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 50Sample 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.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 |
|---|---|---|
| 0 | j = 0 (78 < 92), j = 3 (65 < 85) | Asha 92, Ravi 78, Meena 78, Zoya 85, Karan 65 |
| 1 | j = 2 (78 < 85) | Asha 92, Ravi 78, Zoya 85, Meena 78, Karan 65 |
| 2 | j = 1 (78 < 85) | Asha 92, Zoya 85, Ravi 78, Meena 78, Karan 65 |
| 3 | none | Asha 92, Zoya 85, Ravi 78, Meena 78, Karan 65 |
Then assignRank() works on the sorted list:
| i | marks[i] | marks[i - 1] | Equal? | rank[i] |
|---|---|---|---|---|
| 0 | 92 | - | - | 1 |
| 1 | 85 | 92 | no | 2 |
| 2 | 78 | 85 | no | 3 |
| 3 | 78 | 78 | yes | 3 |
| 4 | 65 | 78 | no | 5 |
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
- Ascending order "Sort the students in ascending order of marks and give ranks from the lowest."
- Topper "Print the names of the students who have the highest marks."
- Selection sort "Write the sort in class
Rankusing selection sort."