PRACTICAL
ON
JAVA II PROGRAMMING PRACTICAL
(COM 211P)
WRITTEN BY:
EFFIONG, GOODNESS NYAKNO
AKP/ASC/CSC/ND2022/2027
SUBMITED TO:
DR. PRINCE VICTOR OKE
SCOOL OF APPLIED SCIENCE
DEPARTMENT OF COMPUTER SCIENCE
AKWA IBOM STATE POLYTECHNIC
IKOT OSURUA, IKOT EKPENE.
JULY,
2024
PRACTICAL ONE
/*
[Link]
Displays Hello world!!! to the output window
*/
public class HelloWorld{
public static void main( String[] args ){
[Link]( Hello World!!! “ ); // print text
}
PRACTICAL TWO
Write a Java program to check if a vowel is present in a string.
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (checkForVowel(str)) {
[Link]("Vowel is present in the string.");
else {
[Link]("No vowel is present in the string.");
public static boolean checkForVowel(String str) {
for (int i = 0; i < [Link](); i++) {
char c = [Link](i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == E' || c == 'I' || c == 'O' || c == 'U') {
return true;
return false;
PRACTICAL THREE
Write a java program to reverse a string in Java?
public class ReverseString {
public static void main(String[] args) {
String str = "Hello world!";
String reversed = reverseString(str);
[Link]("Original string: " + str);
[Link]("Reversed string: " + reversed);
public static String reverseString(String str) {
StringBuilder sb = new StringBuilder();
for (int i = [Link]() - 1; i >= 0; i--) {
[Link]([Link](i));
return [Link]();
}
PRACTICAL FOUR
How do you swap two numbers without using a third variable in Java?
Using addition and subtraction:
```java
int a = 5;
int b = 10;
a = a + b;
b = a - b;
a = a - b;
[Link]("a = " + a); // Output: 10
[Link]("b = " + b); // Output: 5
PRACTICAL FIVE
Write a Java program to check if a vowel is present in a string
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (checkForVowel(str)) {
[Link]("Vowel is present in the string.");
} else {
[Link]("No vowel is present in the string.");
public static boolean checkForVowel(String str) {
for (int i = 0; i < [Link](); i++) {
char c = [Link](i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == E' || c == 'I' || c == 'O' || c == 'U') {
return true;
return false;
}
PRACTICAL SIX
Write a Java program to check if the given number is a prime number.
public class PrimeNumberChecker {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if(isPrime(num)){
[Link](num + " is a prime number.");
} else {
[Link](num + " is not a prime number.");
public static boolean isPrime(int num) {
if(num <= 1){
return false;
for(int i=2; i<=[Link](num); i++){
if(num % i == 0){
return false;
return true;
}
PRACTICAL SEVEN
Write a Java program to print a Fibonacci sequence using recursion.
public class FibonacciRecursion {
public static void main(String[] args) {
int n = 10;
[Link]("Fibonacci Series up to " + n + " terms:");
for (int i = 0; i < n; i++) {
[Link](fibonacci(i) + " ");
public static int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
PRACTICAL EIGHT
How do you check if a list of integers contains only odd numbers in Java
public class Main {
public static boolean hasOnlyOddNumbers(List<Integer> numbers) {
for (int num : numbers) {
if (num % 2 == 0) {
return false;
return true;
public static void main(String[] args) {
List<Integer> numbers = [Link](1, 3, 5, 7);
boolean result = hasOnlyOddNumbers(numbers);
if (result) {
[Link]("List contains only odd numbers.");
} else {
[Link]("List contains even numbers.");
}
PRACTICAL NINE
Write a simple program to check whether a string is a palindrome in Java?
public class PalindromeCheck {
public static boolean isPalindrome(String str) {
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) {
String input = "madam"; // Change the input string here
boolean result = isPalindrome([Link]()); // Convert to lower case for
case- n insensitive comparison
if (result) {
[Link](input + " is a palindrome.");
} else {
[Link](input + " is not a palindrome.");
}
PRACTICAL TEN
Write a java program to remove spaces from a string in Java?
public class Main {
public static String removeSpaces(String input) {
return [Link]("\\s", "");
public static void main(String[] args) {
String input = "Hello World! This is a test string.";
String output = removeSpaces(input);
[Link]("Input string: " + input);
[Link]("String without spaces: " + output);