Record Rank Inheritance Program ISC 2011 Theory

A super class Record has been defined to store the names and ranks of 50 students. Define a sub-class Rank to find the highest rank along with the name.

The details of both the 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 the 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 to initialize index = 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 constructor, void highest() and void display().

The main() function and algorithm need not be written.

import java.io.*;
class Record{
    protected String name[];
    protected int rnk[];
    public Record(){
        name = new String[5];
        rnk = new int[5];
    }
    public void readValues()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the names and ranks for each student:");
        for(int i = 0; i < name.length; i++){
            System.out.print("Name: ");
            name[i] = br.readLine();
            System.out.print("Rank: ");
            rnk[i] = Integer.parseInt(br.readLine());
        }
    }
    public void display(){
        for(int i = 0; i < name.length; i++)
            System.out.println("Name: " + name[i] + ", Rank: " + rnk[i]);
    }
}
class Rank extends Record{
    private int index;
    public Rank(){
        super();
        index = 0;
    }
    public void highest(){
        for(int i = 1; i < name.length; i++){
            if(rnk[i] < rnk[index])
                index = i;
        }
    }
    public void display(){
        super.display();
        System.out.println("Name having the topmost rank: " + name[index]);
    }
}
class Inheritance{
    public static void main(String[] args)throws IOException{
        Rank obj = new Rank();
        obj.readValues();
        obj.highest();
        obj.display();
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.