PRASHNIKAप्रश्निका
Back to the paper

A class Palprime has been defined to display Palprime words from a sentence. Palprime is a…

Computer Science202510 marksProgram
A class Palprime has been defined to display Palprime words from a sentence. Palprime is a palindrome word and its length is a prime number. Example: Input: MY MOM POSSESSES A GOOD LEVEL OF KNOWLEDGE IN MALAYALAM Output: MOM, LEVEL The details of the members of the class are given below: Class name : Palprime Data member/instance variable: line : to store a sentence Methods/Member functions: Palprime( ) : constructor to initialise data member void input( ) : to accept the sentence in UPPER CASE boolean isPalin(String n) : to check whether the string n is a palindrome word and return true, otherwise return false boolean primelen(String n) : to find the length of the string and to check for prime and return true if the length is a prime number otherwise return false void display( ) : to display the palprime word(s) from the sentence by invoking isPalin() and primelen() Specify the class Palprime giving details of the constructor( ), void input( ), boolean isPalin(String), boolean primelen(String) and void display( ). Define a main( ) function to create an object and call the member functions accordingly to enable the task.

Answer

Answer

AI
import java.util.Scanner;
import java.util.StringTokenizer;

class Palprime
{
    String line;

    Palprime()
    {
        line = "";
    }

    void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        line = sc.nextLine().toUpperCase();
    }

    boolean isPalin(String n)
    {
        int i = 0, j = n.length() - 1;
        while (i < j)
        {
            if (n.charAt(i) != n.charAt(j))
                return false;
            i++;
            j--;
        }
        return true;
    }

    boolean primelen(String n)
    {
        int len = n.length();
        if (len < 2)
            return false;
        for (int i = 2; i <= len / 2; i++)
        {
            if (len % i == 0)
                return false;
        }
        return true;
    }

    void display()
    {
        StringTokenizer st = new StringTokenizer(line, " ");
        String result = "";
        while (st.hasMoreTokens())
        {
            String word = st.nextToken();
            if (isPalin(word) && primelen(word))
            {
                if (result.length() > 0)
                    result += ", ";
                result += word;
            }
        }
        System.out.println("Palprime words: " + result);
    }

    public static void main(String args[])
    {
        Palprime ob = new Palprime();
        ob.input();
        ob.display();
    }
}
Explanation: isPalin() checks a word for being a palindrome by comparing characters from both ends inward. primelen() checks whether the word's length is a prime number. display() tokenizes the sentence into words and prints every word for which both isPalin() and primelen() return true. Tested with 'MY MOM POSSESSES A GOOD LEVEL OF KNOWLEDGE IN MALAYALAM', the program correctly outputs 'MOM, LEVEL', matching the example in the question.
Objects

From ISC 2025 Improvement Computer Science Paper 1, question 8.