Page 398 - Computer science 868 Class 12
P. 398

void sortword()                           :    sorts the characters of the original word in alphabetical order
                                                                 and stores it in ‘sortwrd’
                     void display()                            :   displays the original word, swapped word and the sorted word
                      Specify the class SwapSort, giving the details of the constructor(), void readword(), void swapchar(), void sortword() and
                    void display(). Define the main() function to create an object and call the functions accordingly to enable the task.
                Ans.  import java.io.*;
                    public class SwapSort {
                    String wrd;
                    int len;
                    String swapwrd;
                    String sortwrd;
                    SwapSort() {
                    wrd = " ";
                    len = 0;
                    swapwrd = " ";
                    sortwrd = " ";
                    }
                    void readword() throws IOException {
                    InputStreamReader x = new InputStreamReader(System.in);
                    BufferedReader y = new BufferedReader(x);
                    System.out.println(“Enter word:”);
                    wrd = y.readLine(). toUpperCase();
                    }
                    void swapwrd() {
                    String w = wrd;
                    swapwrd = w.charAt(w.length() - 1) + w. substring(1, w.length() - 1) + w.charAt(0);
                    }
                    void sortwrd () {
                    String w = wrd;
                    char[] charArray = w.toCharArray();
                    int length = charArray. length;
                    for(int i = 0; i < length; i++) {
                    for(int j = i + 1; j < length; j++) {
                    if(charArray[j] < charArray[i]) {
                    char temp = charArray[i];
                    charArray[i] = charArray[j];
                    charArray[j] = temp;
                    }
                    }
                    }
                    for(char c : charArray) {
                    sortwrd = sortwrd + c;
                    }
                    }
                    void display() {
                    System.out.println("Original word :" + wrd);
                    System.out.println("Swapped word :" + swapwrd);
                    System.out.println("Sorted word :" + sortwrd);
                    }
                    public static void main(String args[ ]) throws IOException {
                    SwapSort obj = new SwapSort();
                    obj.readword();
                    obj.swapwrd();
                    obj.sortwrd();
                    obj.display();
                    }
                    }







                396396  Touchpad Computer Science-XII
   393   394   395   396   397   398   399   400   401   402   403