Page 459 - Cs_withBlue_J_C11_Flipbook
P. 459

Program 2      Write  a  program  to  create  a  package  Eleven  containing  class  Number  having  the  following
                                 specifications.
                                 Package                :  Eleven
                                 Class name             :  Number
                                 Member Methods
                                                                    p
                                 int topower(int n,int p)  :  Returns n  (Assume p is positive)
                                 int numlength(int n)   :  Counts and returns number of digits of ‘n’
                                 An Armstrong number is a positive n-digit number that is equal to the sum of the nth powers
                                 of their digits.
                                                3
                                                        3
                                                    3
                                 Example  153 = 1  + 5  + 3  (power taken as 3 because 153 is a 3 digit number)
                                         4
                                                 4
                                                     4
                                             4
                                 1634 = 1  + 6  + 3  + 4   (power taken as 4 because 1634 is a 4 digit number)
                                 Class Armstrong has the following class description. It calls methods int topower(int,int) and
                                 numlength(int) of class Number belonging to package Eleven in another package Orange_prog
                                 and checks if the given number is Armstrong number.
                                 Class name          :  Armstrong
                                 Package             :  Orange_prog
                                 Data Members
                                 int x               :  To store the number
                                 void accept()       :  Accepts number ‘x’
                                 void checks()       :   Checks whether ‘x’ is an Armstrong number or not by calling methods
                                                        int topower(int,int) and numlength(int) of class Number belonging to
                                                        package Eleven.


                   1       package Eleven; // package declaration
                   2       public class Number

                   3       {
                   4       // methods of class Number

                   5           public int topower(int n,int p)
                   6           {
                   7               if(p==0)

                   8                   return 1;

                   9               else
                  10                   return n*topower(n,p-1);
                  11           }

                  12           public int numlength(int n)
                  13           { if(n==0)

                  14                 return 0;
                  15             else

                  16                 return 1+numlength(n/10);
                  17           }

                  18       }



                                                                                                                       457
                                                                                                            Packages   457
   454   455   456   457   458   459   460   461   462   463   464