Page 391 - Computer science 868 Class 12
P. 391

}
                       public void acceptsent()
                       {
                       Scanner sc=new Scanner(System.in);
                       System.out.println("Enter a sentence");
                       sent=sc.nextLine().toUpperCase();
                       }
                       public void sort(SortAlpha P)
                       {
                       String arr[]=P.sent.split("\\s");
                       int len=arr.length;
                       for(int i=0;i<len-1;i++)
                       {
                       for(int j=0;j<len-1-i;j++)
                       {
                       if(arr[j].compareTo(arr[j+1])>0)
                       {
                       String x=arr[j];
                       arr[j]=arr[j+1];
                       arr[j+1]=x;
                       }
                       }
                       }
                       for(int i=0;i<len;i++)
                       sent+=arr[i]+" ";
                       }
                       public void display()
                       {
                       System.out.println(sent);
                       }
                       public static void main(String args[])
                       {
                       SortAlpha obj=new SortAlpha();
                       SortAlpha tem=new SortAlpha();
                       obj.acceptsent();
                       tem.sort(obj);
                       obj.display();
                       tem.display();
                       }
                       }
                    2. (i)   Design a class Unique, which checks whether a word begins and ends with a vowel. Example: APPLE, ARORA etc.
                          The details of the members of the class are given below:                               [ISC 2022]
                          Class name Unique
                          Data members/instance variables
                          Word                                      :  stores a word
                          len                                       :  to store the length of the word
                          Methods/Member functions
                          Unique()                                  :  default constructor
                          void acceptword()                         :  to accept the word in UPPER CASE
                           boolean checkUnique( )                   :   checks and returns true’ if the word starts and ends with a vowel
                                                                      otherwise returns ‘false
                          void display()                            :  displays the word along with an appropriate message
                            Specify the class Unique, giving details of the void acceptword( ), boolean checkUnique( ) and void display( ). Define the
                          main( ) function to create an object and call the functions accordingly to enable the task.
                   Ans.  import java.util.*;
                       class Unique
                       {
                           String word;
                           int len;

                                                                                                                       389
                                                                                                              Strings  389
   386   387   388   389   390   391   392   393   394   395   396