The Programming Project: THEORY PAPER
Showing posts with label THEORY PAPER. Show all posts
Showing posts with label THEORY PAPER. Show all posts

Friday, January 22, 2021

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS - 2019 Question 11

 Question 11

A linear data structure enables the user to add address from rear end and remove address

from front. Define a class Diary with the following details:

Class name                                                                 : Diary

Data members / instance variables:

Q[ ]                                                                              : array to store the addresses

Size                                                                             : stores the maximum capacity of the

array

start                                                                             : to point the index of the front end

end                                                                              : to point the index of the rear end

Member functions:

Diary (int max)                                                            : constructor to initialize the data

member size=max, start=0 and end=0

void pushadd(String n)                                               : to add address in the diary from the

rear end if possible, otherwise display

the message “ NO SPACE”

String popadd( )                                                          : removes and returns the address from

the front end of the diary if any, else

returns “?????

void show( )                                                                : displays all the addresses in the diary

 

(a) Specify the class Diary giving details of the functions void pushadd(String) and

String popadd( ). Assume that the other functions have been defined.

The main function and algorithm need NOT be written.

 (b) Name the entity used in the above data structure arrangement : Answer QUEUE


In computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence. By convention, the end of the sequence at which elements are added is called the back, tail, or rear of the queue, and the end at which elements are removed is called the head or front of the queue, analogously to the words used when people line up to wait for goods or services.

The operation of adding an element to the rear of the queue is known as enqueue, and the operation of removing an element from the front is known as dequeue. Other operations may also be allowed, often including a peek or front operation that returns the value of the next element to be dequeued without dequeuing it.

import java.util.*;
public class ISC2019Q11 {
       public static void main(String[] args) {
       int max;
       int choice = -1;
       Scanner input = new Scanner(System.in);
       System.out.println("Enter the maximum strength of the diary:");
       max = input.nextInt();
       Diary obj = new Diary(max);
       System.out.println("Enter 0 to exit:");
       System.out.println("Enter 1 to add:");
       System.out.println("Enter 2 to remove:");
       System.out.println("Enter 3 to display:");
       do {
           System.out.println("Enter your choice:");
           choice = input.nextInt();
           switch(choice) {
           case 0:
               break;
           case 1:     
               String n;
               System.out.println("Enter the address to add:");
               n = input.next();
               obj.pushAdd(n);
               break;
           case 2:
               System.out.println(obj.popAdd());
               break;
           case 3:
               obj.show();
               break;
               
           }
                            
       }while(choice !=0);
       input.close();
       }
}
class Diary {
    public void show() {
        for(int i = start; i<end; i++)
            System.out.println("Address at "+(i+1)+" is: "+Q[i]);
    }
    public String popAdd() {
        String frontValue;
        if ( end == start)
            return ("QUEUE IS EMPTY");
        else { 
            frontValue = Q[start];
            for(int i = 0; i < end-1 ;i++)
                Q[i]=Q[i+1];
            end--;
            return (frontValue);
        }
    }
    public void pushAdd(String n) {
        if (end < size) {
            Q[end] = n;
            end++;
        }
        else
            System.out.println("NO SPACE:"); 
    }
    Diary (int max){
        this.size = max;
        this.start = 0;
        this.end = 0;
        Q = new String[this.size];
    }
    private String[] Q;
    private int size;
    private int start;
    private int end;
}

Monday, January 18, 2021

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS - 2019 Question 10

 Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class).

 The class from which the subclass is derived is called a superclass (also a base class or a parent class).Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. 

 In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.A subclass inherits all the members (fields, methods, and nested classes) from its superclass. 

Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS - 2019 Question 10 - SECTION C

A super class Record contains names and marks of the students in two different single

dimensional arrays. Define a sub class Highest to display the names of the students

obtaining the highest mark.

The details of the members of both the classes are given below:

     


Class name : Record

Data member/instance variable:

n[ ]                                                                    : array to store names

m[ ]                                                                  : array to store marks

size                                                                   : to store the number of students

Member functions/methods:

Record(int cap)                                               : parameterized constructor to initialize the data

  member size = cap

void readarray()                                              : to enter elements in both the arrays

void display( )                                                 : displays the array elements

 

Class name: Highest

Data member/instance variable:

index                                                               : to store the index

Member functions/methods:

Highest(…)                                                     : parameterized constructor to initialize the data

  members of both the classes

void find( )                                                      : finds the index of the student obtaining the

                                                                        highest mark and assign it to ‘index

void display( )                                                 : displays the array elements along with the    names and marks of the students who have obtained the highest mark

Assume that the super class Record has been defined. Using the concept of inheritance,

specify the class Highest giving the details of the constructor(…),void find( ) and

void display( ).


import java.util.*;
public class ISC2019Q10 {
       public static void main(String[] args) {
       int cap;
       Scanner input = new Scanner(System.in);
       System.out.println("Enter the strength of students");
       cap = input.nextInt();
       Highest obj = new Highest(cap);
       obj.readArray();
       obj.findHighestMarks();
       obj.display();
       input.close();
       }
}
class Highest extends Record {
    public void display() {
        super.display();
        System.out.println("Highest marks is obatained by "+names[index]+" with "+marks[index]+" as total marks.");
    }
    public void findHighestMarks() {
        // for simplicity I have assumed that there is not tie for the highest marks
        int highest = 0;
        forint i =0; i < size; i++) {
            if( highest < marks[i]) {
                index = i;
                highest = marks[i];
                }
        }
    }
    Highest(int cap){
        super(cap);
        this.index = 0;
        }
    private int index;
    }
class Record {
    protected void display() {
        System.out.println("Entered details are as follows:");
        for(int i = 0; i < size; i++) {
            System.out.println(names[i]+" has obtained "+marks[i]+" marks.");
        }
    }
    public void readArray() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the details of the students:");
        for(int i=0;i<size;i++) {
            if((i+1)%10 == 1) {
                System.out.println("Enter the name of the "+(i+1)+"st student:");
                names[i] = input.next();
                System.out.println("Enter the marks of the "+(i+1)+"st student:");
                marks[i] = input.nextInt();
            }
            else if((i+1)%10==2) {
                System.out.println("Enter the name of the "+(i+1)+"nd student:");
                names[i] = input.next();
                System.out.println("Enter the marks of the "+(i+1)+"nd student:");
                marks[i] = input.nextInt();
            }
            else if((i+1)%10==3) {
                System.out.println("Enter the name of the "+(i+1)+"rd student:");
                names[i] = input.next();
                System.out.println("Enter the marks of the "+(i+1)+"rd student:");
                marks[i] = input.nextInt();
            }
            else {
                System.out.println("Enter the name of the "+(i+1)+"th student:");
                names[i] = input.next();
                System.out.println("Enter the marks of the "+(i+1)+"th student:");
                marks[i] = input.nextInt();
            }
        } 
        input.close();
    }
    Record (int cap) {
        this.size = cap;
        names = new String[size];
        marks = new int[size];
        }
    protected String[] names;
    protected int[] marks;
    protected int size;
}

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS - 2019 Question 9

 



A class Rearrange has been defined to modify a word by bringing all the vowels in the

word at the beginning followed by the consonants.

Example: ORIGINAL becomes OIIARGNL

Some of the members of the class are given below:


Class name                                                     : Rearrange

Data member/instance variable:

wrd                                                                 : to store a word

newwrd                                                          : to store the rearranged word

Member functions/methods:

Rearrange( )                                                    : default constructor

void readword( )                                             : to accept the word in UPPER case

void freq_vow_con( )                                     : finds the frequency of vowels and consonants

                                                                        in the word and displays them with an

                                                                        appropriate message

void arrange( )                                                : rearranges the word by bringing the vowels at

                                                                        the beginning followed by consonants

void display( )                                                 : displays the original word along with the

                                                                        rearranged word

Specify the class Rearrange, giving the details of the constructor( ), void readword( ),

void freq_vow_con( ), void arrange( ) and void display( ). Define the main( ) function to

create an object and call the functions accordingly to enable the task.

import java.util.*;
public class ISC2019Q9 {
       public static void main(String[] args) {
       Rearrange obj = new Rearrange();
       obj.readWord();
       obj.vowelCount();
       obj.rearrangeWord();
       obj.display();
     }
}
class Rearrange {
    public void display() {
        System.out.println("ORIGINAL WORD:"+word);
        System.out.println("REARRANGED WORD:"+word_final);
    }
    public void rearrangeWord() {
        String vowels_beg ="";
        String conso_end ="";
        boolean flag;
        for (int i = 0;i<word.length();i++) {
            flag = false;
            for (int j =0;j<5;j++)
                if(word.charAt(i) == vowels[j]) {
                    flag = true;
                    break;
                }
            if (flag == true)
                vowels_beg += word.charAt(i);
            else
                conso_end +=word.charAt(i);
        }
        word_final = vowels_beg+conso_end;
    }
    public void vowelCount() {
        for (int i = 0;i<word.length();i++) {
            for (int j =0;j<5;j++)
                if(word.charAt(i) == vowels[j])
                    frequency_vowel_count++;
        }
        System.out.println("Number of vowels isn the word "+word+" is:"+frequency_vowel_count);
    }
    public void readWord() {
        Scanner input = new Scanner(System.in);
        boolean flag = true;
        do {
            System.out.println("Enter the word in upper case:");
            word = input.next();
            for(int i = 0;i<word.length();i++) {
                flag = Character.isUpperCase(word.charAt(i));
                if (flag == false ) {
                    System.out.println("You have not entered the word in upper case, try again:");
                    break;
                }
            }
        } while(flag == false);
        input.close();
    }
    Rearrange(){
        this.word = "";
        this.word_final ="";
        this.frequency_vowel_count = 0;
    }
    static char[] vowels = new char[] {'A','E','I','O','U'}; 
    private String word;
    private String word_final;
    private int frequency_vowel_count;
}

Tuesday, August 25, 2020

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS -2019 Question 8

 


import java.util.*;

public class ISC2015Q1 {

       public static void main(String[] args) {

       int M,N;

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

       Scanner in = new Scanner(System.in);

       do {

             System.out.println("ENTER ROW SIZE:");

             M = in.nextInt();

             System.out.println("ENTER COLUMN SIZE:");

             N = in.nextInt();

             if(M<0 || N < 0)

 

                   System.out.println("OUT OF RANGE, TRY AGAIN:");

             }while(M<0 || N < 0);

       MatRev obj = new MatRev(M,N);

       MatRev P = new MatRev(M,N);

       obj.fillarray();

       System.out.println("Entered Matrix");

       obj.show();

       obj.revMat(P);

       System.out.println("Reversed Matrix");

       P.show();

       in.close();

 

 

     }

}

class MatRev {

      

         public void revMat(MatRev P) {

                  for(int p=0;p<M;p++)

              for(int q=0;q<N;q++)

                    P.mat[p][q]= reverse(mat[p][q]); // storing reverse matrix in the current object P

               

         }

         private int reverse (int x) {

                int rev = 0,length;

                length = Integer.toString(x).length()-1;    // calculates the length of the number x

                do {                                                              // if you don't want to use library function     

                       rev += (x%10)*(int)Math.pow(10,length);    // use a do while loop

                       length--;

                       x /=10;

                       }while(x > 0);

                return rev;

         }

      public void show() {

            for(int p=0;p<M;p++)

                   for(int q=0;q<N;q++)

                         if(MAX_MATRIX < mat[p][q])

                                MAX_MATRIX = mat[p][q];

            String s,element;

            s=Integer.toString(MAX_MATRIX);

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

                   for(int j=0;j<N;j++) {

                          element=Integer.toString(mat[i][j]); // for formatted output

                        int tmp=element.length();

                         while(tmp !=s.length()) {

                                       element +=" ";

                                       tmp++;

                                       }

 

                        System.out.print(element+"  ");

                         }              

                 System.out.println();

            }

     }

     public void fillarray() {

            Scanner in = new Scanner(System.in);

            System.out.println("Enter the values of the matrix:");

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

                    System.out.println("Enter the elements of row :"+(i+1));

                   for(int j=0;j<N;j++) {

                         mat[i][j]=in.nextInt();

                   }

            }

            in.close();

      }

 

     MatRev(int M, int N){

            this.M = M;

            this.N = N;

            mat = new int[M][N];

            mat = new int[M][N];

            MAX_MATRIX =0;

            }

 

       private int[][] mat;

       private int M,N;

       private int MAX_MATRIX;    

}