Page 116 - Cs_withBlue_J_C11_Flipbook
P. 116
void display_result()
{
System.out.println("---------------------------------------------------------
----------");
System.out.println("Name "+"\t"+"Roll number"+"\t"+"Computer Marks "+"\t"+"Maths
Marks");
System.out.println(name+"\t"+roll_no+"\t"+comp_m+"\t\t"+math_m);
System.out.println("Average Marks "+"\t"+"Total Marks ");
System.out.println( calculate_marks() +"\t\t"+ total_m);
System.out.println("----------------------------------------------------------
---------");
}
public static void main()
{
student st1=new student();
student st2=new student();
st1.accept();
st2.accept();
st1.display_result();
st2.display_result();
}
}
What does the above code mean?
1. In the above program, two objects are created. The names of the objects are “st1” and “st2”. The syntax to create
the objects is “student <name of object> = new student()”.
2. This class has a constructor, which is invoked when the objects are created and all data members are assigned with
the default values provided in the constructor.
3. After creating the objects st1 and st2, the accept() method is invoked by using dot(.) operator and the data members
name, roll, comp_m and math_m are assigned with the values accepted from the user.
4. Then, the display_result() method is invoked by using dot(.) operator and from this method, the calculate_marks()
is called. The calculate_marks() method returns the average of the marks, which is displayed in the display_result().
When the program is executed, the following result will be displayed.
114114 Touchpad Computer Science-XI

