Sie sind auf Seite 1von 3

Class Alpha 1/3

/* ashking13th@gmail.com
* javaprogramsbyashking13th.blogspot.in
* ashkingjava.blogspot.in
*
*
* QUESTION
*
* design a class Alpha ehich enables the characters of astring to
* be arranged in alphabetical order.
* the details of the members of the class are given below:
*
* Data members:
* Str :to store the original sentence
* Rstr :to store the arranged letters
*
* Member functions:
* Alpha() :dafault constructor
* void readString() :to accept a sentence i uppercase
* void remove_spaces :removes all the white spaces in the string
* void arrange :to arrange the letters of the sentence in
* alphabetical order using bubble sort
* technique
* void disp() :displays the original sentence
* and the arranged string.
*/
import java.io.*;
public class Alpha
{
String Str; //CREATING DATA MEMBERS
String Rstr; //CREATING DAT MEMBERS
DataInputStream d=new DataInputStream(System.in);
public Alpha() //DEFAULT CONSTUCTOR
{
Str=""; //initialising data members
Rstr="";
}
void readString() throws IOException
/*
* method to input a string in upper case
*/
{
System.out.println("ENTER A SENTENCE IN UPPERCASE");
Str=d.readLine();
}
void remove_spaces()
/*
* method to remove spaces from the string
Mar 25, 2014 3:13:15 PM
Class Alpha (continued) 2/3
*/
{
int l=Str.length();
String s="";
for(int i=0;i<l;i++)
{
if(Str.charAt(i)!=' ')
{
s=(s + Str.charAt(i));
/*
* if character is not awhite space then we
* append it the string storing the new string
*/
}
}
Rstr=s;
/*
* giving the value of space remved string
* to data member Rstr
*/
}
void arrange()
{
int l=Rstr.length();
/*
finding length of the string without spaces
*/
char c[]=new char[l];
/*
creating a character type array with it's size equal to size
of space removed string to store every character of the string
*/
char t;
for(int i=0;i<l;i++)
{
c[i]=Rstr.charAt(i);
}
for(int i=0;i<l;i++)
{
for(int j=0;j<l-1;j++)
{
if(c[j]>c[j+1])
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
}
}
Mar 25, 2014 3:13:15 PM
Class Alpha (continued) 3/3
String a="";
for(int i=0;i<l;i++)
{
a=a+c[i];
}
Rstr=a;
/*
* giving the data member Rstr the value of the arranged string a
*/
}
void disp()
{
System.out.println("Original sentence\t" + Str);
/*
* displaying original string
*/
System.out.println("arranged string\t" + Rstr);
/*
* displaying arranged string
*/
}
public void main() throws IOException
{
Alpha o1=new Alpha();
/*
* creating object using default
* constructor
*/
o1.readString();
o1.remove_spaces();
o1.arrange();
o1.disp();
/*
* calling member functions using object
*/
} //end of main
} //end of class
Mar 25, 2014 3:13:15 PM

Das könnte Ihnen auch gefallen