The Programming Project

Monday, August 24, 2020

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS -2019 Question 7

 


ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS -2019 Question 7            

Design a class ArmNum to check if a given number is an Armstrong number or not.           

[A number is said to be Armstrong if sum of its digits raised to the power of length of the             

number is equal to the number]            

Example       : 371   = 3^3 + 7^3 + 1^3

                 1634  = 1^44 + 6^4 + 3^4 + 4^4  

              54748 = 5^5 + 4^5 + 7^5 + 4^5 + 8^5       

Thus 371, 1634 and 54748 are all examples of Armstrong numbers.       

Some of the members of the class are given below:       

Class name : ArmNum  

Data members/instance variables:         n : to store the number     

                                                                     l : to store the length of the number         

Methods/Member functions:

ArmNum (int nn):                                       parameterized constructor to initialize the data    

                                                                    member n=nn  

                                                                     

int sum_pow(int i):                             returns the sum of each digit raised to the      

                                                                     power of the length of the number using  

                                                                     recursive technique  

                                                                     eg. 34 will return 3^2 + 4^2 (as the length of the     

                                                                     number is 2)  

                                                                    

void isArmstrong( ):                                   checks whether the given number is an     

                                                                     Armstrong number by invoking the function       

                                                                     sum_pow( ) and displays the result with an        

                                                                     appropriate message  

Specify the class ArmNum giving details of the constructor( ), int sum_pow(int) and void isArmstrong( ).

Define a main( ) function to create an object and call the functions accordingly to enable the task.         

 


 

import java.util.*;

public class ISC2019Q7 {

       public static void main(String[] args) {

          int m,n;

          Scanner in = new Scanner(System.in);

          do {

                 System.out.println("INPUT: Enter the value n:");

                 n= in.nextInt();

                 if(n <= 0)

                            System.out.println("Wrong input, please try again:");

          }while (n <= 0);    

          ArmNum obj = new ArmNum(n);

          obj.isArmstrong();

          in.close();

      }

}

 

class ArmNum {

      

              public void isArmstrong() {

                     if (nn == sum_pow(nn))

                           System.out.println(nn+" is an Armstrong Number");

                     else

                           System.out.println(nn+" is not an Armstrong Number");

                    

              }

             

              public int sum_pow(int i) {

                     if ( i <= 0)

                           return sum_power;

                     else

                           return (sum_power+(int)Math.pow(i%10, l)+sum_pow(i/10)); // using recursion

              }

             

              private void lengthofNumber() { // calculates the length of the number

                     int temp=nn;

                     do {

                           temp /=10;

                           l++;

                     }while(temp > 0);

              }

             

              ArmNum( int nn){

                     this.nn = nn;

                     l=0;

                     sum_power= 0;

                     lengthofNumber();

              }

             

              private int nn;

              private int l;

              private int sum_power;

}

      

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2020 QUESTION 2: STRING SORT

 Question 3:

Write a program to accept a sentence which may be terminated by either. '.', '?' or '!' only. The words are to be separated by a single blank space and are in UPPER CASE.

 

Perform the following tasks:

 

Check for the validity of the accepted sentence only for the terminating character.

Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically.

Display the original sentence along with the converted sentence.

Example 1:

Input:AS YOU SOW SO SHALL YOU REAP.

Output:

AS YOU SOW SO SHALL YOU REAP.              

AS SO SOW YOU YOU REAP SHALL

 

Example 2:

Input:SELF HELP IS THE BEST HELP.

Output:

SELF HELP IS THE BEST HELP.             

IS THE BEST HELP HELP SELF.

 

Example 3:

Input:BE KIND TO OTHERS.

Output:

BE KIND TO OTHERS.             

BE TO KIND OTHERS.

 

Example 4:

Input:NOTHING IS IMPOSSIBLE#

Output:

INVALID INPUT


 

 

import java.io.*;

public class ISC2020Q3{

       public static void main(String[] args) throws IOException{

               String Sentence;

               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

               do {

               System.out.println("Enter the sentence:");

               Sentence=br.readLine();

               if( Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '!')

                  System.out.println("INVALID INPUT:,TRY AGAIN");

               } while(Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '!');

               SenetenceSort obj = new SenetenceSort(Sentence);

               obj.wordExtractor();

               obj.sortWords();

               System.out.println("OUTPUT:");

               obj.display();

               br.close();

       }

}

class SenetenceSort{

       public void display() {

              for(int i = 0;i<word_counter;i++) {

                     if(i==word_counter-1)

                           System.out.print(word_Storage[i]+msg.charAt(msg.length()-1));

                     else

                           System.out.print(word_Storage[i]+" ");

                     }

       }

       public void sortWords() {

              String temp;

              int i,j;

              for(i = 0;i<word_counter-1; i++)     

                       for (j = 0; j < word_counter-i-1; j++)

                         if (word_Storage[j].length() >= word_Storage[j+1].length()) { // sorting length wise

                              temp=word_Storage[j+1];

                            word_Storage[j+1]=word_Storage[j];

                            word_Storage[j]=temp;

                            }

              for(i = 0;i<word_counter-1; i++)     

                       for (j = 0; j < word_counter-i-1; j++)

                             if(word_Storage[j].length() == word_Storage[j+1].length()) { // if lengths are equal sort alphabetically

                         if(word_Storage[j].compareTo(word_Storage[j+1])>0) {

                            temp=word_Storage[j+1];

                                     word_Storage[j+1]=word_Storage[j];

                                     word_Storage[j]=temp;

                            }

                                  }

              }

      

       public void wordExtractor() {

              int i;

              //counting the number of words

              for(i=0;i<msg.length();i++)

                if(msg.charAt(i)== ' ')

                       word_counter++;

              word_counter +=1; // In any sentence, number of words is 1 greater than the number of spaces

              word_Storage = new String[word_counter];

              int k = 0;

              //extracting words

              for(i=0;i<msg.length();i++) {

                word_Storage[k]="";

                while(msg.charAt(i) != ' ' &&  i < msg.length()-1) { 

                             word_Storage[k] +=msg.charAt(i);

                             i++;

                            }

                k++;

                       } // end of i-loop

                     }

     

      

       SenetenceSort(String Sentence){

              msg=Sentence;

              word_counter=0;

       }

       private String msg;

       private int word_counter;

       private String[] word_Storage;

}