Advertisements
Advertisements
प्रश्न
Define a class Arrange described as below:
Data members/Instance variables: String str (a word), i, p (to store the length of the word), char ch;
Member Methods:
- a parameterised constructor to initialize the data member
- to accept the word
- to arrange all the alphabets of word in ascending order of their ASCII values without using the sorting technique
- to display the arranged alphabets.
Write a main method to create an object of the class and call the above member methods.
कोड लेखन
उत्तर
import java.util.*;
class Arrange{
String str;
int i, p;
char ch;
Arrange(String a,int b, int c, char d)
{str = a;
i = b;
p = c;
ch = d;
}
void accept()
{Scanner ob = new Scanner(System.in);
System.out.println("Enter a word");
str = ob.next().toUpperCase();
}
void ascending()
{String s2 = "";
p = str.length();
for (int i = 65; i <= 90; i++)
{for (int j = 0; j < p; j++)
{ if (i == (int)str.charAt(j))
s2 += str.charAt(j);
}
}
str = s2;
}
void display()
{System.out.println(str);
}
public static void main(String args[])
{Arrange A = new Arrange("", 0, 0, ' ');
A.accept();
A.ascending();
A.display();
}}
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?