Guide For School logo Guide For SchoolICSE and ISC Resources

Java program to arrange words in Ascending order of their Potential (ISC Specimen 2016 Question 2)

28 September 2015

Java program to input a sentence and arrange the words in it in ascending order of their potential. Solution to Question 2 of ISC Computer Science Practical Specimen Paper 2016.


Question:

The encryption of alphabets are to be done as follows:

A = 1 B = 2 C = 3 . . . Z = 26

The potential of a word is found by adding the encrypted value of the alphabets.

Example: KITE

Potential = 11 + 9 + 20 + 5 = 45

Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order.

Output the result in format given below:

Example 1
Plain
INPUT 		: 	THE SKY IS THE LIMIT.

POTENTIAL 	: 	THE 	= 33
				SKY 	= 55
				IS 		= 28
				THE 	= 33
				LIMIT	= 63

OUTPUT	 	: 	IS THE THE SKY LIMIT
Example 2
Plain
INPUT 		: 	LOOK BEFORE YOU LEAP.

POTENTIAL 	: 	LOOK 	= 53
				BEFORE	= 51
				YOU 	= 61
				LEAP 	= 34

OUTPUT 		:	LEAP BEFORE LOOK YOU

Programming Code:

Java
/**
* The class WordPotential inputs a sentence and arranges the words 
* in ascending order of their potential
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @ISC Computer Science Practical Specimen Paper - Question 2 
*/

import java.util.*;
class WordPotential
{
    int findPotential(String s) // Function to find potential of a word
    {
        s = s.toUpperCase();
        int p = 0, l = s.length();
        char ch;
        for(int i=0; i<l; i++)
        {
            ch = s.charAt(i);
            p = p + (ch-64); // if ch = 'A', then 'A'-64 = ASCII value of 'A' - 64 = 65-64 = 1
        }
        return p;
    }
    
    // Function to sort the words in ascending order of their potential
    void sortPotential(String w[], int p[]) 
    {
        int n = w.length, 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 = w.length;
        String ans = "";
        for(int i=0; i<n; i++)
        {
            ans = ans + " " + w[i];
        }
        ans = ans.trim();
        System.out.println("\nOutput\t\t :  \t"+ans);
    }
    
    public static void main(String args[])
    {
        WordPotential ob = new WordPotential();
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter a sentence : \t");
        String s = sc.nextLine();
        
        StringTokenizer str = new StringTokenizer(s," .,?!");
        int n = str.countTokens();
        
        String words[] = new String[n];
        int potential[] = new int[n];
        
        for(int i=0; i<n; i++)
        {
            words[i] = str.nextToken(); // Saving words one by one in an array
            potential[i] = ob.findPotential(words[i]); // Saving potential of every word
        }
        
        // Printing the words along with their potential
        System.out.print("\nPotential\t : \t");
        for(int i=0; i<n; i++)
        {
            System.out.println(words[i]+"\t= "+potential[i]);
            System.out.print("\t\t\t");
        }
        
        ob.sortPotential(words,potential);
    }
}

Output:

Plain
Enter a sentence : 	Look before you leap.

Potential		 : 	Look	= 53
					before	= 51
					you	= 61
					leap	= 34
			
Output		 :  	leap before Look you


Enter a sentence : 	The sky is the limit.

Potential		 : 	The	= 33
					sky	= 55
					is	= 28
					the	= 33
					limit	= 63
			
Output		 :  	is The the sky limit

Tags and Categories

Class 12ISC Important ProgramsString Related ProgramsISCComputer Applications