Friday 25 October 2013

Inheritance Program

/*A class Iscscores defines the scores of a candidate in six subjects and another class bestfour defines
the best four subjects.
The details of both the classes are given below:-

Class name : Iscscores
Data members
number[6][2] : int array to store marks of 6 subjects and subject code.
Member functions
Iscscores() : constructor to accept the marks
int point() : to return the point in each subject according to the
following:
Marks>=90 1 point
80-89 2 points
70-79 3 points
60-69 4 points
50-59 5 points
40-49 6 points
---------------------------------accordingly
Class name : bestfour
Member functions
void bestsubjects() : to display the total points and best four subject codes using
the concept of inheritance.
Specify the class details of both the classes using the concept of inheritance.*/


import java.io.*;
class Iscscores
{
   int number[][];
   int k;
   BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
   Iscscores()
   {
       number=new int [6][2];
       k=0;
   }
   void input()throws IOException
   {
       for(int i=0;i<6;i++)
       {
           System.out.print("Enter Subject Code: ");
           number[i][0]=Integer.parseInt(x.readLine());
           System.out.print("Enter marks of Subject: ");
           number[i][1]=Integer.parseInt(x.readLine());
        }
    }
   int point()
   {
       int n=number[k][1];
       if(n>=90)
       return 1;
       else if (n>=80)
       return 2;
       else if (n>=70)
       return 3;
       else if (n>=60)
       return 4;
       else if (n>=50)
       return 5;
       else if (n>=40)
       return 6;
       else if (n>=30)
       return 7;
       else if (n>=20)
       return 8;
       else
       return 9;
    }
}

class BestFour extends Iscscores
{
    void bestsubject()
    {
        int l=number[0][1],s=number[0][1];
        int pt=0,code,j=0,c=0;
        //total points and lowest marks
        for(int i=0;i<6;i++)
        {
            k=i;
            System.out.println("Subject Code="+number[i][0]+"\tMarks="+number[i][1]+"\tPoints="+point());
            pt=pt+point();
            if(s>number[i][1])
            s=number[i][1];
        }
        System.out.println("Total Points="+pt);
        System.out.println("Best 4 subjects");
        do
        {
            for(int i=0;i<6;i++)
            {
                if(l<number[i][1])
                {
                    l=number[i][1];
                    code=number[i][0];
                    j=i;
                }
            }
            System.out.println("Code="+number[j][0]);
            l=s-1;number[j][1]=s-1;
            ++c;
        }
        while(c!=4);
    }
}

No comments:

Post a Comment