JAVA-Student class-Creating an array of four Student objects and sort the array according to their marks in descending order

Q. Create a class called Student with the following: 
a. Instance variables – name, rollno, marks; use wrapper class to declare them 
b. A three-argument constructor 
c. A method to display the Student detail 
Write a JAVA program to create an array of four Student objects and sort the array according to their marks in descending order using a method that will take the array reference as argument.

//import java.io.*;
class student
{
String name;
int rollno;
double  marks;
student(String a,int b,double c)
{
name=a;
rollno=b;
marks=c;
}
void display()
{
System.out.print("NAME:"+name+"\t");
System.out.print("ROLL NUMBER:"+rollno+"\t");
System.out.println("MARKS:"+marks);
}
}
class progg2
{
public static void main(String args[])
{
int roll,i,j,t2;
double marks,t3;
String name,t1;
student s[]=new student[4];
s[0]=new student("Sukhendu",17,60);
s[1]=new student("Mou",15,100);
s[2]=new student("Suvajit",01,80);
s[3]=new student("Sankha",9,70);
for(i=0;i<=3;i++)
{
s[i].display();
}
for(i=0;i<=3;i++)
{
for(j=0;j<4-i-1;j++)
{
if(s[j].marks<s[j+1].marks)
{
t1=s[j].name;
t2=s[j].rollno;
t3=s[j].marks;
s[j].name=s[j+1].name;
s[j].rollno=s[j+1].rollno;
s[j].marks=s[j+1].marks;
s[j+1].name=t1;
s[j+1].rollno=t2;
s[j+1].marks=t3;
}
}
}
System.out.println("After sorting......");
for(i=0;i<4;i++)
{
s[i].display();
}
}
}
Output:
NAME:Sukhendu  ROLL NUMBER:17  MARKS:60.0
NAME:Mou  ROLL NUMBER:15  MARKS:100.0
NAME:Suvajit  ROLL NUMBER:01  MARKS:80.0
NAME:Sankha  ROLL NUMBER:9  MARKS:70.0
After sorting......




NAME:Mou  ROLL NUMBER:15  MARKS:100.0
NAME:Suvajit  ROLL NUMBER:01  MARKS:80.0
NAME:Sankha  ROLL NUMBER:9  MARKS:70.0
NAME:Sukhendu  ROLL NUMBER:17  MARKS:60.0


//if you have any problem, comment below

Comments