The Programming Project

Tuesday, March 19, 2019

ISC COMPUTER SCIENCE PRACTICAL 2020 SOLVED SPECIMEN PAPER QUESTION 2: STRING

NOTE: This question is from a SPECIMEN PAPER released by the council
The potential of a word is found by adding the ASCII value of the alphabets. (ASCII values of A to Z are 65 to 90). Example: BALL Potential = 66 + 65 + 76 + 76 = 283 Write a program to accept a sentence which may be terminated by either “ . ” , “ ? ” or “ ! ” only. The words of sentence are separated by single blank space and are in UPPER CASE. Decode the words according to their potential and arrange them in ascending order of their potential strength. Test your program with the following data and some random data:

Example 1:
INPUT: HOW DO YOU DO?
OUTPUT:
HOW = 238 DO = 147 YOU = 253 DO = 147
DO DO HOW YOU
Example 2:
INPUT: LOOK BEFORE YOU LEAP.
OUTPUT: LOOK = 309 BEFORE = 435 YOU = 253 LEAP = 290
YOU LEAP LOOK BEFORE
Example 3: INPUT: HOW ARE YOU#

OUTPUT: INVALID INPUT


The logic behind the program
1. Input the sentence by checking its validity.
2. Count the number of words in the sentence by counting number of spaces.
3. Extract the words into a String array
4. For each word calculate its potential and store its value in some integer array
5. Sort both the integer array and the integer array in the same loop
6. Print the sorted arrays.

For step 1 the followind code has been used
  public void wordCount() {
       for(int i=0;i<msg.length();i++)
             if(msg.charAt(i)== ' ')
                    word_counter++;
       word_counter +=1;
       word_Storage = new String[word_counter];
       potential_of_word = new int[word_counter];
       }
For step 3 the following code has been used

  public void wordExtractor() {
             int k = 0;
             for(int i=0;i<msg.length();i++) {
                    word_Storage[k]="";
                     while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
                                  word_Storage[k] +=msg.charAt(i);
                                  i++;
                                 }
                    k++;
                     }
For calculatin the potential of the ith word we have used the below code:
for(int j=0;j<word_Storage[i].length();j++)
                       tmp +=word_Storage[i].charAt(j);


For sorting both the arrays  int[] potential_of_word  and String[] word_Storage

for(int k=0;k<word_counter;k++)
                for(int l=k;l<word_counter;l++) {
                       if (potential_of_word[k] > potential_of_word[l]) {
                             tmp = potential_of_word[l]; sword_Storage[l];
                             potential_of_word[l] = potential_of_word[k]; word_Storage[l] = word_Storage[k];
                             potential_of_word[k] = tmpword_Storage[k] = s;
                           }
                }

complete JAVA CODE

import java.io.*;
public class ISC2020Q2 {
       public static void main(String[] argsthrows IOException{
               String Sentence;
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               System.out.println("Enter the sentence:");
               Sentence=br.readLine();
               Sentence = Sentence.toUpperCase();
               while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '!' ) {
                System.out.println("INVALID INPUT:,TRY AGAIN");
                Sentence=br.readLine();
               }
               Potential obj = new Potential(Sentence);
               obj.wordCount();
               obj.wordExtractor();
               System.out.println("OUTPUT:");
               obj.countAndDisplayPotential();
               br.close();
       }
}
class Potential{
       public void countAndDisplayPotential() {
          int tmp;
          String s;
          for(int i=0;i<word_counter;i++) {
                tmp = 0;
                for(int j=0;j<word_Storage[i].length();j++)
                       tmp +=word_Storage[i].charAt(j);
                potential_of_word[i]=tmp;
                System.out.println(word_Storage[i]+"="+potential_of_word[i]);
          }
          for(int k=0;k<word_counter;k++)
                for(int l=k;l<word_counter;l++) {
                       if (potential_of_word[k] > potential_of_word[l]) {
                             tmp = potential_of_word[l]; s= word_Storage[l];
                             potential_of_word[l] = potential_of_word[k]; word_Storage[l] = word_Storage[k];
                             potential_of_word[k] = tmp; word_Storage[k] = s;
                           }
                }
          System.out.println();
          for(int m=0;m<word_counter;m++)
                System.out.print(word_Storage[m]+" ");
       }
         
      
       public void wordExtractor() {
             int k = 0;
             for(int i=0;i<msg.length();i++) {
                    word_Storage[k]="";
                     while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
                                  word_Storage[k] +=msg.charAt(i);
                                  i++;
                                 }
                    k++;
                     }
       }
       public void wordCount() {
       for(int i=0;i<msg.length();i++)
             if(msg.charAt(i)== ' ')
                    word_counter++;
       word_counter +=1;
       word_Storage = new String[word_counter];
       potential_of_word = new int[word_counter];
       }
    
       Potential (String Sentence) {
             msg = Sentence;
             word_counter = 0;
       }
       private int[] potential_of_word;
       private int word_counter;
       private String msg;
       private String[] word_Storage;
}




import java.io.*;
public class ISC2020Q2 {
       public static void main(String[] args) throws IOException{
               String Sentence;
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               System.out.println("Enter the sentence:");
               Sentence=br.readLine();
               Sentence = Sentence.toUpperCase();
               while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '!' ) {
                System.out.println("INVALID INPUT:,TRY AGAIN");
                Sentence=br.readLine();
               }
               Potential obj = new Potential(Sentence);
               obj.wordCount();
               obj.wordExtractor();
               System.out.println("OUTPUT:");
               obj.countAndDisplayPotential();
               br.close();
       }
}
class Potential{
       public void countAndDisplayPotential() {
          int tmp;
          String s;
          for(int i=0;i<word_counter;i++) {
                tmp = 0;
                for(int j=0;j<word_Storage[i].length();j++)
                       tmp +=word_Storage[i].charAt(j);
                potential_of_word[i]=tmp;
                System.out.println(word_Storage[i]+"="+potential_of_word[i]);
          }
          for(int k=0;k<word_counter;k++)
                for(int l=k;l<word_counter;l++) {
                       if (potential_of_word[k] > potential_of_word[l]) {
                             tmp = potential_of_word[l]; s= word_Storage[l];
                             potential_of_word[l] = potential_of_word[k]; word_Storage[l] = word_Storage[k];
                             potential_of_word[k] = tmp; word_Storage[k] = s;
                           }
                }
          System.out.println();
          for(int m=0;m<word_counter;m++)
                System.out.print(word_Storage[m]+" ");
       }
         
      
       public void wordExtractor() {
             int k = 0;
             for(int i=0;i<msg.length();i++) {
                    word_Storage[k]="";
                     while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
                                  word_Storage[k] +=msg.charAt(i);
                                  i++;
                                 }
                    k++;
                     }
       }
       public void wordCount() {
       for(int i=0;i<msg.length();i++)
             if(msg.charAt(i)== ' ')
                    word_counter++;
       word_counter +=1;
       word_Storage = new String[word_counter];
       potential_of_word = new int[word_counter];
       }
    
       Potential (String Sentence) {
             msg = Sentence;
             word_counter = 0;
       }
       private int[] potential_of_word;
       private int word_counter;
       private String msg;
       private String[] word_Storage;
}


ISC COMPUTER SCIENCE PRACTICAL SPECIMEN PAPER SOLVED 2020 QUESTION 1 : EVIL NUMBER


NOTE: This question is from a SPECIMEN PAPER released by the council

An Evil number is a positive whole number which has even number of 1’s in its binary equivalent. Example: Binary equivalent of 9 is 1001, which contains even number of 1’s. Thus, 9 is an Evil Number. A few Evil numbers are 3, 5, 6, 9.... Design a program to accept a positive whole number ‘N’ where N>2 and N<100. > <100. Find the binary equivalent of the number and count the number of 1s in it and display whether it is an Evil number or not with an appropriate message. Test your program with the following data and some random data:


Example 1:
 INPUT: N = 15
BINARY EQUIVALENT: 1111
NUMBER OF 1’s: 4
OUTPUT: EVIL NUMBER
 Example 2:
 INPUT: N = 26
BINARY EQUIVALENT: 11010
NUMBER OF 1’s: 3
OUTPUT: NOT AN EVIL NUMBER
Example 3:
INPUT: N = 145
OUTPUT: NUMBER OUT OF RANGE



This particualr problem is quite simple. Going through the code will reveal the logic!



import java.util.*;
public class ISC2020Q1 {
       public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       do {
             System.out.println("Enter the number:");
             N = in.nextInt();
             if(N <=2 || N>=100)
                    System.out.println("NUMBER OUT OF RANGE,TRY AGAIN");
             }while(N <=2 || N>=100);
       EvilNumber obj = new EvilNumber(N);
       obj.dsiplay();
       in.close();
       }
}
class EvilNumber {
       public void dsiplay() {
             testEvilNumber();
             System.out.println("INPUT NUMBER N:"+number);
             System.out.println("BINARY EQUIVALENT:");
             for(int i=1;i<=counter;i++)
                    System.out.print(binary[counter-i]+" ");
             System.out.println();
             System.out.println("NUMBER OF 1's:"+evil_counter);
             if (flag==true)
                    System.out.print("EVIL NUMBER");
             else
                    System.out.print("NOT AN EVIL NUMBER");
       }
       private void testEvilNumber() {
             int tmp=number;
             do {
                    binary[counter] = tmp%2;
                    tmp /=2;
                    counter++;
                    }while(tmp >=1);
             for(int i=0;i<counter;i++)
                    if(binary[i]==1)
                          evil_counter++;
            
             if(evil_counter%2==0)
                    flag=true;
       }
       EvilNumber(int N) {
             number = N;
             binary = new int[7]; // a binary sequence of length 7 ranges from 1 to 127
             counter = 0;
             evil_counter=0;
             flag = false;
       }
       private boolean flag;
       private int number;
       private int[] binary;
       private int counter;
       private int evil_counter;
}