Page 394 - Computer science 868 Class 12
P. 394

3.  A class Mix has been defined to mix two words, character by character, in the following manner:   [ISC 2020]
                     The first character of the first word is followed by the first character of the second word and so on. If the words are of different
                    length, the remaining characters of the longer word are put at the end. Example: If the First word is “JUMP” and the second
                    word is “STROLL”, then the required word will be “JSUTMRPOLL”
                     Some of the members of the class are given below:
                    Classname                                     :   Mix
                    Data Member/Instance variable
                    wrd                                           :   to store a word
                    len                                           :   to store the length of the word
                    Member Functions/Methods
                    Mix( )                                        :    default  constructor to initialize the data members with  legal
                                                                    initial values
                    void feedword()                               :   to accept the word in UPPER case
                    void mix_word(Mix P, Mix Q)                   :    mixes the words of objects P and Q as stated above and stores the
                                                                    resultant word in the current object
                    void display()                                :   displays the word
                     Specify the class Mix giving the details of the constructor(), void feedword(), void mix_word(Mix, Mix) and void display().
                    Define the main() function to create objects and call the functions accordingly to enable the task.
                Ans.  import java.util.*;
                    class Mix
                    {
                       String wrd;
                       int len;
                       static Scanner x=new Scanner(System.in);
                       Mix()
                       {
                          wrd="";
                          len=0;
                       }
                       void feedword()
                       {
                          System.out.println( "Enter a word in UPPER CASE");
                          wrd=x.next();
                          len=wrd.length();
                       }
                       void mix_word(Mix P,Mix Q)
                       {
                          int s=(P.len <Q.len)? P.len:Q.len;
                          for(int i=0;i<s;i++)
                            wrd += P.wrd.charAt(i)+""+Q.wrd.charAt(i);
                          if (P.len > Q.len)
                             wrd +=P.wrd.substring(Q.len);
                          else
                             wrd +=Q.wrd.substring(P.len);
                       }
                       void display()
                       {
                          System.out.println("WORD = " + wrd);
                       }
                      public static void main(String[] args)
                       {
                          Mix obj=new Mix();
                          Mix obj1=new Mix();
                          Mix obj2= new Mix();
                          obj.feedword();
                          obj1.feedword();
                          obj2.mix_word (obj,obj1);
                          obj2.display();
                       }
                    }

                392392  Touchpad Computer Science-XII
   389   390   391   392   393   394   395   396   397   398   399