TY_BCS_JAVA_SLIP 21_1



Slip 21_1. Define a student class ( roll number, name, percentage). Define a default and parameterized constructor. keep a count of object created. Create object using parameterized constructor and display the object count after each object is created. ( use static member and method). Also display the content of each object. Modify program to create “n” object of the student class. Accept details for each object. Define static method “shortStudent” which shorts the array on the basis
Of percentage.

import java.io.*;
class Student
{
                int rollno;
                String name;
                float per;
                static int count;

                Student(){}
                Student(String n,float p)
                {
                                count++;
                                rollno=count;
                                name=n;
                                per=p;

                }

                void display()
                {
                                System.out.println(rollno+"\t"+name+"\t"+per);
                }
                float getper()
                {
                                return per;
                }
                static void counter()
                {
                                System.out.println(count+" object is created");
                }
                public static void sortStudent(Student s[],int n)
                {
                                for(int i=n-1;i>=0;i--)
                                {
                                                for(int j=0;j<i;j++)
                                                {
                                                                if(s[j].getper()>s[j+1].getper())
                                                                {
                                                                                Student t=s[j];
                                                                                s[j]=s[j+1];
                                                                                s[j+1]=t;
                                                                }
                                                }
                                }
                                for(int i=0;i<n;i++)
                                                s[i].display();

                }
}
class Slip21_1
{
                public static void main(String args[]) throws IOException
                {
                                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                System.out.println("Enter no. of Student:");
                                int n=Integer.parseInt(br.readLine()); 
                                Student p[]=new Student[n];
                                for(int i=0;i<n;i++)
                                {
                                                System.out.print("Enter Name:");
                                                String name=br.readLine();
                                                System.out.print("Enter percentage:");
                                                float per=Float.parseFloat(br.readLine());
                                                p[i]=new Student(name,per);
                                                p[i].counter();
                                }
                                Student.sortStudent(p,Student.count);
                }
}

1 comment: