Page 308 - CA_Blue( J )_Class10
P. 308

Let us observe the output of the program:





















                   12.4 DIFFERENCE BETWEEN CONSTRUCTOR AND METHOD
                                  Constructor                                           Method

               It is used to initialize the instance variables of the class.  It is used to execute the Java code which performs a specific job.
               It can be invoked only once at the time of creating the object. It can be invoked anytime as and when required.
               It has the same name as that of the class.         It has the different name from the class.
               It does not have a return type.                    It has a return type.
               It always public.                                  It may be public or private.


                   12.5 SIMILARITY BETWEEN CONSTRUCTOR AND METHOD
              The similarities are as follows:
              1.  Both constructors and methods may or may not have arguments.
              2.  Within the curly brackets, the body of the constructor as well as method contains a sequence of statements that
                  performs a specific task.


                                                                                     21 st
               Some More Programs                                                  Century   #Coding & Computational Thinking
                                                                                     Skills
                              To create a class Perfect with the following details:
                Program 1     Data Members
                              int n                :  Number to check
                              Member Methods
                              Perfect()            :  Constructor
                              void input()         :  Inputs a number to check
                              void isperfect()     :  Checks whether the number is perfect or not.

                 1  import java.util.*;
                 2  class Perfect

                 3  {
                 4      int n;

                 5      Perfect()
                 6      {

                 7          n=0;



                306306  Touchpad Computer Applications-X
   303   304   305   306   307   308   309   310   311   312   313