Record Highest Inheritance Java Program | ISC Computer Science 2019 Theory

A super class Record contains names and marks of the students in two different single dimensional arrays. Define a subclass Highest to display the names of the students obtaining the highest mark.

The details of the members of both the classes are given below:

Class name: Record
Data members/instance variables:
n[]: array to store names
m[]: array to store marks
size: to store the number of students
Member functions/methods:
Record(int cap): parameterized constructor to initialize the data member size = cap
void readarray(): to enter elements in both the arrays
void display(): displays the array elements

Class name: Highest
Data member/instance variable:
ind: to store the index
Member functions/methods:
Highest(…): parameterized constructor to initialize the data members of both the classes
void find(): finds the index of the student obtaining the highest mark and assign it to ‘ind’
void display(): displays the array elements along with the names and marks of the students who have obtained the highest mark

Assume that the super class Record has been defined. Using the concept of inheritance, specify the class Highest giving details of the constructor(…), void find() and void display().

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

import java.util.Scanner;
class Record{
    String n[];
    int m[];
    int size;
    public Record(int cap){
        size = cap;
        n = new String[size];
        m = new int[size];
    }
    public void readarray(){
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < size; i++){
            System.out.print("Student name: ");
            n[i] = in.nextLine();
            System.out.print("Marks scored: ");
            m[i] = Integer.parseInt(in.nextLine());
        }
    }
    public void display(){
        for(int i = 0; i < size; i++)
            System.out.println(n[i] + " score = " + m[i]);
    }
}
class Highest extends Record{
    int ind;
    public Highest(int cap){
        super(cap);
        ind = 0;
    }
    public void find(){
        for(int i = 1; i < size; i++){
            if(m[i] > m[ind])
                ind = i;
        }
    }
    public void display(){
        super.display();
        System.out.println("Students with highest mark:");
        for(int i = 0; i < size; i++){
            if(m[i] == m[ind])
                System.out.println(n[i] + " = " + m[i]);
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("How many students? ");
        int s = Integer.parseInt(in.nextLine());
        Highest obj = new Highest(s);
        obj.readarray();
        obj.find();
        obj.display();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *