Define a Student class (roll number, name, percentage). Define a default and parameterized
constructor. Keep a count of objects created. Create objects using parameterized constructor and
display the object count after each object is created. (Use static member and method). Also
display the contents of each object.
a1seta1.java
import java.io.*;
import java.util.*;
class Student
{
static int cnt=0;
int rno;
String name;
double per;
static void sortstudent(Student s1[],int n,Student y)
{
int i,j;
for(i=0;i<n-1;i++)
for(j=1;j<n;j++)
{
if(s1[j].per<s1[i].per)
{
y=s1[i];
s1[i]=s1[j];
s1[j]=y;
}
}
}
static void displaycnt()
{
++cnt;
System.out.println("Object created :"+cnt);
}
void display()
{
System.out.println("Roll No: " +rno+ "Name: " +name+ "Percentage: " +per);
}
Student(int a,String b,double c)
{
rno=a;
name=b;
per=c;
}
Student()
{
rno=0;name=null;per=0.0;
}
}
class a1seta1
{
public static void main(String args [])throws IOException
{
int i;
Scanner s= new Scanner(System.in);
System.out.println("\nHow many objects you will create.\n");
int n=s.nextInt();
Student s1[]=new Student[n];
Student y=new Student();
for(i=0;i<n;i++)
{
System.out.println("\nEnter rno,name and percentage of student.\n");
int rno=s.nextInt();
String name=s.next();
double per=s.nextDouble();
s1[i]=new Student(rno,name,per);
s1[i].display();
Student.displaycnt();
}
Student.sortstudent(s1,n,y);
for(i=0;i<n;i++)
{
s1[i].display();
}
}
}
0 Comments
If anyone has Doubts or suggestions please let me know