0% found this document useful (0 votes)
18 views3 pages

ISC Class 11 Java Practical Guide

The document contains a practical file for Class 11 ISC Computer Science, submitted by Yash Raj. It includes two Java programs: one to count the number of vowels in a string and another to check if a string is a palindrome, along with their respective outputs. Each program is accompanied by a description of its functionality and sample input/output results.

Uploaded by

rkgrhryr16
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

ISC Class 11 Java Practical Guide

The document contains a practical file for Class 11 ISC Computer Science, submitted by Yash Raj. It includes two Java programs: one to count the number of vowels in a string and another to check if a string is a palindrome, along with their respective outputs. Each program is accompanied by a description of its functionality and sample input/output results.

Uploaded by

rkgrhryr16
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class 11 ISC Computer Practical File

Submitted By: Yash Raj


Subject: Computer Science (Java)

Programs with their respective questions and outputs


Question 1: Write a program using a user-defined method countVowels(String s) that returns
the number of vowels in a given string.
Program:
import [Link];
public class CountVowels
{
static int countVowels(String s)
{
int count = 0;
s = [Link]();
for (int i = 0; i < [Link](); i++)
{
char ch = [Link](i);
if ("aeiou".indexOf(ch) != -1)
count++;
}
return count;
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int result = countVowels(str);
[Link]("Number of vowels = " + result);
}
}

Output:
Enter a string: India
Number of vowels = 3
Question 2: Define a method isPalindrome(String s) that returns true if the string is a
palindrome, otherwise false.
Program:
import [Link];
public class PalindromeString
{
static boolean isPalindrome(String s)
{
s = [Link]();
int i = 0, j = [Link]() - 1;
while (i < j)
{
if ([Link](i) != [Link](j))
return false;
i++;
j--;
}
return true;
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (isPalindrome(str))
[Link]("Palindrome String");
else
[Link]("Not a Palindrome String");
}
}

Output:
Enter a string: level
Palindrome String

Common questions

Powered by AI

Static methods are used in these programs to allow the methods ('countVowels' and 'isPalindrome') to be called directly on the class without needing to instantiate an object. This simplifies the code when the methods do not need access to instance variables, reflecting a functional style suitable for utility operations. Advantages include ease of access and reduced memory overhead. A disadvantage is that static methods cannot access instance variables or methods, limiting their use in more complex object-oriented programming tasks .

Enhancing the 'countVowels' program for non-English languages would involve expanding the set of characters recognized as vowels to include accented characters or vowel equivalents in those languages. This could be implemented by maintaining a comprehensive list of vowels or dynamic language-specific inclusion of characters using configuration files or databases that adjust based on user language settings. Additionally, providing multi-language support for input prompts and outputs would improve usability .

The 'main' method in both programs acts as a simple interface to invoke user-defined methods after obtaining input from the user. A generalized strategy for integrating multiple methods would involve defining a structured interface in 'main' that reads input, processes it appropriately, and methodically calls each user-defined method in sequence or as needed by the application logic. This may include modularizing input handling and output display for reusability and maintaining organizational clarity .

The main methods in both programs use the 'Scanner' class to read user input, which may pose a security risk if the input is directly utilized in environments where shell commands or SQL are processed, opening up potential for injection attacks. However, within these programs, the input is only used locally for string processing, posing minimal risk. It underscores the importance of validating and sanitizing user inputs in broader applications, especially when dealing with database operations or command executions .

The 'isPalindrome' method converts the string to lowercase and uses two pointers to traverse the string from the beginning and end, moving towards the center. If characters at these pointers are not equal, it returns false; otherwise, it continues until the pointers meet or cross, indicating a palindrome. To optimize, one could avoid converting the whole string to lowercase initially and instead compare lowercase versions of characters during each comparison. This would save unnecessary conversions for strings found to be non-palindromes early in the process .

The 'isPalindrome' method might return an incorrect result if the input contains non-alphabetic characters or spaces, which it currently treats as significant. For example, the string 'A man, a plan, a canal, Panama!' is a palindrome but will return false with the current method. To rectify this, the method could be modified to ignore non-alphanumeric characters and spaces, possibly by preprocessing the string to remove these elements before palindrome checking .

The choice between a 'for' loop in 'countVowels' and a 'while' loop in 'isPalindrome' is more contextual than performance-driven. A 'for' loop is typically used for fixed-range iterations, as in 'countVowels', making it concise when the start and end conditions are well known. 'While' loops, as used in 'isPalindrome', are suited for conditions where iteration depends on a dynamic state, such as matching characters. Performance differences are negligible for small data sizes, but semantic clarity is enhanced by choosing the loop type that best fits iteration logic .

The 'countVowels' method could be extended to handle accented vowels by including them in the string checked by 'indexOf'. For example, one could append 'áéíóú' and similar accented characters to the string containing vowels. To accommodate other alphabets, similar logic could be applied: including vowels from those alphabets in the matching string or using Unicode ranges for dynamic inclusion .

The 'countVowels' method iterates through each character in the input string after converting it to lowercase, checking if the character is one of the lowercase vowels ('a', 'e', 'i', 'o', 'u') by using the 'indexOf' method on a string containing these vowels. If a character is a vowel, it increments a counter. A limitation of this method is that it does not account for accented vowels or any special characters that might be considered vowels in other languages .

Converting the input string to lowercase standardizes the comparison process, ensuring that the case of the characters does not affect the outcome. In 'countVowels', this avoids missing vowels due to case differences, and in 'isPalindrome', it ensures character comparisons are case-insensitive, enabling accurate palindrome checking regardless of uppercase or lowercase character usage in the input .

You might also like