Java program to arrange words in Ascending or
Potential
/**
* The class WordPotential inputs a sentence and arranges the words
* in ascending order of their potential
* @author : [Link]
* @Program Type : BlueJ Program - Java
* @ISC Computer Science Practical Specimen Paper - Question 2
*/
import [Link].*;
class WordPotential
{
int findPotential(String s) // Function to find potential of a word
{
s = [Link]();
int p = 0, l = [Link]();
char ch;
for(int i=0; i<l; i++)
{
ch = [Link](i);
p = p + (ch-64); // if ch = 'A', then 'A'-64 = ASCII value of 'A' - 64 = 65-64
}
return p;
}
// Function to sort the words in ascending order of their potential
void sortPotential(String w[], int p[])
{
int n = [Link], t1 = 0;
String t2 = "";
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(p[i]>p[j])
{
t1 = p[i];
p[i] = p[j];
p[j] = t1;
t2 = w[i];
w[i] = w[j];
w[j] = t2;
}
}
}
printResult(w,p);
void printResult(String w[], int p[]) // Function to print the final result
{
int n = [Link];
String ans = "";
for(int i=0; i<n; i++)
{
ans = ans + " " + w[i];
}
ans = [Link]();
[Link]("\nOutput\t\t : \t"+ans);
}
public static void main(String args[])
{
WordPotential ob = new WordPotential();
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence : \t");
String s = [Link]();
StringTokenizer str = new StringTokenizer(s," .,?!");
int n = [Link]();
String words[] = new String[n];
int potential[] = new int[n];
for(int i=0; i<n; i++)
{
words[i] = [Link](); // Saving words one by one in an array
potential[i] = [Link](words[i]); // Saving potential of every word
}
// Printing the words along with their potential
[Link]("\nPotential\t : \t");
for(int i=0; i<n; i++)
{
[Link](words[i]+"\t= "+potential[i]);
[Link]("\t\t\t");
}
[Link](words,potential);
}