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

Create a class called Student with number, name and marks of 3 subjects

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