Sie sind auf Seite 1von 13

Write a Java class to store following

information :
Name

Roll
Number

Cgpa

Ankit

101403001 7.5

Payal

101403002 8.0

Tarun

101403003 8.5

public class Student {


String name;
long rollNumber;
double cgpa;
int semester;
}

Semester

public class StudentMain {


public static void main(String[] args) {
// creating first object
Student std1=new Student();
std1.name="Ankit";
std1.rollNumber=101403001L;
std1.cgpa=7.5;
std1.semester=2;
// creating second object
Student std2=new Student();
std2.name="Payal";
std2.rollNumber=101403002L;
std2.cgpa=8.0;
std2.semester=2;
// creating third object
Student std3=new Student();
std3.name="Tarun";
std3.rollNumber=101403003L;
std3.cgpa=8.5;
std3.semester=2;
}
}

Now, further add methods to Student class so


that :
instance variables (attributes) can be
initialized
value of cgpa can be changed for a student
value of cgpa can be read for a student

public class Student {


String name;
long rollNumber;
double cgpa;
int semester;
public void setDetails(String n,long rn, double cg, int sem){
name=n;
rollNumber=rn;
cgpa=cg;
semester=sem;
}
public double getCgpa(){
return cgpa;
}
public void setCgpa(double newCg){
cgpa=newCg;
}
}

public class StudentMain {


public static void main(String[] args) {
// creating first object
Student std1=new Student();
/*std1.name="Ankit";
std1.rollNumber=101403001L;
std1.cgpa=7.5;
std1.semester=2;*/
std1.setDetails("Ankit", 101403001L, 7.5, 2);
// creating second object
Student std2=new Student();
/*std2.name="Payal";
std2.rollNumber=101403002L;
std2.cgpa=8.0;
std2.semester=2;*/
std2.setDetails("Payal", 101403002L, 8.0, 2);
std2.setCgpa(8.5);
System.out.println("New cgpa of std2="+std2.getCgpa());
}
}

Constructors
tedious to initialize all of the variables in a
class each time an instance is created.
Even when you add convenience functions
like setDetails( ), it would be simpler and
more concise to have all of the setup done at
the time the object is first created
Because the requirement for initialization is
so common, Java allows objects to initialize
themselves when they are created. This
automatic initialization is performed through
the use of a constructor.

Constructors
A constructor initializes an object immediately
upon creation.
It has the same name as the class in which it
resides
Once defined, the constructor is
automatically called immediately after the
object is created, before the new operator
completes.
they have no return type, not even void

public class Student


{

public class StudentMain {

String name;
long rollNumber;
double cgpa;
int semester;

public static void main(String[] args) {


Student std3=new Student();
System.out.println(name="+std3.name);
System.out.println("rollno="+std3.rollNu
mber);
System.out.println("cgpa="+std3.cgpa);
System.out.println("sem="+std3.semester)
;

public Student(){
name="abc";
rollNumber=1014005;
}
}

Output
:
name=abc
rollno=1014005
cgpa=0.0
sem=0

Constructors
new Student( ) is calling the Student( )
constructor.
When you do not explicitly define a constructor for
a class, then Java creates a default constructor for
the class. This is why the preceding line of code
worked in earlier program also.
The default constructor automatically initializes all
instance variables to zero.
The default constructor is often sufficient for
simple classes, but it usually wont do for more
sophisticated ones.

Parameterized Constructors
While the Student( ) constructor in the
preceding example does initialize a Student
object, it is not very usefulall students will
be initialized with same name and rollno.
What is needed is a way to construct Student
objects of different name and rollno values.
The easy solution is to add parameters to the
constructor.

public class Student {


String name;
long rollNumber;
double cgpa;
int semester;
public Student(String n,long rn, double cg, int sem ){
name=n;
rollNumber=rn;
cgpa=cg;
semester=sem;
}
}

public class StudentMain {


public static void main(String[] args) {
Student std1=new Student("Ankit",101403001L,7.5,2);
Student std2=new Student("Payal",101403002L,8.0,2);
Student std3=new Student("Tarun",101403003L,8.5,2);
System.out.println("Student 1 details");
System.out.println("name="+std1.name+"
rollno="+std1.rollNumber+" cgpa="+std1.cgpa+"
sem="+std1.semester);
System.out.println("Student 2 details");
System.out.println("name="+std2.name+"
rollno="+std2.rollNumber+" cgpa="+std2.cgpa+"
sem="+std2.semester);
System.out.println("Student 3 details");
System.out.println("name="+std3.name+"
rollno="+std3.rollNumber+" cgpa="+std3.cgpa+"
sem="+std3.semester);
}
}

Output:
Student 1 details
name=Ankit rollno=101403001 cgpa=7.5 sem=2
Student 2 details
name=Payal rollno=101403002 cgpa=8.0 sem=2
Student 3 details
name=Tarun rollno=101403003 cgpa=8.5 sem=2

Das könnte Ihnen auch gefallen