Page 91 - computer science (868) class 11
P. 91
2. After creating the object, the assign() method is invoked and the data members length and breadth are assigned
with the values 5 and 2 respectively.
3. Then, the cal_area() method is executed to calculate the area of the rectangle. The calculated value is assigned to
the data member “area”.
4. After that, the calculation of the perimeter is done by invoking the cal_perimeter() method and the result is stored
in the “perimeter” data member.
5. And at the end of the execution of the program, the display() method is invoked to display the calculated area and
the perimeter of the rectangle.
When the program is executed, the following result will be displayed.
Let’s consider another example:
import java.util.*;
class student
{
String name;
int roll_no, comp_m, math_m, total_m;
student()
{
name="";
roll_no=0;
comp_m=0;
math_m=0;
total_m=0;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name of the student : ");
name=sc.nextLine();
System.out.print("Enter roll number of the student : ");
roll_no=sc.nextInt();
System.out.print("Enter computer marks of the student : ");
comp_m=sc.nextInt();
System.out.print("Enter math marks of the student : ");
math_m=sc.nextInt();
}
double calculate_marks()
{
total_m=comp_m+math_m;
return total_m/2.0;
}
89
Objects 89

