Monday, 3 November 2014

Define Student class.Define default constructors,override the toString method.Keep count objects created.Create objects using parameterized constructors and display object count after each object is created.Also display contents of each objects.




Name    : Shreyal Mandot

Title   : Assignment 2 SetA 1. Define Student class.Define default constructors,override the toString method.Keep count objects created.Create objects using parameterized constructors and display object count after each object is created.Also display contents of each objects.



import java.io.*;

class student
{

static int num;
int rollno;
double per;
String name;

student()
{

System.out.println("\nObject Created : Obj No :"+num);
rollno=0;
per=0;
name="No Name Assigned";

}

student(int a,double b,String c)
{

System.out.println("\nObject created.\nobject no:"+num);
rollno=a;
per=b;
name=c;

}

void display()
{

System.out.println("\nRoll no : "+rollno+"\nName : "+name+"\nPercentage : "+per);

}

public String toString()
{

String string="Name : "+name+"Rollno : "+rollno+" "+"Percentage : "+per;
  return(string);

}

}

class ass2_1
{

public static void main(String args[])
{

try
{
         
int n1,a1;
double b1;
String c1;

System.out.println("\nEnter No Of Students : ");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n1=Integer.parseInt(br.readLine());

student o[]=new student[n1];

System.out.println("\nEnter The Data : ");

for(int i=0;i<n1;i++)
{

System.out.print("\nRoll No : ");
  a1=Integer.parseInt(br.readLine());

System.out.print("\nPercentage : ");
b1=Double.parseDouble(br.readLine());

System.out.print("\nName:");
c1=br.readLine();

student.num++;

o[i]=new student(a1,b1,c1);

}

System.out.println("\nTotal No Of Students : "+student.num+"\nData : \n");

for(int i=0;i<n1;i++)
o[i].display();

System.out.println("\ntoString : ");

for(int i=0;i<n1;i++)
System.out.println("\nConversion toString : "+o[i].toString());

}

catch(IOException e)
{

System.out.println(e);

}

}

}

/*

Output-


*/

1 comment: