Assignment - 2
Exercise-1 : Write a program to count word frequencies in a given text .
import java. util. Scanner;
public class test
{
public static void main(String[] args)
{
Scanner scan= new Scanner([Link]);
String str=[Link]();
String[] s=[Link](" ");
int count=1;
for(int i=0;i<=[Link];i++)
{
for(int j=i+1;j<[Link];j++)
{
if(s[i].equals(s[j]) && s[i]!="-1")
{
s[j]="-1";
count++;
}
}
if(count>1 && s[i]!="-1")
{
[Link](s[i]+" "+count);
s[i]="-1";
}
count=1;
}
}
}
Exercise-2 : Write a program that checks if a given word is a palindrome.
import [Link];
public class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner([Link]);
[Link]("Enter a string:");
str = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
rev = rev + [Link](i);
if ([Link](rev))
[Link](str+" is a palindrome");
else
[Link](str+" is not a palindrome");
}
}
Exercise 3: Create a list of numbers, then write a program that prints the
square of each number in the list .
import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
List<Integer> numbers = new ArrayList<>();
[Link](1);
[Link](2);
[Link](3);
[Link](4);
[Link](5);
List<Integer> squarednumbers = squareNumbers(numbers);
[Link]("Given Numbers : " + numbers);
[Link]("Squared Numbers : " + squarednumbers);
}
public static List<Integer> squareNumbers(List<Integer> numbers)
{
List<Integer> squarednumbers = new ArrayList<>();
[Link](number -> [Link](number * number));
return squarednumbers;
}
}