Page 116 - ComputerScience_Class_11
P. 116

44              st1.accept();

                45              st2.accept();
                46              st1.display_result();

                47              st2.display_result();
                48          }
                49      }

              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_no, 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().
              The output of the preceding program is as follows:

                      BlueJ: Terminal Window - Java
                   Options

                  Enter name of the student : Ram Kumar
                  Enter roll number of the student : 1
                  Enter computer marks of the student : 98
                  Enter math marks of the student : 98
                  Enter name of the student : Amit Singh
                  Enter roll number of the student : 2
                  Enter computer marks of the student : 90
                  Enter math marks of the student : 80
                  ---------------------------------------------------------------------------------------------------------------
                  Name                Roll               Computer Marks       Maths Marks
                  Ram Kumar           1                  98                   98
                  Average Marks       Total Marks
                  98.0                196
                  ---------------------------------------------------------------------------------------------------------------
                  ---------------------------------------------------------------------------------------------------------------
                  Name                Roll               Computer Marks       Maths Marks
                  Amit Singh          2                  90                   80
                  Average Marks       Total Marks
                  85.0                170
                  ---------------------------------------------------------------------------------------------------------------


              5.2.2 Properties of Class and Object
              Class and object have the following properties:
              •  Class is an object factory: Factories are the places that produce products of the same kind. A class acts as a factory
                 as by using it, similar types of objects are created with different characteristics and common behaviours. We can
                 also say a class is a blueprint of objects. Hence, a class is called an object factory.



                  114  Touchpad Computer Science (Ver. 3.0)-XI
   111   112   113   114   115   116   117   118   119   120   121