import java.util.*;
class Student
{
String name;
int id,p,c,m,total;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter name: ");
name = sc.next();
System.out.print("Enter ID: ");
id = sc.nextInt();
System.out.print("Enter Physics marks: ");
p = sc.nextInt();
System.out.print("Enter Chemistry marks: ");
c = sc.nextInt();
System.out.print("Enter Maths marks: ");
m = sc.nextInt();
total = p+c+m;
}
void display()
{
System.out.print("\nName:"+name+" ID:"+id+" Physics:"+p+" Chemistry:"+c+" Maths:"+m+" Total:"+total);
}
static void sort_marks(Student s[])
{
int n = s.length;
Student temp = new Student();
for(int i=1; i<n; i++)
{
for(int j=0; j<(n-i); j++)
{
if(s[j].total<s[j+1].total)
{
temp =s[j];
s[j] =s[j+1];
s[j+1]=temp;
}
}
}
}
static void sort_name(Student s[])
{
int n = s.length;
Student temp = new Student();
for(int i=1; i<n; i++)
{
for(int j=0; j<(n-i); j++)
{
if(s[j].name.compareTo(s[j+1].name)>0)
{
temp =s[j];
s[j] =s[j+1];
s[j+1]=temp;
}
}
}
}
}
class ArrayObjectDemo
{
public static void main(String soham[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter number of students: ");
int n = sc.nextInt();
Student s[]=new Student[n];
for(int i=0; i<=(n-1); i++)
{
System.out.print("\nEnter Details for Student "+(i+1));
s[i] = new Student();
s[i].accept();
}
//Sorted according to total marks
Student.sort_marks(s);
System.out.print("\nSorted according to total marks: ");
for(int i=0; i<=(n-1); i++)
{
s[i].display();
}
System.out.println();
//Sorted according to name
Student.sort_name(s);
System.out.print("\nSorted according to name: ");
for(int i=0; i<=(n-1); i++)
{
s[i].display();
}
System.out.println();
}
}
class Student
{
String name;
int id,p,c,m,total;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter name: ");
name = sc.next();
System.out.print("Enter ID: ");
id = sc.nextInt();
System.out.print("Enter Physics marks: ");
p = sc.nextInt();
System.out.print("Enter Chemistry marks: ");
c = sc.nextInt();
System.out.print("Enter Maths marks: ");
m = sc.nextInt();
total = p+c+m;
}
void display()
{
System.out.print("\nName:"+name+" ID:"+id+" Physics:"+p+" Chemistry:"+c+" Maths:"+m+" Total:"+total);
}
static void sort_marks(Student s[])
{
int n = s.length;
Student temp = new Student();
for(int i=1; i<n; i++)
{
for(int j=0; j<(n-i); j++)
{
if(s[j].total<s[j+1].total)
{
temp =s[j];
s[j] =s[j+1];
s[j+1]=temp;
}
}
}
}
static void sort_name(Student s[])
{
int n = s.length;
Student temp = new Student();
for(int i=1; i<n; i++)
{
for(int j=0; j<(n-i); j++)
{
if(s[j].name.compareTo(s[j+1].name)>0)
{
temp =s[j];
s[j] =s[j+1];
s[j+1]=temp;
}
}
}
}
}
class ArrayObjectDemo
{
public static void main(String soham[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter number of students: ");
int n = sc.nextInt();
Student s[]=new Student[n];
for(int i=0; i<=(n-1); i++)
{
System.out.print("\nEnter Details for Student "+(i+1));
s[i] = new Student();
s[i].accept();
}
//Sorted according to total marks
Student.sort_marks(s);
System.out.print("\nSorted according to total marks: ");
for(int i=0; i<=(n-1); i++)
{
s[i].display();
}
System.out.println();
//Sorted according to name
Student.sort_name(s);
System.out.print("\nSorted according to name: ");
for(int i=0; i<=(n-1); i++)
{
s[i].display();
}
System.out.println();
}
}
Output:
Enter number of students: 3
Enter Details for Student 1
Enter name: ABC
Enter ID: 101
Enter Physics marks: 90
Enter Chemistry marks: 70
Enter Maths marks: 80
Enter Details for Student 2
Enter name: XYZ
Enter ID: 102
Enter Physics marks: 80
Enter Chemistry marks: 70
Enter Maths marks: 80
Enter Details for Student 3
Enter name: PQR
Enter ID: 103
Enter Physics marks: 90
Enter Chemistry marks: 70
Enter Maths marks: 60
Sorted according to total marks:
Name:ABC ID:101 Physics:90 Chemistry:70 Maths:80 Total:240
Name:XYZ ID:102 Physics:80 Chemistry:70 Maths:80 Total:230
Name:PQR ID:103 Physics:90 Chemistry:70 Maths:60 Total:220
Sorted according to name:
Name:ABC ID:101 Physics:90 Chemistry:70 Maths:80 Total:240
Name:PQR ID:103 Physics:90 Chemistry:70 Maths:60 Total:220
Name:XYZ ID:102 Physics:80 Chemistry:70 Maths:80 Total:230
Enter Details for Student 1
Enter name: ABC
Enter ID: 101
Enter Physics marks: 90
Enter Chemistry marks: 70
Enter Maths marks: 80
Enter Details for Student 2
Enter name: XYZ
Enter ID: 102
Enter Physics marks: 80
Enter Chemistry marks: 70
Enter Maths marks: 80
Enter Details for Student 3
Enter name: PQR
Enter ID: 103
Enter Physics marks: 90
Enter Chemistry marks: 70
Enter Maths marks: 60
Sorted according to total marks:
Name:ABC ID:101 Physics:90 Chemistry:70 Maths:80 Total:240
Name:XYZ ID:102 Physics:80 Chemistry:70 Maths:80 Total:230
Name:PQR ID:103 Physics:90 Chemistry:70 Maths:60 Total:220
Sorted according to name:
Name:ABC ID:101 Physics:90 Chemistry:70 Maths:80 Total:240
Name:PQR ID:103 Physics:90 Chemistry:70 Maths:60 Total:220
Name:XYZ ID:102 Physics:80 Chemistry:70 Maths:80 Total:230
No comments:
Post a comment