Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS - 2019 Question 10

Monday, January 18, 2021

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS - 2019 Question 10

 Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class).

 The class from which the subclass is derived is called a superclass (also a base class or a parent class).Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. 

 In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.A subclass inherits all the members (fields, methods, and nested classes) from its superclass. 

Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS - 2019 Question 10 - SECTION C

A super class Record contains names and marks of the students in two different single

dimensional arrays. Define a sub class 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 member/instance variable:

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:

index                                                               : 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 ‘index

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 the details of the constructor(…),void find( ) and

void display( ).


import java.util.*;
public class ISC2019Q10 {
       public static void main(String[] args) {
       int cap;
       Scanner input = new Scanner(System.in);
       System.out.println("Enter the strength of students");
       cap = input.nextInt();
       Highest obj = new Highest(cap);
       obj.readArray();
       obj.findHighestMarks();
       obj.display();
       input.close();
       }
}
class Highest extends Record {
    public void display() {
        super.display();
        System.out.println("Highest marks is obatained by "+names[index]+" with "+marks[index]+" as total marks.");
    }
    public void findHighestMarks() {
        // for simplicity I have assumed that there is not tie for the highest marks
        int highest = 0;
        forint i =0; i < size; i++) {
            if( highest < marks[i]) {
                index = i;
                highest = marks[i];
                }
        }
    }
    Highest(int cap){
        super(cap);
        this.index = 0;
        }
    private int index;
    }
class Record {
    protected void display() {
        System.out.println("Entered details are as follows:");
        for(int i = 0; i < size; i++) {
            System.out.println(names[i]+" has obtained "+marks[i]+" marks.");
        }
    }
    public void readArray() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the details of the students:");
        for(int i=0;i<size;i++) {
            if((i+1)%10 == 1) {
                System.out.println("Enter the name of the "+(i+1)+"st student:");
                names[i] = input.next();
                System.out.println("Enter the marks of the "+(i+1)+"st student:");
                marks[i] = input.nextInt();
            }
            else if((i+1)%10==2) {
                System.out.println("Enter the name of the "+(i+1)+"nd student:");
                names[i] = input.next();
                System.out.println("Enter the marks of the "+(i+1)+"nd student:");
                marks[i] = input.nextInt();
            }
            else if((i+1)%10==3) {
                System.out.println("Enter the name of the "+(i+1)+"rd student:");
                names[i] = input.next();
                System.out.println("Enter the marks of the "+(i+1)+"rd student:");
                marks[i] = input.nextInt();
            }
            else {
                System.out.println("Enter the name of the "+(i+1)+"th student:");
                names[i] = input.next();
                System.out.println("Enter the marks of the "+(i+1)+"th student:");
                marks[i] = input.nextInt();
            }
        } 
        input.close();
    }
    Record (int cap) {
        this.size = cap;
        names = new String[size];
        marks = new int[size];
        }
    protected String[] names;
    protected int[] marks;
    protected int size;
}

No comments:

Post a Comment