Advertisements
Advertisements
Question
A class Sort Alpha has been defined to sort the words in the sentence in alphabetical order.
Example: Input: THE SKY IS BLUE
Output: BLUE IS SKY THE
Some of the members of the class are given below:
Class name | Short Alpha |
Data members/instance variables: |
|
sent |
to store a sentence |
n | integer to store the number of words in a sentence |
Methods/Member functions: |
|
ShortAlpha( ) |
default constructor to initialise data members with legal initial values |
void acceptsent( ) | to accept a sentence in UPPERCASE |
void short(SortAlpha P) | sorts the words of the sentence of object P in alphabetical order and stores the sorted sentence in the current object |
void display( ) | display the original sentence along with the sorted sentence by invoking the method sort( ) |
Specify the class Sort Alpha giving details of the constructor (), void acceptsent(), void sort (Sort Alpha) and void display(). Define a main()function to create an object and call the functions accordingly to enable the task.
Solution
import java.util. Scanner;
public class SortAlpha
{
public String sent;
int n;
public SortAlpha()
{
sent="";
n=0;
}
public void acceptsent()
{
Scanner in = new Scanner(System.in);
System.out.println(“Enter a sentence in Uppercase
:");
sent=in.nextLine();
}
public void sort(SortAlpha P)
{
int i=0,j=0;
String str=P.sent+" ";
String word="";
String words[]=new String[20];
while (i<str.length())
{
if (str.charAt(i)!=' ')
word+=str.charAt(i);
else
{words[j++]=word;
word="";
}
i++;
}
int p=j;
for(i= 0; i<p; i++) {
for(j=0;j<(p- 1-i); j++) {
if (words[j].compareToIgnoreCase(words[j + 1]) >0)
{
String temp = words[j + 1];
words[j + 1] = words[j];
words[j] = temp;
}
}
}
str="";
for (i =0; i<p; i++)
str+=words[i]+" ";
P.sent=str;
}
public void display()
{
System.out.println(“Original sentence :"+ sent);
sort(this);
System.out.println(“Sorted sentence :"+ sent);
}
public static void main(String args[])
{
SortAlpha ob=new SortAlpha();
ob.acceptsent();
ob.display();
}
}
APPEARS IN
RELATED QUESTIONS
Write the statement in Java to extract the word “MISS” from the word “SUBMISSION”.
What is the output of the statement given below?
System.out.print("FAN" + ("AUTOMATIC".charAt(5) ) );
A matrix M[- 6, ... 10, 4 ... 15] is stored in the memory with each element requiring 4 bytes of storage. If the base address is 1025, find the address of M [4] [8] when the matrix is stored in Column Major wise.
A matrix M[-6….10, 4…15] is stored in the memory, with each element requiring 4 bytes of storage. If the base address is 1025, find the address of M[4][8] when the matrix is stored in column major-wise.
The following function getIt() is a part of some class. Assume x is a positive integer, f is the lower bound of arr[ ] and l is the upper bound of the arr[ ].
Answer the questions given below along with dry run/working.
public int getIt(int x,intarr[],int f,int l)
{
if(f>l)
return-1;
int m=(f+l)/2;
if(arr[m]<x)
return getIt(x,m+1,l);
else if(arr[m]>x)
return getIt(x,f,m-1);
else
return m;
}
- What will the function getIt( ) return if arr[ ] = {10,20,30,40,50} and x = 40?
-
What is function getIt( ) performing apart from recursion?