Page 114 - ComputerScience_Class_11
P. 114

21          }
                22          public static void main(String[] args)

                23          {
                24              rectangle obj = new rectangle();
                25              obj.assign();

                26              obj.cal_area();

                27              obj.cal_perimeter();
                28              obj.display();
                29          }

                30      }

              What does the above code mean?
              1.  In the above program, an object is being created. The name of the object is “obj”. The line that is used to create the
                 object is “rectangle obj = new rectangle();”.
              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.
              The output of the preceding program is as follows:

                      BlueJ: Terminal Window - Java

                   Options
                  Area : 10 cm
                  Perimeter : 14 cm


              Let’s consider another example:

                Program 2     Write a program to store and display student details, including their marks in Computer and
                              Mathematics and calculate the average marks and total marks for each student.

                1       import java.util.*;
                2       class student

                3       {
                4           String name;

                5           int roll_no, comp_m, math_m, total_m;
                6           student()

                7           {
                8               name="";




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