Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Create a class which generates student merit list according to the percentage and total marks of three subjects

  1. /*
  2.  * Create a class which generates student merit list. Get number of students to enter followed by
  3.  * details of each student and then display the student merit list according to the percentage and
  4.  * total marks.
  5.  */
  6.  
  7. import java.io.DataInputStream;
  8.  
  9. class Student
  10. {
  11.  int sno;
  12.  String sname;
  13.  int m[] = new int[3];
  14.  int total;
  15.  float percentage;
  16.  
  17.  void getdata()
  18.  {
  19.   DataInputStream in = new DataInputStream(System.in);
  20.  
  21.   try
  22.   {
  23.    System.out.print("Enter Student No. ");
  24.    sno = Integer.parseInt(in.readLine());
  25.    
  26.    System.out.print("Enter Student Name. ");
  27.    sname = in.readLine();
  28.    
  29.    System.out.print("Marks of Subject # 1: ");
  30.    m[0] = Integer.parseInt(in.readLine());
  31.    
  32.    System.out.print("Marks of Subject # 2: ");
  33.    m[1] = Integer.parseInt(in.readLine());
  34.    
  35.    System.out.print("Marks of Subject # 3: ");
  36.    m[2] = Integer.parseInt(in.readLine());
  37.    
  38.   }
  39.   catch(Exception e)
  40.   {
  41.    System.out.println("IO Exception  " + e);
  42.    System.exit(1);
  43.   }
  44.  
  45.  }
  46.  
  47.  int getTotal()
  48.  {
  49.   return(m[0] + m[1] + m[2]);
  50.  }
  51.  
  52.  float getPercentage()
  53.  {
  54.   return(getTotal()/3.0f);
  55.  }
  56.  
  57.  void printStudent()
  58.  {
  59.   System.out.println("Student No. : "+ sno);
  60.   System.out.println("Student Name. : "+ sname);
  61.   System.out.println("Subject # 1 : "+ m[0]);
  62.   System.out.println("Subject # 2 : "+ m[1]);
  63.   System.out.println("Subject # 3 : "+ m[2]);
  64.  }
  65.  
  66. }
  67.  
  68. class StudentMeritList
  69. {
  70.  public static void main(String args[])
  71.  {
  72.   int i,j, n = 0;
  73.   Student ts = new Student();
  74.  
  75.   DataInputStream in = new DataInputStream(System.in);
  76.   try
  77.   {
  78.    System.out.print("How Many Students? ");
  79.    n = Integer.parseInt(in.readLine());
  80.   }
  81.   catch(Exception e)
  82.   {
  83.    System.out.println("Exception " + e );
  84.    System.exit(1);
  85.   }
  86.  
  87.   Student s[] = new Student[n];
  88.  
  89.   for(i=0; i<n; i++)
  90.   {
  91.    s[i] = new Student();
  92.    s[i].getdata();
  93.   }
  94.  
  95.   for(i=0; i<n-1; i++)
  96.   {
  97.    for(j=i+1; j<n; j++)
  98.    {
  99.     if(s[i].getPercentage() < s[j].getPercentage())
  100.     {
  101.      ts = s[i];
  102.      s[i] = s[j];
  103.      s[j] = ts;
  104.     }
  105.    }
  106.   }
  107.   System.out.println("\t\tStudents Merit List");
  108.   System.out.println("\t\t===============");
  109.  
  110.   for(i=0; i<n; i++)
  111.   {
  112.    s[i].printStudent();
  113.    System.out.println("Total Marks: " + s[i].getTotal());
  114.    System.out.println("Percentage: " + s[i].getPercentage());
  115.   }
  116.  }
  117. }
  118.  
  119.  
Your rating: None Average: 5 (1 vote)
Share this

Post new comment

5 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.