Q.6 Define Student class(roll_no, name, percentage) to create n objects of the Student class. Accept details from the user for each object. Define a static method “sortStudent” which sorts the array on the basis of percentage.

Q.6 Define Student class(roll_no, name, percentage) to create n objects of the Student class. Accept details from the user for each object. Define a static method “sortStudent” which sorts the array on the basis of percentage.
import java.io.*; import java.util.*; class student { int rno; String name; float per; static int cnt=0; student() // Default constuctor { rno=100; name="Shreya"; per=85; } student(int rno,String name,float per) // parameterized constuctor { this.rno=rno; this.name=name; this.per=per; cnt++; } public void display() { System.out.print(" "+rno); System.out.print("\t"+name); System.out.print("\t" +per); System.out.println(""); } public static void sort(student s[],int n) { for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(s[i].per>s[j].per) { student t=s[i]; s[i]=s[j]; s[j]=t; } } } for(int i=0;i<n;i++) s[i].display(); } } class studentsort { public static void main(String args[]) { Scanner sc=new Scanner(System.in); student s=new student(); System.out.println("** Default Student Information****"); s.display(); System.out.println("How many student you want"); int n= sc.nextInt(); student [] s1=new student[n]; // Array of objects for(int i=0;i<n;i++) { System.out.println("Enter the roll number"); s.rno=sc.nextInt(); System.out.println("Enter the name"); s.name=sc.next(); System.out.println("Enter the student percentage"); s.per=sc.nextFloat(); s1[i]=new student(s.rno,s.name,s.per); } System.out.println("*** Student Information Before sorting****"); System.out.println("Roll No \t Name \t Per"); for(int i=0;i<n;i++) s1[i].display(); System.out.println("*** Student Information After sorting****"); System.out.println("Roll no \t Name \t Percentage"); s.sort(s1,s.cnt); } }

Post a Comment

0 Comments