Search This Blog

Friday 15 July 2022

5. NEP Java Program to define a class with the following attributes Name of the class (BCA, BCom, BSc), Name of the staff No of the students in the class, Array of students in the class.

 //5.In a college first year class are having the following attributes Name of the class (BCA, BCom, BSc), Name of the staff No of the students in the class, Array of students in the class. Define a class called first year with above attributes and define a suitable constructor. Also write a method called best Student() which process a first-year object and return the student with the highest total mark. In the main method define a first-year object and find the best student of this class

import java.io.*;

class Program51

{

     public String cname, sname;

     public static int big,count=-1;

     public static void main(String args[]) throws IOException

    {

          int n;  // Total number of students.

          System.out.println("How many students?");

          DataInputStream in= new DataInputStream(System.in);

          n=Integer.parseInt(in.readLine());

         FirstYear  student[] = new FirstYear[n];

          //System.out.println("n="+n);

         

          for(int i=0;i<n;i++)

          {

System.out.println("Enter the Class Name, Student Name and Total Marks");

              student[i] = new FirstYear("abc","xyz");

                                 student[i].read(in.readLine(),in.readLine(),Integer.parseInt(in.readLine()));

          }

       

          for(int i=0;i<n;i++)

          {

              student[i].BestStudent();

          }

          System.out.println("Best Student Details:");

    System.out.println("StudentName: "+student[count].sname+

"\nClass: "+student[count].cname+"\nTotalMarks: "+big);

     }

}

class FirstYear

{

          public int total=0;

          public String cname, sname;

         

          Program51 obj=new Program51();

          FirstYear(String cname, String sname)

          {

              this.cname = cname;

              this.sname = sname;

          }

       public void read(String cname, String sname,int total )

          {

              this.cname=cname;

              this.sname=sname;

              this.total=total;

         

          }

    

          public void BestStudent()

          {

              if(this.total>obj.big)

              {

                   obj.big=this.total;

                   obj.count++;

               }

          }

}

No comments :

Post a Comment

FREE Hit Counters