Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- * Create a class which generates student merit list. Get number of students to enter followed by
- * details of each student and then display the student merit list according to the percentage and
- * total marks.
- */
- import java.io.DataInputStream;
- class Student
- {
- int sno;
- String sname;
- int m[] = new int[3];
- int total;
- float percentage;
- void getdata()
- {
- DataInputStream in = new DataInputStream(System.in);
- try
- {
- System.out.print("Enter Student No. ");
- sno = Integer.parseInt(in.readLine());
- System.out.print("Enter Student Name. ");
- sname = in.readLine();
- System.out.print("Marks of Subject # 1: ");
- m[0] = Integer.parseInt(in.readLine());
- System.out.print("Marks of Subject # 2: ");
- m[1] = Integer.parseInt(in.readLine());
- System.out.print("Marks of Subject # 3: ");
- m[2] = Integer.parseInt(in.readLine());
- }
- catch(Exception e)
- {
- System.out.println("IO Exception " + e);
- System.exit(1);
- }
- }
- int getTotal()
- {
- return(m[0] + m[1] + m[2]);
- }
- float getPercentage()
- {
- return(getTotal()/3.0f);
- }
- void printStudent()
- {
- System.out.println("Student No. : "+ sno);
- System.out.println("Student Name. : "+ sname);
- System.out.println("Subject # 1 : "+ m[0]);
- System.out.println("Subject # 2 : "+ m[1]);
- System.out.println("Subject # 3 : "+ m[2]);
- }
- }
- class StudentMeritList
- {
- public static void main(String args[])
- {
- int i,j, n = 0;
- Student ts = new Student();
- DataInputStream in = new DataInputStream(System.in);
- try
- {
- System.out.print("How Many Students? ");
- n = Integer.parseInt(in.readLine());
- }
- catch(Exception e)
- {
- System.out.println("Exception " + e );
- System.exit(1);
- }
- Student s[] = new Student[n];
- for(i=0; i<n; i++)
- {
- s[i] = new Student();
- s[i].getdata();
- }
- for(i=0; i<n-1; i++)
- {
- for(j=i+1; j<n; j++)
- {
- if(s[i].getPercentage() < s[j].getPercentage())
- {
- ts = s[i];
- s[i] = s[j];
- s[j] = ts;
- }
- }
- }
- System.out.println("\t\tStudents Merit List");
- System.out.println("\t\t===============");
- for(i=0; i<n; i++)
- {
- s[i].printStudent();
- System.out.println("Total Marks: " + s[i].getTotal());
- System.out.println("Percentage: " + s[i].getPercentage());
- }
- }
- }

Nice post. I used to be
Post new comment