Practical No.
18:
Write a java file handling program to count and display the number of palindromes present in a text file
"[Link]". Example: If the file "[Link]" contains the following lines, My name is NITIN Hello aaa and
bbb word How are You ARORA is my friend Output will be => 4
Source Code:
import [Link].*;
import [Link].*;
class PalindromeCounter {
public static void main(String[] args) throws Exception {
int palindromeCount = 0;
Scanner sc = new Scanner(new File("[Link]"));
while([Link]()){
String s = [Link]();
if(isPalindrome(s)){
[Link](s);
palindromeCount++;
[Link]("number of palindromes present are: " + palindromeCount);
static boolean isPalindrome(String word) {
if (word == null || [Link]()) {
return false;
String w = [Link]();
int left = 0;
int right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right)) {
return false;
Name: Ananya Dobhal Roll no.: 12 Section: A2 33
left++;
right--;
return true;
OUTPUT
Name: Ananya Dobhal Roll no.: 12 Section: A2 34
Practical No. 19:
Write a program MultiThreads that creates two threadsone thread with the name CSthread and the other thread
named ITthread. Each thread should display its respective name and execute after a gap of PCS-408 (Java
Programming Lab) Page 16 500 milliseconds. Each thread should also display a number indicating the number
of times it got a chance to execute.
Source Code:
import [Link].*;
import [Link].*;
class PalindromeCounter {
public static void main(String[] args) throws Exception {
int palindromeCount = 0;
Scanner sc = new Scanner(new File("[Link]"));
while([Link]()){
String s = [Link]();
if(isPalindrome(s)){
[Link](s);
palindromeCount++;
[Link]("number of palindromes present are: " + palindromeCount);
static boolean isPalindrome(String word) {
if (word == null || [Link]()) {
return false;
String w = [Link]();
int left = 0;
int right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right)) {
return false;
}
Name: Ananya Dobhal Roll no.: 12 Section: A2 35
left++;
right--;
return true;
OUTPUT
Name: Ananya Dobhal Roll no.: 12 Section: A2 36
Practical No. 20:
Write a java program for to solve producer consumer problem in which a producer produce a value and
consumer consume the value before producer generate the next value.
Source Code:
import [Link].*;
class Pc1 {
LinkedList <Integer> list = new LinkedList<>();
int capacity = 2;
public void produce() throws Exception{
int val=0;
while(true){
synchronized(this){
while([Link]() == capacity){
wait(); }
[Link]("Producer is going to produce:" + val);
[Link](val++);
notify();
[Link](500);
}}}
public void consume() throws Exception{
while(true){
synchronized(this){
while([Link]() == 0){
wait(); }
[Link]("Consumer is going to consume" + [Link]());
notify();
[Link](500);
}}}}
class A extends Thread{
Pc1 p;
public A(Pc1 p){
Name: Ananya Dobhal Roll no.: 12 Section: A2 37
this.p = p;
public void run(){
try{
[Link]();
catch(Exception e){
[Link](e);
}}}
class B extends Thread{
Pc1 p;
public B(Pc1 p){
this.p = p;
public void run(){
try{
[Link]();
catch(Exception e){
[Link](e);
}}}
public class ProducerConsumer{
public static void main(String[] args) {
Pc1 p = new Pc1();
A t1 = new A(p);
B t2 = new B(p);
[Link]();
[Link]();
Name: Ananya Dobhal Roll no.: 12 Section: A2 38
OUTPUT
Name: Ananya Dobhal Roll no.: 12 Section: A2 39
Practical No. 21:
Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of
the strings of even length from the list. (Use ArrayList)
Source Code:
import [Link].*;
public class RemoveEven {
public void removeEvenLength(ArrayList<String> list) {
Iterator<String> it = [Link]();
while ([Link]()) {
String str = [Link]();
if ([Link]() % 2 == 0) {
[Link]();
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
ArrayList<String> list = new ArrayList<String>();
[Link]("Enter number of Elements: ");
int n=[Link]();
[Link]();
for(int i=0;i<n;i++){
[Link]("Enter element: ");
String str=[Link]();
[Link](str);
RemoveEven obj = new RemoveEven();
[Link](list);
[Link]("Filtered list: " + list);
Name: Ananya Dobhal Roll no.: 12 Section: A2 40
}
OUTPUT
Name: Ananya Dobhal Roll no.: 12 Section: A2 41
Practical No. 22:
Write a method swapPairs that switches the order of values in an ArrayList of Strings in a pairwise fashion.
Your method should switch the order of the first two values, then switch the order of the next two, switch the
order of the next two, and so on.
Source Code:
import [Link];
import [Link];
public class swappair {
public void swap(ArrayList<String> list){
for(int i=0;i<[Link]()-1;i+=2){
String str=[Link](i);
[Link](i,[Link](i+1));
[Link](i+1,str);
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
ArrayList<String> list = new ArrayList<String>();
[Link]("Enter number of Elements: ");
int n=[Link]();
[Link]();
for(int i=0;i<n;i++){
[Link]("Enter element: ");
String str=[Link]();
[Link](str);
[Link]("Original List: "+list);
swappair obj=new swappair();
[Link](list);
[Link]("Swapped List: "+list);
}}
Name: Ananya Dobhal Roll no.: 12 Section: A2 42
OUTPUT
Name: Ananya Dobhal Roll no.: 12 Section: A2 43
Practical No. 23:
Write a method called alternate that accepts two Lists of integers as its parameters and returns a new List
containing alternating elements from the two lists, in the following order:
• First element from first list
• First element from second list
• Second element from first list
• Second element from second list
• Third element from first list
• Third element from second list
Source Code:
import [Link].*;
public class duallist {
LinkedList<Integer> list3=new LinkedList<Integer>();
public void alternate(LinkedList<Integer> list1,LinkedList<Integer> list2){
int m=[Link](),i=0;
int n=[Link](),j=0;
while(i<m && j<n){
[Link]([Link](i++));
[Link]([Link](j++));
while(i<m){
[Link]([Link](i++));
while(j<n){
[Link]([Link](j++));
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int m=0;
LinkedList<Integer> list1=new LinkedList<Integer>();
Name: Ananya Dobhal Roll no.: 12 Section: A2 44
[Link]("Enter number of Elements in list1: ");
m=[Link]();
[Link]("Enter Elements in list1: ");
for(int i=0;i<m;i++){
[Link]([Link]());
int n=0;
LinkedList<Integer> list2=new LinkedList<Integer>();
[Link]("Enter number of Elements in list2: ");
n=[Link]();
[Link]("Enter Elements in list2: ");
for(int i=0;i<n;i++){
[Link]([Link]());
duallist obj=new duallist();
[Link](list1, list2);
[Link]("List 1: "+list1);
[Link]("List 2: "+list2);
[Link]("List 3: "+obj.list3);
Name: Ananya Dobhal Roll no.: 12 Section: A2 45
OUTPUT
Name: Ananya Dobhal Roll no.: 12 Section: A2 46
Practical No. 24:
Write a GUI program to develop an application that receives a string in one text field, and count number of
vowels in a string and returns it in another text field, when the button named “CountVowel” is clicked. When
the button named “Reset” is clicked it will reset the value of text field one and Text field two. When the button
named “Exit” is clicked it will close the application.
Source Code:
import [Link].*;
import [Link].*;
import [Link].*;
class countvowel extends JFrame implements ActionListener{
JTextField tf1,tf2;
int count=0;
public countvowel(){
JLabel l1=new JLabel("Enter String: ");
tf1=new JTextField(20);
JButton jb1=new JButton("COUNT VOWEL");
JButton jb2=new JButton("RESET");
JButton jb3=new JButton("EXIT");
JLabel l2=new JLabel("Result: ");
tf2=new JTextField(20);
setLayout(new FlowLayout([Link],30,10));
add(l1);
add(tf1);
add(jb1);
add(jb2);
add(jb3);
add(l2);
add(tf2);
[Link](this);
[Link](this);
[Link](this);
Name: Ananya Dobhal Roll no.: 12 Section: A2 47
}
public void actionPerformed(ActionEvent e){
String s=[Link]();
String str=[Link]();
if([Link]("EXIT")){
[Link](0);
else if ([Link]("RESET")) {
count=0;
[Link]("");
[Link]("");
else{
for(int i=0;i<[Link]();i++){
char ch=[Link](i);
ch=[Link](ch);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'){
count++;
[Link]([Link](count));
public static void main(String args[]) {
countvowel d = new countvowel();
[Link](400, 300);
[Link](true);
Name: Ananya Dobhal Roll no.: 12 Section: A2 48
OUTPUT:
Name: Ananya Dobhal Roll no.: 12 Section: A2 49