Page 395 - Computer science 868 Class 12
P. 395

4.  Design a class Rearrange using the description of the data members and member functions given below.   [ISC 2019]
                       Class name                                   :   Rearrange
                       Data Members/Instance variables
                       Txt                                          :   to store a word
                       Cxt                                          :   to store the rearranged word
                       len                                          :   to store the length of the word
                       Member Functions
                       Rearrange () :  constructor to initialise the instance variables
                       void readword ()                             :   to accept the word input in UPPERCASE
                       void convert ()                              :   to convert the word into its changed form and store it in string Cxt
                       void display()                               :   to display the original and the changed word
                         Specify the class Rearrange giving the details of the constructor (), void readword (), void convert () and void display (). Define
                       a main () function to create an object and call the functions accordingly to enable the task.

                   Ans.  import java.io.*;
                       import java.util.*;
                       class Rearrange {
                       private String wrd;
                       private String newwrd;
                       public Rearrange() {
                       wrd = new String();
                       newwrd = new String();
                       }
                       public void readword() throws IOException {
                       Scanner sc = new Scanner(System.in);
                       System.out.print(“Enter the word:”);
                       wrd = sc.next();
                       }
                       public void freq_vow_con() {
                       wrd = wrd.toUpperCase();
                       int v = 0;
                       int c = 0;
                       for (int i = 0; i < wrd.length(); i++) {
                       char ch = wrd.charAt(i);
                       if(Character.isLetter(ch)) {
                       switch(ch) {
                       case 'A':
                       case 'E':
                       case 'I':
                       case 'O':
                       case 'U':
                       v++;
                       break;
                       default: c++;
                       }
                       }
                       }
                       System.out.println("Frequency of vowels: "+v);
                       System.out.println("Frequency of consonants: "+c);
                       }

                                                                                                                       393
                                                                                                              Strings  393
   390   391   392   393   394   395   396   397   398   399   400