Name : Shreyal Mandot
Title : Assignment 2 SetA 2. Write a java program to create n objects of the Student class. Assign roll numbers in the ascending order. Accept name and percentage from the user for each object. Define a static method “sortStudent†which sorts the array on the basis of percentage.
import java.io.*;
class student1
{
static int num;
int rollno;
double per;
String name;
student1()
{
rollno=0;
per=0;
name="No Name Assigned";
}
student1(int a,double b,String c)
{
rollno=a;
per=b;
name=c;
}
void display()
{
System.out.println("\nRoll no : "+rollno+"\nName : "+name+"\nPercentage : "+per);
}
}
class ass2_2
{
static void sortStudent(student1 []s1)
{
double tempd;
String temps;
int tempi;
for(int i=1;i<student1.num;i++)
{
System.out.println(i);
for(int j=0;j<i;j++)
{
if(s1[i].per>s1[j].per)
{
tempi=s1[i].rollno;
tempd=s1[i].per;
temps=s1[i].name;
s1[i].rollno=s1[j].rollno;
s1[i].per=s1[j].per;
s1[i].name=s1[j].name;
s1[j].rollno=tempi;
s1[j].per=tempd;
s1[j].name=temps;
}
}
}
System.out.println("\nData After Sorting In Descending Order Of Percentage : \n");
for(int i=0;i<student1.num;i++)
s1[i].display();
}
public static void main(String args[])
{
try
{
int n1,a1;
double b1;
String c1;
System.out.println("\nEnter The No Of Students : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n1=Integer.parseInt(br.readLine());
student1 o[]=new student1[n1];
System.out.println("\nEnter The Data : \n");
for(int i=0;i<n1;i++)
{
System.out.print("\n\nName : ");
c1=br.readLine();
System.out.print("\n\nRoll No : ");
a1=Integer.parseInt(br.readLine());
System.out.print("\n\nPercentage : ");
b1=Double.parseDouble(br.readLine());
student1.num++;
o[i]=new student1(a1,b1,c1);
}
System.out.println("\nTotal No Of Students : "+student1.num+"\nData Before Sorting : \n");
for(int i=0;i<n1;i++)
o[i].display();
sortStudent(o);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
/*
Output-
[prady@localhost setA]$ javac ass2_2.java
[prady@localhost setA]$ java ass2_2
Enter The No Of Students :
5
Enter The Data :
Name : Pradnya Parandekar
Roll No : 43
Percentage : 68
Name : Pooja Gogate
Roll No : 13
Percentage : 69
Name : Snehal Chougule
Roll No : 3
Percentage : 66
Name : Amit Raut
Roll No : 55
Percentage : 64
Name : Madhuri Katekar
Roll No : 48
Percentage : 70
Total No Of Students : 5
Data Before Sorting :
Roll no : 43
Name : Pradnya Parandekar
Percentage : 68.0
Roll no : 13
Name : Pooja Gogate
Percentage : 69.0
Roll no : 3
Name : Snehal Chougule
Percentage : 66.0
Roll no : 55
Name : Amit Raut
Percentage : 64.0
Roll no : 48
Name : Madhuri Katekar
Percentage : 70.0
Data After Sorting In Descending Order Of Percentage :
Roll no : 48
Name : Madhuri Katekar
Percentage : 70.0
Roll no : 13
Name : Pooja Gogate
Percentage : 69.0
Roll no : 43
Name : Pradnya Parandekar
Percentage : 68.0
Roll no : 3
Name : Snehal Chougule
Percentage : 66.0
Roll no : 55
Name : Amit Raut
Percentage : 64.0
[prady@localhost setA]$
*/
No comments:
Post a Comment