मराठी

Design a class PendulumS to perform an operation on a word containing alphabets in upper case only. Rearrange the word by putting the lowest ASCII value character at the centre

Advertisements
Advertisements

प्रश्न

Design a class PendulumS to perform an operation on a word containing alphabets in upper case only. Rearrange the word by putting the lowest ASCII value character at the centre and the second lowest ASCII value character to its right and the third to its left and so on.

Example 1: Input: COMPUTER

                   Output: TPMCEORU

Example 2: Input: SCIENCE

                   Output: SIECCEN

The details of the members of the class are given below:

Class name : PendulumS
Data members/instance variables:
wrd : to store the original word
newwrd : to store the rearranged word
Methods/Member functions:
PendulumS(String k) : parameterised constructor to initialise wrd = k and newwrd = ""
int minCharIndex(String str) : to find the index of the minimum ASCII value character in str and return it
void arrange() : to rearrange the characters of wrd as per the given instructions and store it in newwrd by invoking minCharIndeх()
void display() : to display the original word and the rearranged word

Specify the class PendulumS giving the details of the constructor( ), int minCharIndex(String), void arrange() and void display(). Define the main( ) function to create an object and call the functions accordingly to enable the task.

कोड लेखन
Advertisements

उत्तर

import java.util.Scanner;

class PendulumS {
    String wrd, newwrd;

    //Parameterised constructor
    PendulumS(String k) {
        wrd = k.toUpperCase();
        newwrd = "";
    }

    // Find the index of the minimum ASCII character
    int minCharIndex(String str) {
        int minIdx = 0;
        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) < str.charAt(minIdx)) {
                minIdx = i;
            }
        }
        return minIdx;
    }

    // Rearrange characters as per pendulum logic
    void arrange() {
        int len = wrd.length();
        char[] sortedChars = new char[len];
        String temp = wrd;

        // Sort characters manually by repeatedly finding the minimum
        for (int i = 0; i < len; i++) {
            int idx = minCharIndex(temp);
            sortedChars[i] = temp.charAt(idx);
            // Remove the found char to find the next minimum
            temp = temp.substring(0, idx) + temp.substring(idx + 1);
        }

        // Build the pendulum string
        char[] res = new char[len];
        int mid = (len - 1) / 2;
        res[mid] = sortedChars[0]; // Lowest at center

        int left = mid - 1;
        int right = mid + 1;

        for (int i = 1; i < len; i++) {
            if (i % 2 != 0) { // Odd index (2nd, 4th...) goes to right
                res[right++] = sortedChars[i];
            } else { // Even index (3rd, 5th...) goes to left
                res[left--] = sortedChars[i];
            }
        }
        newwrd = new String(res);
    }

    void display() {
        System.out.println("Original Word: " + wrd);
        System.out.println("Rearranged Word: " + newwrd);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String input = sc.next();

        PendulumS obj = new PendulumS(input);
        obj.arrange();
        obj.display();
    }
}
shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2025-2026 (March) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×