Record Inheritance ISC Computer Science 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 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 the constructor, void highest() and void display().

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

import java.util.Scanner;
class Record{
    protected String name[];
    protected int rnk[];
    public Record(){
        name = new String[50];
        rnk = new int[50];
    }
    public void readValues(){
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < name.length; i++){
            System.out.print("Name " + (i + 1) + ": ");
            name[i] = in.nextLine();
            System.out.print("Rank: ");
            rnk[i] = Integer.parseInt(in.nextLine());
        }
    }
    public void display(){
        for(int i = 0; i < name.length; i++)
            System.out.println(name[i] + " - " + rnk[i]);
    }
}
class Rank extends Record{
    int index;
    public Rank(){
        super();
        index = 0;
    }
    public void highest(){
        for(int i = 1; i < name.length; i++){
            if(rnk[index] > rnk[i])
                index = i;
        }
    }
    public void display(){
        super.display();
        System.out.println("Topmost rank: " + name[index]);
    }
}
class Student{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        Rank obj = new Rank();
        obj.readValues();
        obj.highest();
        obj.display();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory