A)
//executestudent.java
class student
{
int rollno;
String name;
private int marks;
private char grade;
void givedatatostudent(int rno, String nm, int marks)
{
rollno=rno;
name=nm;
this.marks=marks;
calculategrade();
}
void displaystudentdetails()
{
System.out.println("Rollno :"+rollno);
System.out.println("Name :"+name);
System.out.println("Marks :"+marks);
System.out.println("Grade :"+grade);
}
private voidcalculategrade()
{
if(marks>=60)
grade='A';
else if(marks>=50)
grade='B';
else
grade='C';
}
}
class executestudent {
public static void main(String[] args) {
//(1)
student s = new student();
//(2)
s.givedatatostudent(1001,"hari",74);
s.displaystudentdetails();
}
}
Output:
Rollno :1001
Rollno :1001
Name :hari
Marks :74
Grade :A
(1)
In this line the object is in default state. So, in this state the variables contain JVM default values.
(2)
Main is invoking the object behaviour/method I.e. main method calling the give date to student () behaviour that method only changing the behaviour of the object.
So the main not directly change the state of object through “proper channel” by invoking proper behaviour/method.
Comments
Post a Comment