Page 580 - Computer science 868 Class 12
P. 580

PROGRAMS ON PASSING AND RETURNING OBJECTS


                Program 5      Add Rupees and Paisa using Object Passing.
                               A class name called Amount is defined to add two Amount objects and print it. The details
                               of the class is given below:
                               Data Members
                               int r, p                     :  to enter rupees and paise
                               Member Function
                               Amount(int, int)             :  to initialise r as a and p as b
                               Amount add(Amount, Amount)  :  to add Amount objects and return the result
                               void display()               :  to display amount as rupees and paisa


                 1       import java.util.*;

                 2       class Amount

                 3       {
                 4       Scanner sc = new Scanner(System.in);
                 5       int r,p;

                 6       Amount(int a, int b)
                 7       {

                 8       r=a;
                 9       p=b;

                10       }
                11       Amount add(Amount A1, Amount A2)

                12       {
                13       Amount A3=new Amount(0,0);

                14       A3.r= A1.r+A2.r+(A1.p+A2.p)/100;  // rupees
                15       A3.p= (A1.p+A2.p)%100;  // paisa

                16       return A3;
                17       }

                18       void display()
                19       {

                20       System.out.println(r+"RUPEES AND"+p+"PAISE");
                21       }

                22       public static void main(String args[])
                23       {

                24       Scanner sc = new Scanner(System.in);
                25       System.out.println("ENTER RUPEES OF THE FIRST OBJECT");



                578578  Touchpad Computer Science-XII
   575   576   577   578   579   580   581   582   583   584   585