Class 11 Computer Science Practical Mid Term 2019

Question 1: A unique digit integer is a positive integer (without leading zeroes) with no duplicate digits. For example, 7, 135, 214 are all unique digit integers, whereas 33, 3121, 300 are not.

Given two positive integers m and n, write a program to determine how many unique digit integers are there in the range between m and n (both inclusive) and output them.

The input contains two positive integers m and n. Assume m < 30000 and n < 30000.

You are to output the number of unique digit integers in the specified range along with their values in the format specified below:

SAMPLE DATA:
INPUT:
m = 100
n = 120
OUTPUT:
THE UNIQUE DIGIT INTEGERS ARE:
102, 103, 104, 105, 106, 107, 108, 109, 120.
FREQUENCY OF UNIQUE DIGIT INTEGERS IS: 9

INPUT:
m = 2500
n = 2550
OUTPUT:
THE UNIQUE DIGIT INTEGERS ARE:
2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2519, 2530, 2531, 2534, 2536, 2537, 2538, 2539, 2540, 2541, 2543, 2546, 2547, 2548, 2549.
FREQUENCY OF UNIQUE DIGIT INTEGERS IS: 28

import java.io.*;
class Unique{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("m = ");
        int m = Math.abs(Integer.parseInt(br.readLine()));
        System.out.print("n = ");
        int n = Math.abs(Integer.parseInt(br.readLine()));
        if(m >= 30000 || n >= 30000 || m > n){
            System.out.println("INVALID INPUT");
            return;
        }
        int count = 0;
        for(int i = m; i <= n; i++){ if(isUnique(i)){ if(count > 0)
                    System.out.print(", " + i);
                else
                    System.out.print(i);
                count++;
            }
        }
        if(count > 0)
            System.out.println(".");
        System.out.println("FREQUENCY OF UNIQUE DIGIT INTEGERS IS: " + count);
    }
    public static boolean isUnique(int num){
        int a[] = new int[10];
        boolean status = true;
        while(num > 0){
            int d = num % 10;
            a[d] += 1;
            if(a[d] > 1){
                status = false;
                break;
            }
            num /= 10;
        }
        return status;
    }
}

Question 2: Encryption is a technique of coding messages to maintain their secrecy. A string array of size ‘n’ where 1 < n < 10, stores single sentences (each sentence ends with a full stop) in each row of the array.

Write a program to accept the size of the array. Display an appropriate message if the size is not satisfying the given condition. Define a string array of the accepted size and fill it with sentences row-wise. Change the sentence of the odd rows with an encryption of two characters ahead of the original characters. Also change the sentence of the even rows by storing the sentence in reverse order of their words. Display the encrypted sentences as per the sample data given below.

Test your program on the sample data and some random data.

SAMPLE DATA:
INPUT:
n = 4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.

INPUT:
n = 13
OUTPUT:
INVALID INPUT

import java.io.*;
import java.util.StringTokenizer;
class Encryption{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("n = ");
        int n = Integer.parseInt(br.readLine());
        if(n <= 1 || n >= 10){
            System.out.println("INVALID INPUT");
            return;
        }
        String a[] = new String[n];
        System.out.println("Enter sentences:");
        for(int i = 0; i < a.length; i++){
            a[i] = br.readLine().toUpperCase();
            a[i] = a[i].trim();
            if(a[i].charAt(a[i].length() - 1) != '.'){
                System.out.println("INVALID INPUT");
                return;
            }
            if((i + 1) % 2 == 1)
                a[i] = encode(a[i]);
            else
                a[i] = reverse(a[i]);
        }
        for(int i = 0; i < a.length; i++)
            System.out.println(a[i]);
    }
    public static String encode(String s){
        String t = "";
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(Character.isLetter(ch)){
                if(ch == 'Y')
                    t += 'A';
                else if(ch == 'Z')
                    t += 'B';
                else
                    t += (char)(ch + 2);
            }
            else
                t += ch;
        }
        return t;
    }
    public static String reverse(String s){
        String t = "";
        StringTokenizer st = new StringTokenizer(s, " .");
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            String w = st.nextToken();
            t = w + " " + t;
        }
        t = t.trim();
        return t + ".";
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: