Computer Applications

Define a class Arrange described as below:

Data members/instance variables:

  1. String str (a word)
  2. String i
  3. int p (to store the length of the word)
  4. char ch;

Member Methods:

  1. A parameterised constructor to initialize the data member
  2. To accept the word
  3. To arrange all the alphabets of word in ascending order of their ASCII values without using the sorting technique
  4. To display the arranged alphabets.

Write a main method to create an object of the class and call the above member methods.

Java

Java Constructors

48 Likes

Answer

import java.util.Scanner;

public class Arrange
{
    private String str;
    private String i;
    private int p;
    private char ch;
    
    public Arrange(String s) {
        str = s;
        i = "";
        p = s.length();
        ch = 0;
    }
    
    public void rearrange() {
        for (int a = 65; a <= 90; a++) {
            for (int j = 0; j < p; j++) {
                ch = str.charAt(j);
                if (a == Character.toUpperCase(ch))
                    i += ch;
            }
        }
    }
    
    public void display() {
        System.out.println("Alphabets in ascending order:");
        System.out.println(i);
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String word = in.nextLine();
        
        Arrange obj = new Arrange(word);
        obj.rearrange();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

18 Likes


Related Questions