The Programming Project

Wednesday, March 13, 2019

ISC COMPUTER SCIENCE PRACTICAL 2016 QUESTION 3 : STRING


Write a program to accept a sentence which may be terminated by either '.', '?', or '!' only. The words may be separated by more than one blank space and are in UPPER CASE.
Perform the following tasks:
(a) Find the number of words beginning and ending with a vowel.
(b) Place the words which begin and end with a vowel at the beginning, following by the remaining words as they occur in the sentence.

Test your program with the sample data and some random data:

Example 1
Input : ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
Output : NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Example 2
Input : YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY
Output : NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY


import java.io.*;
public class ISC2015Q1 {
       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();
               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();
               }
               Vowel obj = new Vowel(Sentence);
               obj.wordCount();
               obj.wordExtractor();
               System.out.println("OUTPUT:");
               obj.vowelEndingWordCount();
               System.out.println();
               obj.display();
               br.close();
       }
}
class Vowel{
      
       public void display() {
             for(int i=0;i<word_counter;i++)
                    if(position_tag[i]==1)
                          System.out.print(word_Storage[i]+" ");
             for(int j=0;j<word_counter;j++)
                    if(position_tag[j]==0)
                          System.out.print(word_Storage[j]+" ");
            
       }
      
       public void vowelEndingWordCount() {
             for(int i=0;i<word_counter;i++) {
                    position_tag[i]=0;
                    flag = false;
                    for(int j=0;j<5;j++)
                          if(word_Storage[i].charAt(0)==vowels[j])
                                 for(int k=0;k<5;k++)
                                        if(word_Storage[i].charAt(word_Storage[i].length()-1)==vowels[k])
                                              flag = true;
                    if(flag==true) {
                          number_beg_end_with_vowel++;
                          position_tag[i]=1;
                          }
             }
             System.out.println("NUMBER OF WORDS BEGINING AND ENDING WITH A VOWEL:"+number_beg_end_with_vowel);  
       }
      
       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];
       position_tag = new int[word_counter];
       }
      
       Vowel (String Sentence) {
             msg = Sentence;
             number_beg_end_with_vowel = 0;
             word_counter = 0;
       }
       private boolean flag;
       private char[] vowels= {'A','E','I','O','U'};
       private int word_counter;
       private int number_beg_end_with_vowel;
       private int[] position_tag;
       private String msg;
       private String[] word_Storage;
}

ISC COMPUTER SCIENCE PRACTICAL 2016 QUESTION 2 : MATRIX


Question:


Write a program to declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:
(a) Sort the non-boundary elements in ascending order using any standard sorting technique and rearrange them in the matrix.
(b) Calculate the sum of both the diagonals.
(c) Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix with their sum.

Example 1

INPUT :M = 4
9 2 1 5 
8 13 8 4 
15 6 3 11 
7 12 23 8 
OUTPUT:
ORIGINAL MATRIX
9 2 1 5 
8 13 8 4 
15 6 3 11 
7 12 23 8 
REARRANGED MATRIX
9 2 1 5 
8 3 6 4 
15 8 13 11 
7 12 23 8 
DIAGONAL ELEMENTS
9   5 
 3 6  
 8 13  
7   8  
SUM OF THE DIAGONAL ELEMENTS = 59





public class ISC2016Q2 {
       public static void main(String[] args) {
             int M;
             Scanner in = new Scanner(System.in);
             System.out.println("Enter the order of the matrix:");
             M = in.nextInt();
             while( M<=3 || M >= 10) {
                    System.out.println("INVALID INPUT,try again:");
                    M = in.nextInt();
             }
             Matrix obj = new Matrix(M);
             System.out.println("Enter the elements of the matrix:");
             obj.inputMatrix();
             System.out.println("ORIGINAL MATRIX:");
             obj.displayMatrix();
             System.out.println("REARRANGED MATRIX:");
             obj.rearangedMatrix();
             obj.displayMatrix();
             System.out.println("DIAGONAL ELEMENTS:");
             obj.displayDiagonalElements();
             in.close();
       }
}
class Matrix{

public void displayDiagonalElements() {
       for(int i=0;i<order;i++) {
             for(int j=0;j<order;j++)
                    if(i==j || i+j == order-1) {
                          System.out.print(Mat[i][j]+" "); sum_of_diagonals += Mat[i][j];}
                    else
                          System.out.print(" ");
             System.out.println();
             }
       System.out.println("SUM OF DIAGONAL ELEMENTS:"+sum_of_diagonals);
       }

public void rearangedMatrix() {
       int counter =0;
       for(int i=1;i<order-1;i++)
             for(int j=1;j<order-1;j++)
                    non_boundary_elements[counter++]=Mat[i][j];
       Arrays.sort(non_boundary_elements);
       counter = 0;
       for(int i=1;i<order-1;i++)
             for(int j=1;j<order-1;j++)
                    Mat[i][j]=non_boundary_elements[counter++];
       }

public void displayMatrix() {
       for(int i=0;i<order;i++) {
             for(int j=0;j<order;j++)
                    System.out.print(Mat[i][j]+" ");
             System.out.println();
             }
       }

public void inputMatrix() {
       Scanner in = new Scanner(System.in);
       for(int i=0;i<order;i++)
             for(int j=0;j<order;j++)
                    Mat[i][j]=in.nextInt();
       }

Matrix(int M){
       order = M;
       Mat = new int[M][M];
       sum_of_diagonals = 0;
       non_boundary_elements = new int[(M-2)*(M-2)];
       }
       private int order;
       private int[][] Mat;
       private int sum_of_diagonals;
       private int[] non_boundary_elements;
      
}