Core Java
Core Java
Core Java
}
USERMAINCODE:
public class UserMainCode {
public static int checkSum(int n)
{
int n1;
int sum=0;
int r;
while(n!=0)
{
n1=n%10;
if(n1%2!=0)
{
sum=sum+n1;
}
n=n/10;
}
if(sum%2==0)
{
r=-1;
}
else
{
r=1;
}
return r;
}
2. Number Validation
Write a program to read a string of 10 digit number , check whether the string contains a 10
digit number in the format XXX-XXX-XXXX where 'X' is a digit.
Include a class UserMainCode with a static method validateNumber which accepts a
string
as input .
The return type of the output should be 1 if the string meets the above specified format . In
case the number does not meet the specified format then return -1 as output.
Create a class Main which would get the input as a String of numbers and call the static
methodvalidateNumber present in the UserMainCode.
Input and Output Format:
Input consists of a string.
Output is a string specifying the given string is valid or not .
Refer sample output for formatting specifications.
Sample Input 1:
123-456-7895
Sample Output 1:
Valid number format
Sample Input 2:
-123-12344322
Sample Output 2:
Invalid number format
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
String number=s.next();
int r=UserMainCode.validateNumber(number);
if(r==1)
{
System.out.println("Valid number format“);
}
else
{
System.out.println("Invalid number format");
}
s.close();
}
USERMAINCODE:
import java.util.*;
public class UserMainCode {
public static int validateNumber(String number)
{
int b;
if(number.matches("[0-9]{3}[-]{1}[0-9]{3}[-]{1}[0-9]{4}"))
{
b=1;
}
else
{
b=0;
}
return b;
}
USERMAINCODE:
public class UserMainCode {
public static int sumOfSquaresOfEvenDigits(int n)
{
int n1=0;
int sum=0;
while(n!=0)
{
n1=n%10;
if(n1%2==0)
{
sum+=n1*n1;
}
n=n/10;
}
return sum;
}
USERMAINCODE:
import java.util.*;
public class UserMainCode {
public static String getMiddleChars(String str)
{
StringBuffer sb=new StringBuffer();
if(str.length()%2==0)
{
sb.append(str.substring((str.length()/2)-1,(str.length()/2)+1));
}
return sb.toString();
}
MAIN:
import java.util.*;
public class main {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
String input=s.nextLine();
int r=UserMainCode.checkCharacters(input);
if(r==1)
{
System.out.println("Valid");
}
else
{
System.out.println("Invalid");
}
s.close();
}
USERMAINCODE:
import java.util.*;
public class UserMainCode {
public static int checkCharacters(String input)
{
int r;
StringTokenizer t = new StringTokenizer(input," ");
String s = t.nextToken();
String s1 =s ;
while(t.hasMoreTokens())
{
s1 = t.nextToken();
}
if(s.charAt(0) == s1.charAt(s1.length()-1))
r=1;
else
r=0;
return r;
}
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
int n1=s.nextInt();
System.out.println(UserMainCode.formNewWord(s1,n1));
s.close();
}
USERMAINCODE:
import java.util.*;
public class UserMainCode {
public static String formNewWord(String s1,int n)
{
String s = new String();
if(s1.length()>n)
{
s = s1.substring(0,n) + s1.substring(s1.length()-n, s1.length());
return s;
}
else
return null;
}
}
7. Reversing a Number
Write a program to read a positive number as input and to get the reverse of the given
number and return it as output.
Include a class UserMainCode with a static method reverseNumber which accepts a
positive
integer .
The return type is an integer value which is the reverse of the given number.
Create a Main class which gets the input as a integer and call the static
method reverseNumber present in the UserMainCode
Input and Output Format:
Input consists of a positive integer.
Output is an integer .
Refer sample output for formatting specifications.
Sample Input 1:
543
Sample Output 1:
345
Sample Input 1:
1111
Sample Output 1:
1111
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println(UserMainCode.reverseNumber(n));
s.close();
}
USERMAINCODE:
public class UserMainCode {
public static int reverseNumber(int n)
{
int a,r=0;
while(n!=0)
{
a=n%10;
r=r*10+a;
n=n/10;
}
return r;
}
}
UERMAINCODE:
import java.util.*;
public class UserMainCode {
public static ArrayList<Integer> sortMergedArraylist(ArrayList<Integer>
list1,ArrayList<Integer> list2)
{
list1.addAll(list2);
Collections.sort(list1);
ArrayList<Integer> ans=new ArrayList<Integer>();
ans.add(list1.get(2));
ans.add(list1.get(6));
ans.add(list1.get(8));
return ans;
}
}
UserMainCode:
import java.util.*;
import java.text.*;
public class UserMainCode{
public static int ValidateDate(String s1) {
if(s1.matches("[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}"))
{
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
try {
Date d1=sdf.parse(s1);
return 1;
} catch (ParseException e) {
return -1;
}
}
else{
return -1;}}}
import java.text.*;
import java.util.*;
public class UserMainCode{
public static int ValidateTime(String str){
StringTokenizer st=new StringTokenizer(str,":");
if(st.countTokens()==3)
{
SimpleDateFormat sdf1 = new SimpleDateFormat("h:mm:ss a");
sdf1.setLenient(false);
try
{
Date d2=sdf1.parse(str);
return 1;
}
catch(Exception e)
{
return -1;
}}
else
{
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
sdf.setLenient(false);
try
{
Date d1=sdf.parse(str);
return 1;
}
catch(Exception e){
return -1;
}}}}
UserMainCode:
Main:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
System.out.println(UserMainCode.removeEvenVowels(s1));
s.close();
}}
UserMainCode:
public class UserMainCode{
public static String removeEvenVowels(String s1) {
StringBuffer sb1=new StringBuffer();
for(int i=0;i<s1.length();i++)
if((i%2)==0)
sb1.append(s1.charAt(i));
else if((i%2)!=0)
if(s1.charAt(i)!='a' && s1.charAt(i)!='e' &&
s1.charAt(i)!='i' && s1.charAt(i)!='o' && s1.charAt(i)!='u')
if(s1.charAt(i)!='A' && s1.charAt(i)!='E' &&
s1.charAt(i)!='I' && s1.charAt(i)!='O' && s1.charAt(i)!='U')
sb1.append(s1.charAt(i));
return sb1.toString();
}}
14. Sum of Powers of elements in an array
Given a method with an int array. Write code to find the power of each individual element
accoding to its position index, add them up and return as output.
Include a class UserMainCode with a static method getSumOfPower which accepts an
integer array as input.
The return type of the output is an integer which is the sum powers of each element in the
array.
Create a Main class which gets integer array as an input and call the static
method getSumOfPowerpresent in the UserMainCode.
Input and Output Format:
Input is an integer array.First element corresponds to the number(n) of elements in an
array.The next inputs corresponds to each element in an array.
Output is an integer .
Sample Input 1:
4
3
6
2
1
Sample Output 1:
12
Sample Input 2:
4
5
3
7
2
Sample Output 2:
61
Main:
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println(UserMainCode.getSumOfPower(n,a));
sc.close();
}}
UserMainCode:
public class UserMainCode{
public static int getSumOfPower(int n,int[]a)
{{
int sum=0;
for(int i=0;i<n;i++)
sum=(int)(sum+Math.pow(a[i], i));
return sum;
}}}
Main:
import java.util.*;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println(UserMainCode.getBigDiff(a,n));
sc.close();
}}
UserMainCode:
import java.util.*;
public class UserMainCode{
public static int getBigDiff(int [] a,int n)
{
Arrays.sort(a);
int n1=a[a.length-1]-a[0];
return n1;
}}
Main:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int fr=sc.nextInt();
String a[]=new String[fr];
for(int i=0;i<fr;i++)
{
a[i]=sc.next();
}
String ba=sc.next();
UserMainCode.getElementPosition(a,ba);
sc.close();
}}
UserMainCode:
import java.util.*;
public class UserMainCode{
public static void getElementPosition(String[] a, String b) {
ArrayList<String>al=new ArrayList<String>();
for(int i=0;i<a.length;i++)
{
al.add(a[i]);
}
Collections.sort(al);
Collections.reverse(al);
for(int i=0;i<al.size();i++)
{
if(b.equals(al.get(i)))
{
System.out.println(i+1);
}}}}
Given a method taking an odd positive Integer number as input. Write code to evaluate the
following series:
1+3-5+7-9…+/-n.
Include a class UserMainCode with a static method addSeries which accepts a positive
integer .
Create a class Main which would get the input as a positive integer and call the static
Sample Input 1:
Sample Output 1:
-3
Sample Input 2:
11
Sample Output 2:
Main
import java.util.*;
int n=s.nextInt();
System.out.println(UserMainCode.addSeries(n));
s.close();
UserMainCode
import java.util.ArrayList;
import java.util.List;
for(int i=1;i<=n;i++)
if(i%2!=0)
l1.add(i);
int n1=l1.get(0);
for(int i=1;i<l1.size();i++)
if(i%2!=0)
n1=n1+l1.get(i);
else
n1=n1-l1.get(i);
return n1;
Given a method calculateElectricityBill() with three inputs. Write code to calculate the
current bill.
Create a class Main which would get the inputs and call the static
First input is previous reading, second input is current reading and last input is per unit
charge.
Reading Format - XXXXXAAAAA where XXXXX is consumer number and AAAAA is
meter
reading.
Sample Input 1:
ABC2012345
ABC2012660
Sample Output 1:
1260
Sample Input 2:
ABCDE11111
ABCDE11222
Sample Output 2:
333
Main
import java.util.Scanner;
String input1=s.next();
String input2=s.next();
int input3=s.nextInt();
System.out.println(UserMainCode.calculateElectricityBill(input1,input2,inpu
t3));
s.close();
UserMainCode
int n=Math.abs((n2-n1)*input3);
return n;
Write code to get the sum of all the digits present in the given string.
Include a class UserMainCode with a static method sumOfDigits which accepts string
input.
Return the sum as output. If there is no digit in the given string return -1 as output.
Create a class Main which would get the input and call the static
good23bad4
Sample Output 1:
Sample Input 2:
good
Sample Output 2:
-1
Main
import java.util.Scanner;
String s1=s.next();
UserMainCode.sumOfDigits(s1);
s.close();
UserMainCode
int sum=0;
for(int i=0;i<s1.length();i++)
{
char a=s1.charAt(i);
if(Character.isDigit(a))
int b=Integer.parseInt(String.valueOf(a));
sum=sum+b;
if(sum==0)
System.out.println(-1);
else
System.out.println(sum);
20.String Concatenation
Write code to get two strings as input and If strings are of same length simply append them
together and return the final string. If given strings are of different length, remove starting
characters from the longer string so that both strings are of same length then append them
Include a class UserMainCode with a static method concatstring which accepts two
string
input.
The return type of the output is a string which is the concatenated string.
Create a class Main which would get the input and call the static
method concatstring present in the UserMainCode.
Output is a string.
Sample Input 1:
Hello
hi
Sample Output 1:
lohi
Sample Input 2:
Hello
Delhi
Sample Output 2:
HelloDelhi
Main
import java.util.Scanner;
String s1=s.next();
String s2=s.next();
UserMainCode.concatstring(s1,s2);
s.close();
}
} UserMainCode
int l1=s1.length();
int l2=s2.length();
if(l1==l2)
sb.append(s1).append(s2);
else if(l1>l2)
sb.append(s1.substring(s1.length()-
s2.length(),s1.length())).append(s2);
else if(l1<l2)
sb.append(s1).append(s2.substring(s2.length()-
s1.length(),s2.length()));
System.out.println(sb);
string. The return type (integer) should return 1 if the color is as per the rules else return -1.
Create a Class Main which would be used to accept a String and call the static method
present in UserMainCode.
Sample Input 1:
#FF9922
Sample Output 1:
Valid
Sample Input 2:
#FF9(22
Sample Output 2:
Invalid
Main
import java.util.*;
String s1=s.next();
int b=UserMainCode.validateColorCode(s1);
if(b==1)
System.out.println("Valid");
else
System.out.println("Invalid");
s.close();
UserMainCode
int b=0,b1=0;
String s2=s1.substring(1,s1.length());
if(s1.length()==7)
if(s1.charAt(0)=='#')
b1=1;
if(b1==1){
/*for(int i=0;i<s2.length();i++){
char c=s2.charAt(i);
if(c!='#')
{*/
if(s2.matches("[A-F0-9]{1,}"))
b=1;
else
b=-1;
//break;
return b;
22.Three Digits
Write a program to read a string and check if the given string is in the format "CTS-XXX"
The return type (integer) should return 1 if the string format is correct else return -1.
Create a Class Main which would be used to accept a String and call the static method
present in UserMainCode.
Sample Input 1:
CTS-215
Sample Output 1:
Valid
Sample Input 2:
CTS-2L5
Sample Output 2:
Invalid
Main
import java.util.Scanner;
String s1=s.next();
int b=UserMainCode.validatestrings(s1);
if(b==1){
System.out.println("Valid");}
else
System.out.println("Invalid");
s.close();
UserMainCode
int res=0;
if(s1.matches("(CTS)[-]{1}[0-9]{3}"))
res=1;
else
res=-1;
return res;
Given a method with a HashMap<Integer,string> as input. Write code to remove all the
entries having keys multiple of 4 and return the size of the final hashmap.
The return type of the output is an integer which is the size of the resultant hashmap.
Create a class Main which would get the input and call the static
Sample Input 1:
hi
hello
12
hello world
Sample Output 1:
Sample Input 2:
hi
sdfsdf
asdf
Sample Output 2:
Main
import java.util.*;
int s=sc.nextInt();
for(int i=0;i<s;i++){
hm.put((sc.nextInt()),(sc.next()));
System.out.println(UserMainCode.sizeOfResultandHashMap(hm));
sc.close();
} }
UserMainCode
import java.util.HashMap;
import java.util.Iterator;
int count=0;
Iterator<Integer>itr=hm.keySet().iterator();
while(itr.hasNext())
int n=itr.next();
if(n%4!=0)
count++;
return count;
24.Largest Element
Write a program to read an int array of odd length, compare the first, middle and the last
elements in the array and return the largest. If there is only one element in the array return
accepts an int arrayThe return type (integer) should return the largest element among the
Create a Class Main which would be used to accept Input array and call the static method
present in UserMainCode.
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in
the array. The next 'n' integers correspond to the elements in the array.
Sample Input 1:
Sample Output 1:
Main
import java.util.*;
int n=s.nextInt();
for(int i=0;i<n;i++){
a[i]=s.nextInt();
System.out.println(UserMainCode.checkLargestAmongCorner(a));
s.close();
UserMainCode
int max=0;
int x,y,z;
x=a[0];
y=a[a.length/2];
z=a[a.length-1];
max=x;
max=y;
else if(z>x && z>y)
max=z;
return max;
25. nCr
Write a program to calculate the ways in which r elements can be selected from n
population, using nCr formula nCr=n!/r!(n-r)! where first input being n and second input
being r.
Note2 : 0! = 1.
Example 5!=5x4x3x2x1=120
Include a class UserMainCode with a static method calculateNcr which accepts two
integers. The return type (integer) should return the value of nCr.
Create a Class Main which would be used to accept Input elements and call the static
Input consists of 2 integers. The first integer corresponds to n, the second integer
corresponds to r.
Sample Input 1:
3
Sample Output 1:
Main
import java.util.*;
int n=s.nextInt();
int r=s.nextInt();
System.out.println(UserMainCode.calculateNcr(n,r));
UserMainCode
int fact=1,fact1=1,fact2=1;
for(int i=1;i<=n;i++)
fact=fact*i;
//System.out.println(fact);
for(int i=1;i<=r;i++)
fact1=fact1*i;
}
//System.out.println(fact1);
for(int i=1;i<=(n-r);i++)
fact2=fact2*i;
//System.out.println(fact2);
int res=fact/(fact1*fact2);
return res;
Write a program to find out sum of common elements in given two arrays. If no common
two integer arrays and their sizes. The return type (integer) should return the sum of
common elements.
Create a Class Main which would be used to accept 2 Input arrays and call the static method
present in UserMainCode.
Input consists of 2+m+n integers. The first integer corresponds to m (Size of the 1st array),
the second integer corresponds to n (Size of the 2nd array), followed by m+n integers
Output consists of a single Integer corresponds to the sum of common elements or a string
“No common elements”.
Sample Input 1:
Sample Output 1:
Sample Input 2:
12
31
9
Sample Output 2:
No common elements
Main
import java.util.Scanner;
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=0;i<n;i++){
a[i]=sc.nextInt();}
for(int i=0;i<m;i++){
b[i]=sc.nextInt();}
if(u==-1)
else
System.out.println(u);
sc.close();
}}
UserMainCode
public class UserMainCode {
public static int getSumOfIntersection(int a[],int b[],int n,int m)
int sum=0;
for(int i=0;i<a.length;i++)
for(int j=0;j<b.length;j++)
{if(a[i]==b[j])
sum=sum+a[i];
}}
if(sum==0)
return -1;
else
return sum;
102.Write a code get a password as string input and validate using the rules specified below.
password string as input and returns an integer. The method returns 1 if the password is
valid. Else it returns -1.
Create a class Main which would get the input and call the static
Sample Input 1:
ashok_23
Sample Output 1:
Valid
Sample Input 2:
1980_200
Sample Output 2:
Invalid
Main
import java.util.*;
String a=sc.next();
int e=UserMainCode.validatePassword(a);
if(e==1){
System.out.println("Valid");
}
else
System.out.println("Invalid");
sc.close();
}}
UserMainCode
int d=0;
if(a.length()>=8){
char c= a.charAt(0);
//System.out.println(c);
if(Character.isAlphabetic(c))
char dd=a.charAt(a.length()-1);
//System.out.println(dd);
if((Character.isAlphabetic(dd))||(Character.isDigit(dd)))
if(a.matches(".*[0-9]{1,}.*")||a.matches(".*[a-zA-Z]{1,}.*")){
d=1;
}
}
else
d=-1;
return d;
}}
28.iD Validation
Write a program to get two string inputs and validate the ID as per the specified format.
strings as input.
Create a class Main which would get the input and call the static
First string is ID and second string is location. ID is in the format CTS-LLL-XXXX where
LLL is
the first three letters of given location and XXXX is a four digit number.
Sample Input 1:
CTS-hyd-1234
hyderabad
Sample Output 1:
Valid id
Sample Input 2:
CTS-hyd-123
hyderabad
Sample Output 2:
Invalid id
Main
import java.util.*;
String s1=sc.next();
String s2=sc.next();
boolean b=UserMainCode3.validateIDLocations(s1,s2);
if(b==true)
System.out.println("Valid id");
else
System.out.println("Invalid id");
sc.close();
}
UserMainCode
import java.util.StringTokenizer;
boolean b=false;
String s4=t.nextToken();
String s5=t.nextToken();
String s6=t.nextToken();
b=true;
else{
b=false;}
return b;
29.Remove Elements
Write a program to remove all the elements of the given length and return the size of the
final array as output. If there is no element of the given length, return the size of the same
array as output.
array, the number of elements in the array and an integer. The return type (integer) should
Create a Class Main which would be used to accept Input String array and a number and call
Input consists of a integers that corresponds to n, followed by n strings and finally m which
Sample Input 1:
bb
ccc
ddd
Sample Output 1:
Main
import java.util.*;
public class Main
int n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
int m=Integer.parseInt(sc.nextLine());
System.out.println(UserMainCode.removeElements(a,m));
sc.close();
UserMainCode
int u=a.length;
for(int i=0;i<a.length;i++)
if(a[i].length()==m)
u--;
return u;
}
30.Find the difference between Dates in months
Given a method with two date strings in yyyy-mm-dd format as input. Write code to find the
The return type of the output is an integer which returns the diffenece between two dates
in months.
Create a class Main which would get the input and call the static
Output is an integer.
Sample Input 1:
2012-03-01
2012-04-16
Sample Output 1:
Sample Input 2:
2011-03-01
2012-04-16
Sample Output 2:
13
Main
import java.text.*;
import java.util.*;
String s1=sc.next();
String s2=sc.next();
System.out.println(UserMainCode.getMonthDifference(s1,s2));
sc.close();
}}
UserMainCode
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
Date d1=sdf.parse(s1);
Date d2=sdf.parse(s2);
Calendar cal=Calendar.getInstance();
cal.setTime(d1);
int months1=cal.get(Calendar.MONTH);
int year1=cal.get(Calendar.YEAR);
cal.setTime(d2);
int months2=cal.get(Calendar.MONTH);
int year2=cal.get(Calendar.YEAR);
int n=((year2-year1)*12)+(months2-months1);
return n;
Write a program to get an int array as input and identify even and odd numbers. If number
is odd get cube of it, if number is even get square of it. Finally add all cubes and squares
Include a class UserMainCode with a static method addEvenOdd which accepts integer
array as input.
The return type of the output is an integer which is the sum of cubes and squares of
Create a class Main which would get the input and call the static
Sample Input 1:
Sample Output 1:
208
Main
import java.util.Scanner;
int n=sc.nextInt();
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
System.out.println(UserMainCode.addEvenOdd(a));
sc.close();
}
}
UserMainCode
int n1=0,n2=0;
for(int i=0;i<a.length;i++)
if(a[i]%2==0)
n1+=(a[i]*a[i]);
else
n2+=(a[i]*a[i]*a[i]);
return n1+n2;
32.IP Validator
Write a program to read a string and validate the IP address. Print “Valid” if the IP address is
Include a class UserMainCode with a static method ipValidator which accepts a string.
The
Create a Class Main which would be used to accept Input String and call the static method
present in UserMainCode.
Note: An IP address has the format a.b.c.d where a,b,c,d are numbers between 0-255.
Sample Input 1:
132.145.184.210
Sample Output 1:
Valid
Sample Input 2:
132.145.184.290
Sample Output 2:
Invalid
Main
import java.util.*;
String ipAddress=sc.next();
boolean b=UserMainCode7.validateIpAddress(ipAddress);
if(b==true)
System.out.println("Valid");
else
System.out.println("Invalid");
sc.close();
}
}
UserMainCode
import java.util.StringTokenizer;
boolean b1=false;
int a=Integer.parseInt(t.nextToken());
int b=Integer.parseInt(t.nextToken());
int c=Integer.parseInt(t.nextToken());
int d=Integer.parseInt(t.nextToken());
&& d<=255))
b1=true;
return b1;
Get two date strings as input and write code to find difference between two dates in days.
Include a class UserMainCode with a static method getDateDifference which accepts two
date strings as input.
The return type of the output is an integer which returns the diffenece between two dates
in days.
Create a class Main which would get the input and call the static
Output is an integer.
Sample Input 1:
2012-03-12
2012-03-14
Sample Output 1:
Sample Input 2:
2012-04-25
2012-04-28
Sample Output 2:
Main
import java.text.ParseException;
import java.util.*;
String s1=s.nextLine();
String s2=s.nextLine();
int output=UserMainCode.getDateDifference(s1,s2);
System.out.println(output);
s.close();
UserMainCode
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
Date d=sd.parse(s1);
Calendar c=Calendar.getInstance();
c.setTime(d);
long d1=c.getTimeInMillis();
d=sd.parse(s2);
c.setTime(d);
long d2=c.getTimeInMillis();
return n;
}
}
34.File Extension
Write a program to read a file name as a string and find out the file extension and return it
as output. For example, the file sun.gif has the extension gif.
Include a class UserMainCode with a static method fileIdentifier which accepts a string. The
return type (string) should return the extension of the input string (filename).
Create a Class Main which would be used to accept Input String and call the static method
present in UserMainCode.
Sample Input 1:
sun.gif
Sample Output 1:
Gif
Main
import java.util.*;
String s1=s.nextLine();
String output=UserMainCode.fileIdentifier(s1);
System.out.println(output);
s.close();
UserMainCode
import java.util.*;
t.nextToken();
String s2=t.nextToken();
return s2;
Given a method with two strings as input. Write code to count the common and unique
Note:
Include a class UserMainCode with a static method commonChars which accepts two
strings as input.
The return type of the output is the count of all common and unique characters in the two
strings.
Create a class Main which would get the inputs and call the static
Output is an integer.
Sample Input 1:
a black cow
battle ship
Sample Output 1:
[Explanation : b, l and a are the common letters between the 2 input strings. But 'a'
appears
more than once in the 1st string. So 'a' should not be considered while computing the count
value.]
Sample Input 2:
australia
sri lanka
Sample Output 2:
Main
import java.util.Scanner;
String s1=sc.nextLine();
String s2=sc.nextLine();
int output=UserMainCode.commonChars(s1,s2,sb1,sb2);
System.out.println(output);
sc.close();
UserMainCode
import java.util.*;
for(int i=0;i<sb1.length();i++){
int c=0;
for(int j=i+1;j<sb1.length();j++){
if(sb1.charAt(i)==sb1.charAt(j)){
sb1.deleteCharAt(j);
c++;
if(c>=1){
sb1.deleteCharAt(i);
for(int i=0;i<sb2.length();i++){
int c=0;
for(int j=i+1;j<sb2.length();j++){
if(sb2.charAt(i)==sb2.charAt(j)){
sb2.deleteCharAt(j);
c++;
if(c>=1){
sb2.deleteCharAt(i);
int count=0;
for(int i=0;i<sb1.length();i++){
for(int j=0;j<sb2.length();j++){
if(sb1.charAt(i)==sb2.charAt(j)){
count++;
return (count);
36.Initial Format
Write a program to input a person's name in the format "FirstName LastName" and return
Create a Class Main which would be used to accept Input String and call the static method
present in UserMainCode.
Sample Input :
Jessica Miller
Sample Output:
Miller, J
Main
import java.util.*;
String s1=s.nextLine();
String output=UserMainCode.nameFormatter(s1);
System.out.println(output);
s.close();
UserMainCode
import java.util.*;
String s2=st.nextToken();
String s3=st.nextToken();
sb.append(s3).append(",");
sb.append(s2.substring(0,1).toUpperCase());
return sb.toString();
37.Character cleaning
Write a program to input a String and a character, and remove that character from the given
and a character. The return type (string) should return the character cleaned string.
Create a Class Main which would be used to accept Input String and call the static method
present in UserMainCode.
Sample Input :
elephant
Sample Output:
Lphant
Main
import java.util.*;
String s1=s.nextLine();
String c=s.nextLine();
String output=UserMainCode.removeCharacter(s1,c);
System.out.println(output);
UserMainCode
import java.util.*;
String d=s1.replace(c,"");
return d;
38.Vowel Check
Write a program to read a String and check if that String contains all the vowels. Print "yes"
Include a class UserMainCode with a static method getVowels which accepts a string. The
return type (integer) should return 1 if the String contains all vowels else return -1.
Create a Class Main which would be used to accept Input String and call the static method
present in UserMainCode.
Sample Input 1:
abceiduosp
Sample Output 1:
yes
Sample Input 2:
bceiduosp
Sample Output 2:
No
Main
import java.util.*;
String s1=s.nextLine();
String s2=s1.toLowerCase();
int output=UserMainCode.getVowels(s2);
//System.out.println(output);
if(output==1)
System.out.println("yes");
else
System.out.println("no");
s.close();
UserMainCode
import java.util.*;
return 1;
else
return -1;
39.Swap Characters
Write a program to input a String and swap the every 2 characters in the string. If size is an
odd number then keep the last letter as it is. Print the final swapped string.
Include a class UserMainCode with a static method swapCharacter which accepts a string.
The return type (String) should return the character swapped string.
Create a Class Main which would be used to accept Input String and call the static method
present in UserMainCode.
Sample Input 1:
TRAINER
Sample Output 1:
RTIAENR
Sample Input 2:
TOM ANDJERRY
Sample output 2:
OT MNAJDREYR
Main
import java.util.*;
String s1=s.nextLine();
String output=UserMainCode.swapCharacter(s1);
System.out.println(output);
s.close();
UserMainCode
import java.util.*;
{
StringBuffer sb=new StringBuffer();
int l=s1.length();
if(l%2==0)
for(int i=0;i<s1.length()-1;i=i+2)
char a=s1.charAt(i);
char b=s1.charAt(i+1);
sb.append(b).append(a);
return sb.toString();
else
for(int i = 0;i<s1.length()-1;i=i+2)
char a=s1.charAt(i);
char b=s1.charAt(i+1);
sb.append(b).append(a);
sb.append(s1.charAt(l-1));
return sb.toString();
Given a method with a HashMap<int, float> as input. Write code to find out avg of all
values whose keys are even numbers. Round the average to two decimal places and return
as output.
[Hint : If the average is 5.901, the rounded average value is 5.9 . It the average is 6.333, the
The return type of the output is a floating point value which is the average of all values
Create a class Main which would get the input and call the static method avgOfEven
present
in the UserMainCode.
Input consists of the number of elements in the HashMap and the HashMap<int, float>.
Sample Input 1:
2.3
4.1
6.2
Sample Output 1:
5.15
Sample Input 2:
3.1
6.3
2.6
Sample Output 2:
6.3
Main
import java.util.HashMap;
import java.util.Scanner;
int s=sc.nextInt();
HashMap<Integer,Float>hm=new HashMap<Integer,Float>();
for(int i=0;i<s;i++)
int r=sc.nextInt();
Float j=sc.nextFloat();
hm.put(r,j);
System.out.println(UserMainCode.display(hm));
sc.close();
}
UserMainCode
import java.text.DecimalFormat;
import java.util.*;
float sum=0;
int count=0;
Iterator<Integer> it=hm.keySet().iterator();
while(it.hasNext())
int y=it.next();
if(y%2==0)
sum=(float) (sum+hm.get(y));
count++;
}}
float d=sum/count;
return df.format(d);
}
41.Calculate Average – Hash Map
Write amethod that accepts the input data as a hash map and finds out the avg of all values
whose keys are odd numbers.
Include a class UserMainCode with a static method calculateAverage which accepts
aHashMap<Integer,Double> and the size of the HashMap. The return type (Double) should
return the calculated average. Round the average to two decimal places and return it.
Create a Class Main which would be used to accept Input values and store it as a hash map,
and call the static method present in UserMainCode.
Input and Output Format:
Input consists of an integer n corresponds to number of hash map values, followed by 2n
values. (index followed by value).
Output consists of a Double.
Refer sample input and output for formatting specifications.
Sample Input :
4
1
3.41
2
4.1
3
1.61
4
2.5
Sample Output :
2.51
Main
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] arg)
{
HashMap<Integer,Double> hm=new HashMap<Integer,Double>();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
int a=sc.nextInt();
double s=sc.nextDouble();
hm.put(a,s);
}
System.out.println(UserMaincode.dis(hm));}}
UserMainCode
class UserMaincode
{
public static double dis(HashMap<Integer,Double> h1)
{
double avg=0.0,sum=0.0;
int k=0;
for(Map.Entry m:h1.entrySet())
{
int a=(Integer)m.getKey();
if(a%2!=0)
{
Double d=(Double) m.getValue();
sum=sum+d;
k++;
}
}
avg = (double)sum/k;
DecimalFormat df = new DecimalFormat(".##");
String b1 = df.format(avg);
double b = Double.parseDouble(b1);
return b;}
System.out.println(UserMainCode.consecutiveRepeatitionOfChar(input1));
}
}
UserMainCode
class UserMainCode
{
UserMaincode
class UserMainCode
{
public static int largestChunk(String s1) {
int max=1;
int b=0;
StringTokenizer t=new StringTokenizer(s1," ");
while(t.hasMoreTokens()){
String s2=t.nextToken();
int n=0;
for(int i=0;i<s2.length()-1;i++)
if(s2.charAt(i)==s2.charAt(i+1))
n++;
if(n>max)
{
max=n;
b=max+1;
}
}
return b;
}
}
UserMainCode
class UserMaincode
{
public static int uniqueCounter(String s1)
{
45.Name Shrinking
Write a program that accepts a string as input and converts the first two names into
dotseparated
initials and printa the output.
Input string format is 'fn mn ln'. Output string format is 'ln [mn's 1st character].[fn's 1st
character]'
Include a class UserMainCode with a static method getFormatedString which accepts a
string. The return type (String) should return the shrinked name.
Create a Class Main which would be used to accept Input String and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of a String.
Refer sample output for formatting specifications.
Sample Input:
Sachin Ramesh Tendulkar
Sample Output:
Tendulkar R.S
Main
import java.text.*;
import java.util.*;
UserMainCode
class UserMainCode
{
UserMainCode
public class UserMainCode {
public static int oddDigitSum (String[] s1) {
int sum=0;
for(int i=0;i<s1.length;i++)
for(int j=0;j<s1[i].length();j++){
char c=s1[i].charAt(j);
if(Character.isDigit(c)){
if(c%2!=0)
{
String t=String.valueOf(c);
int n=Integer.parseInt(t);
sum=sum+n; } }}
return sum;
}
}
47.Unique Number
Write a program that accepts an Integer as input and finds whether the number is Unique or
not. Print Unique if the number is “Unique”, else print “Not Unique”.
Note: A Unique number is a positive integer (without leading zeros) with no duplicate
digits.For example 7, 135, 214 are all unique numbers whereas 33, 3121, 300 are not.
Include a class UserMainCode with a static method getUnique which accepts an integer.
The return type (Integer) should return 1 if the number is unique else return -1.
Create a Class Main which would be used to accept Input Integer and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of an integer .
Output consists of a String (“Unique” or “Not Unique”).
Refer sample output for formatting specifications.
Sample Input 1:
123
Sample Output 1:
Unique
Sample Input 2:
33
Sample Output 2:
Not Unique
import java.util.*;
import java.text.*;
class UserMainCode
{
public static int getUnique(int n)
{
int []a=new int[100];
int i=0,count=0;
while(n!=0)
{
int num=n%10;
a[i]=num;
i++;
n=n/10;
}
for(int j=0;j<=i-1;j++)
{
for(int k=j+1;k<=i-1;k++)
{
if(a[j]==a[k]){
count++;
}
}}
return count;
}
Main
import java.util.*;
public class Add {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.next();
boolean b=userMainCode. validateColourCode (s1);
if(b==true)
System.out.println("valid color code");
else
System.out.println("invalid color code");
}
UserMainCode
boolean b=false;
if(s1.length()==7&&s1.matches("#[A-F0-9]{1,}")){
b=true;
}
return b;
}}}
Main
import java.util.*;
public class Main {
UserMainCode
class userMainCode {
Main
import java.util.*;
import java.text.*;
public class Main {
UserMainCode
class userMainCode{
public static String calculateBornDay(String input) throws ParseException{
SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf1= new SimpleDateFormat("EEEEE");
Date d= sdf.parse(input);
String s1= sdf1.format(d);
return s1;
}
}
Main
import java.util.*;
import java.text.*;
public class Main {
System.out.println(UserMainCode.afterDelete(hm));
s.close();
}
UserMainCode
class UserMainCode{
public static int afterDelete(HashMap<Integer, String> hm){
int count=0;
Iterator<Integer>itr=hm.keySet().iterator();
while(itr.hasNext())
{
int n=itr.next();
if(n%3!=0)
{
count++;
}
}
return count;
}
}
53.Experience Calculator
Write a program to read Date of Joining and current date as Strings and Experience
as integer and validate whether the given experience and calculated experience are the
same. Print “true” if same, else “false”.
Include a class UserMainCode with a static method calculateExperience which accepts
2
strings and an integer. The return type is boolean.
Create a Class Main which would be used to accept 2 string (dates) and an integer and call
the static method present in UserMainCode.
Input and Output Format:
Input consists of 2 strings and an integer, where the 2 strings corresponds to the date of
joining and current date, and the integer is the experience.
Output is either “true” or “false”.
Refer sample output for formatting specifications.
Sample Input 1:
11/01/2010
01/09/2014
4
Sample Output 1:
true
Sample Input 2:
11/06/2009
01/09/2014
4
Sample Output 2:
False
Main
import java.util.*;
import java.text.*;
public class Main {
UserMainCode
class userMainCode{
public static long calculateExperience(String a, String b, int c)throws
ParseException{
54.Flush Characters
Write a program to read a string from the user and remove all the alphabets and
spaces from the String, and only store special characters and digit in the output
String. Print
the output string.
Include a class UserMainCode with a static method getSpecialChar which accepts a
string.
The return type (String) should return the character removed string.
Create a Class Main which would be used to accept a string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a strings.
Output consists of an String (character removed string).
Refer sample output for formatting specifications.
Sample Input :
cogniz$#45Ant
Sample Output :
$#45
Main:
import java.util.*;
import java.text.*;
public class Main {
UserMainCode:
class UserMainCode{
public static String getSpecialChar(String input){
int i;
StringBuffer sb= new StringBuffer();
for(i=0;i<input.length();i++)
{
char a=input.charAt(i);
if (!Character.isAlphabetic(a))
sb.append(a);
}
return sb.toString();
}
}
55.String Repetition
Write a program to read a string and an integer and return a string based on the below
rules.
If input2 is equal or greater than 3 then repeat the first three character of the String by
given input2 times, separated by a space.
If input2 is 2 then repeat the first two character of String two times separated by a space,
If input2 is 1 then return the first character of the String.
Include a class UserMainCode with a static method repeatString which takes a string &
integer and returns a string based on the above rules.
Create a Class Main which would be used to accept Input string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a string and integer.
Output consists of a string.
Refer sample output for formatting specifications.
Sample Input 1:
COGNIZANT
4
Sample Output 1:
COG COG COG COG
Sample Input 2:
COGNIZANT
2
Sample Output 2:
CO CO
class Main{
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println(“enter a string”);
String input= s.next();
int n= s.nextInt();
System.out.println(UserMainCode.repeatString(input, n));
class UserMainCode{
public static String repeatString(String input, int n){
StringBuffer sb= new StringBuffer();
String s1= new String();
if (n==1){
s1=input.substring(0,1);
sb.append(s1).append(" ");
}
if(n==2){
s1=input.substring(0,2);
for(int i=0;i<n;i++)
sb.append(s1).append(" ");
}
if(n>=3){
s1=input.substring(0,3);
for(int i=0;i<n;i++)
sb.append(s1).append(" ");
}
return sb.toString();
}
}
Main:
import java.util.*;
import java.text.*;
public class Main {
UserMainCode:
class UserMainCode{
public static double AverageElements(int array[]){
int n, sum=0,count=0,count1=0;
double average;
n=array.length;
for(int i=0;i<=n;i++){
for(int j=1;j<n;j++){
if(i%j==0)
count++;
if(count==2){
sum= sum+array[i];
count1++;
}
}
}
average= sum/count1;
DecimalFormat df=new DecimalFormat("#.00");
double ddd=Double.parseDouble(df.format(average));
return ddd;
}
}
57.Common Elements
Write a program to read two integer arrays and find the sum of common elements in both
the arrays. If there are no common elements return -1 as output Include a class
UserMainCode with a static method sumCommonElements which accepts two single integer
array. The return type (integer) should be the sum of common elements.
Create a Class Main which would be used to accept Input array and call the static method
present in UserMainCode. Assume that all the elements will be distinct. Input and Output
Format: Input consists of 2n+1 integers. The first integer corresponds to n, the number of
elements in the array. The next 'n' integers correspond to the elements in the array, The last n
elements correspond to the elements of the second array. Output consists of a single Integer
value. Refer sample output for formatting specifications.
Sample Input 1:
4
1
2
3
4
2
3
6
7
Sample Output 1:
5
Main:
import java.util.*;
public class Main {
private static Scanner s ;
;
public static void main(String[] args) {
s = new Scanner (System.in);
int n = s.nextInt();
int a[] = new int[n];
int b[] = new int[n];
for(int i=0;i<n;i++)
{
a[i] = s.nextInt();
}
for(int i=0;i<n;i++)
{
b[i] = s.nextInt();
}
System.out.println(UserMainCode.sumCommonElements(a, b));
}
}
UserMainCode:
Sample Input 1:
5
1
5
23
64
9
Sample Output 1:
23
Main:
import java.util.*;
s = new Scanner(System.in);
int n = s.nextInt();
for(int i=0;i<n;i++){
a[i] = s.nextInt();
System.out.println(UserMainCode.getMiddleElement(a));
UserMainCode:
int n = a.length;
return a[n/2];
}
}
Main:
import java.util.*;
s = new Scanner(System.in);
String s1 = s.next();
System.out.println(UserMainCode.getString(s1));
UserMainCode:
char a=s1.charAt(0);
char b=s1.charAt(1);
if(a!='j'&& b!='b')
sb.append(s1.substring(2));
sb.append("j").append(s1.substring(2));
sb.append(s1.substring(1));
else
sb.append(s1.substring(0));
return sb.toString();
Main:
import java.util.*;
String s= sc.next();
int b = UserMainCode.getvalues(s);
if(b==1)
System.out.println("Valid");
else
System.out.println("Invalid");
UserMainCode:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
if(s.matches("[0-9]{2}[.]{1}[0-9]{2}[.]{1}[0-9]{4}"))
sdf.setLenient(false);
try
Date d1=sdf.parse(s);
return 1;
} catch (ParseException e) {
return -1;
else if(s.matches("[0-9]{2}[/]{1}[0-9]{2}[/][0-9]{4}"))
sdf.setLenient(false);
try
Date d1=sdf.parse(s);
return 1;
} catch (ParseException e) {
return -1;
else if(s.matches("[0-9]{2}[-]{1}[0-9]{2}[-][0-9]{4}"))
sdf.setLenient(false);
try
Date d1=sdf.parse(s);
return 1;
} catch (ParseException e) {
return -1;
else
return -1;
Main:
import java.util.*;
import java.util.Arrays;
int s = sc.nextInt();
a[i] = sc.nextInt();
System.out.println(UserMainCode.getBoundaryAverage(a));
UserMainCode
import java.util.Arrays;
Arrays.sort(a);
return avg;
}
62. Count Vowels
Given a string input, write a program to find the total number of vowels in the given string.
Include a class UserMainCode with a static method “countVowels” that accepts a String
argument and returns an int that corresponds to the total number of vowels in the given string.
Create a class Main which would get the String as input and call the static method
countVowels present in the UserMainCode. Input and Output Format: Input consists of a
string. Output consists of an integer..
Sample Input:
avinash
Sample Output:
3
Main:
import java.util.*;
public class Main {
private static Scanner s;
UserMainCode
public class UserMainCode{
public static int countVowels(String s1)
{
String s2=s1.toLowerCase();
String s3="aeiou";
int count=0;
for(int i=0;i<s2.length();i++)
{
for(int j=0;j<s3.length();j++)
{
if(s2.charAt(i)==s3.charAt(j))
{
count++;
}
}
}
return count;
}
}
Main:
import java.text.ParseException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ParseException {
UserMainCode:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UserMainCode {
public static String calculateBornDay(String s1) throws ParseException
{
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yy");
SimpleDateFormat sdf1=new SimpleDateFormat("MMMM");
Date d=sdf.parse(s1);
String s=sdf1.format(d);
return s.toUpperCase();
}
}
Main:
import java.util.*;
public class Main {
private static Scanner s;
public static void main(String[] args) {
s =new Scanner(System.in);
String input1= s.next();
int input2=s.nextInt();int input3=s.nextInt();
System.out.println(UserMainCode.retrieveString(input1,input2,input3));
}
}
UserMainCode
class UserMainCode {
public static String retrieveString(String input1, int input2, int input3) {
StringBuffer sb=new StringBuffer(input1);
sb.reverse();
String output=sb.substring(input2, input2+input3);
return output;
}
}
65. String Finder
Given three strings say Searchstring, Str1 and Str2 as input, write a program to find out if Str2
comes after Str1 in the Searchstring.
Include a class UserMainCode with a static method “stringFinder” that accepts 3 String
arguments and returns an integer. The 3 arguments correspond to SearchString, Str1 and Str2.
The function returns 1 if Str2 appears after Str1 in the Searchtring. Else it returns 2.
Create a class Main which would get 3 Strings as input and call the static
method stringFinder present in the UserMainCode.
USERMAINCODE:
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.next();
String s2=s.next();
String s3=s.next();
int b=UserMainCode.stringFinder(s1, s2, s3);
if(b==1)
{
System.out.println("yes");
}
else
System.out.println("No");
s.close();
}
USERMAINCODE:
if (s2.matches("[0-9]{10}"))
return 1;
else
return 2;
}
MAIN:
import java.util.*;
String s1=s.nextLine();
int b=UserMainCode.validatePhoneNumber(s1);
if(b==1)
System.out.println("Valid");
else
System.out.println("Invalid");
s.close();
Given two inputs year and month (Month is coded as: Jan=0, Feb=1 ,Mar=2 ...), write a
program to find out total number of days in the given month for the given year. Include a
class UserMainCode with a static method “getNumberOfDays” that
accepts 2 integers as arguments and returns an integer. The first argument corresponds to
the year and the second argument corresponds to the month code. The method returns an
integer corresponding to the number of days in the month.
Create a class Main which would get 2 integers as input and call the static method
getNumberOfDays present in the UserMainCode.
import java.util.Calendar;
public class UserMainCode {
public static int getNumberOfDays(int y,int c)
{
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, c);
int day=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return day;
}
}
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int y=s.nextInt();
int c=s.nextInt();
System.out.println(UserMainCode.getNumberOfDays(y, c));
s.close();
}
}
Given a string input, write a program to replace every appearance of the word "is" by "is
not".
If the word "is" is immediately preceded or followed by a letter no change should be made
to the string .
Include a class UserMainCode with a static method “negativeString” that accepts a String
arguement and returns a String.
Create a class Main which would get a String as input and call the static method
negativeString present in the UserMainCode.
USERMAINCODE:
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
System.out.println(UserMainCode.negativeString(s1));
s.close();
}
}
USERMAINCODE:
MAIN:
import java.util.*;
public class Main {
70. Digits
Write a program to read a non-negative integer n that returns the count of the occurances of
7 as digit.
Include a class UserMainCode with a static method countSeven which accepts the integer
value. The return type is integer which is the count value.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
USERMAINCODE:
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println(UserMainCode.countSeven(n));
s.close();
}
}
71. String Processing – III
Write a program to read a string where all the lowercase 'x' chars have been moved to the
end of the string.
Include a class UserMainCode with a static method moveX which accepts the string. The
return type is the modified string.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
USERMAINCODE:
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.next();
System.out.println(UserMainCode.moveX(s1));
s.close();
}}
Write a program to read a string and also a number N. Form a new string starting with
1stcharacter and with every Nth character of the given string. Ex - if N is 3, use chars 1, 3, 6,...
and so on to form the new String. Assume N>=1.
Include a class UserMainCode with a static method getStringUsingNthCharacter which
accepts the string and the number n. The return type is the string as per the problem
statement.
Create a Class Main which would be used to accept the string and integer and call the static
method present in UserMainCode.
USERMAINCODE:
}
return sb.toString();
}}
MAIN:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.next();
int n=s.nextInt();
System.out.println(UserMainCode.getStringUsingNthCharacter(s1, n));
s.close();
}
UserMainCode
public class UserMainCode {
public static boolean compareLastDigit(int c,int d)
{
int c1=c%10;
int d1=d%10;
boolean b=false;
if(c1==d1)
{
b=true;
}
return b;
}
}
Main
import java.util.*;
public class Main {
else
{
System.out.println("FALSE");
}
s.close();
}
74. Duplicates
GIven three integers (a,b,c) find the sum. However, if one of the values is the same as
another, both the numbers do not count towards the sum and the third number is returned
as the sum.
Include a class UserMainCode with a static method getDistinctSum which accepts three
integers and returns integer.
Create a Class Main which would be used to accept three integers and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of three integers.
Output consists of a integer.
Refer sample output for formatting specifications.
Sample Input 1:
1
2
1
Sample Output 1:
2
Sample Input 2:
1
2
3
Sample Output 2:
6
UserMainCode:
public class UserMainCode {
public static int getDistinctSum(int a,int b,int c)
{
int sum;
if(a==b)
{
sum=c;
}
else if(b==c)
{
sum=a;
}
else if(c==a)
{
sum=b;
}
else
{
sum=a+b+c;
}
return sum;
}
Main:
import java.util.*;
public class Main {
UserMainCode
if(a==1&&b==1&&c==1)
{
b1=true;
}
return b1;
}
Main:
import java.util.*;
public class Main {
}
76. String Processing
Write a program to read a string and return a new string where the first and last chars have
been interchanged.
Include a class UserMainCode with a static method exchangeCharacters which accepts
the
string. The return type is the modified string.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of string.
Refer sample output for formatting specifications.
Sample Input 1:
HelloWorld
Sample Output 1:
delloWorlH
UserMainCode
public class UserMainCode {
public static String exchangeCharacters(String s1)
{
String s2=s1.substring(1,s1.length()-1);
StringBuffer sb=new StringBuffer();
char c1=s1.charAt(0);
char c2=s1.charAt(s1.length()-1);
sb.append(c2).append(s2).append(c1);
return sb.toString();
}
}
Main:
import java.util.*;
public class Main {
System.out.println(UserMainCode.exchangeCharacters(s1));
s.close();
}
return b;
}
Main:
import java.util.*;
public class Main {
s.close();
}
Main:
import java.util.*;
public class Main {
79. SumOdd
Write a program to read an integer and find the sum of all odd numbers from 1 to the given
number. [inclusive of the given number]
if N = 9 [ 1,3,5,7,9]. Sum = 25
Include a class UserMainCode with a static method addOddNumbers which accepts the
number n. The return type is the integer based on the problem statement.
Create a Class Main which would be used to accept the integer and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a integer.
Output consists of a integer.
Refer sample output for formatting specifications.
Sample Input 1:
6
Sample Output 1:
9
UserMainCode:
public class UserMainCode {
public static int addOddNumbers(int n)
{
int sum=0;
for(int i=1;i<=n;i+=2)
{
sum=sum+i;
}
return sum;
}
}
Main:
import java.util.*;
public class Main {
return s3;
}
}
Main:
import java.util.*;
public class Main {
81.Unique Number
GIven three integers (a,b,c) , Write a program that returns the number of unique integers
among the three.
Include a class UserMainCode with a static method calculateUnique which accepts three
integers and returns the count as integer.
Create a Class Main which would be used to accept three integers and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of three integers.
Output consists of a integer.
Refer sample output for formatting specifications.
Sample Input 1:
12
4
3
Sample Output 1:
3
Sample Input 2:
4
-4
4
Sample Output 2:
2
Main:
import java.util.*;
public class Main {
public static void main(String[]args){
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
System.out.println(UserMainCode.calculateUnique(a, b, c));
s.close();
}
}
UserMainCode:
public class UserMainCode {
public static int calculateUnique(int a,int b,int c)
{
int d=0;
if(a!=b&&a!=c&&b!=c)
{
d=3;
}
else if(a==b&&a==c&&b==c)
{
d=1;
}
else if((a!=b&&a==c&&b==c) || (a!=b&&a!=c&&b==c))
{
d=2;
}
else if((a==b&&a!=c&&b==c) || (a==b&&a!=c&&b!=c))
{
d=2;
}
else if((a==b&&a==c&&b!=c) || (a!=b&&a==c&&b!=c))
{
d=2;
}
return d;
}}
UserMainCode:
public class UserMainCode {
public static int calculator(int a,int b,char c)
{
int a1=0;
if(c=='*')
{
a1=a*b;
}
else if(c=='+')
{
a1=a+b;
}
else if(c=='-')
{
a1=a-b;
}
else if(c=='/')
{
a1=a/b;
}
else if(c=='%')
{
a1=a%b;
}
return a1;
}}
83. Scores
Write a program to read a integer array of scores, if 100 appears at two consecutive
locations return true else return false.
Include a class UserMainCode with a static method checkScores which accepts the
integer
array. The return type is boolean.
Create a Class Main which would be used to accept the integer array and call the static
method present in UserMainCode.
Input and Output Format:
Input consists of an integer n which is the number of elements followed by n integer values.
Output consists of a string that is either 'TRUE' or 'FALSE'.
Refer sample output for formatting specifications.
Sample Input 1:
3
1
100
100
Sample Output 1:
TRUE
Sample Input 2:
3
100
1
100
Sample Output 2:
FALSE
Main:
import java.util.*;
public class Main {
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
System.out.println(UserMainCode.checkScores(arr, n));
sc.close();
}
}
UserMainCode:
public class UserMainCode {
public static boolean checkScores(int arr[], int n){
boolean b = false;
for(int i=0;i<n-1;i++){
if(arr[i] == 100){
if(arr[i+1] == 100){
b = true;
break;
}
}
}
return b;
}
}
84. ArrayFront
Write a program to read a integer array and return true if one of the first 4 elements in the
array is 9 else return false.
Note: The array length may be less than 4.
Include a class UserMainCode with a static method scanArray which accepts the integer
array. The return type is true / false.
Create a Class Main which would be used to accept the integer array and call the static
method present in UserMainCode.
Input and Output Format:
Input consists of an integer n which is the number of elements followed by n integer values.
Output consists of TRUE / FALSE.
Refer sample output for formatting specifications.
Sample Input 1:
6
1
2
3
4
5
6
Sample Output 1:
FALSE
Sample Input 2:
3
1
2
9
Sample Output 2:
TRUE
Main:
import java.util.*;
int s=sc.nextInt();
for(int i=0;i<s;i++)
a[i]=sc.nextInt();
}
if(UserMainCode.scanArray(a)==true)
System.out.println("TRUE");
else
System.out.println("FALSE");
sc.close();
UserMainCode:
public class UserMainCode {
public static boolean scanArray(int[] a)
{
int u=0,l=0;
boolean b=false;
if(a.length>=4)
l=4;
else
l=a.length;
for(int i=0;i<l;i++)
if(a[i]==9)
u=10;
if(u==10)
b=true;
return b;
}
}
Main:
import java.util.*;
public class Main {
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String[] str=new String[n];
for(int i=0;i<n;i++)
{
str[i]=sc.next();
}
int c=sc.nextInt();
System.out.println(UserMainCode.countWord(n,str,c));
sc.close();
}}
UserMainCode:
public class UserMainCode {
public static int countWord(int n,String str[],int c)
{
int count=0;
for(int i=0;i<str.length;i++)
{
if(str[i].length()==c)
{
count++;
}
}
return count;
}
}
Main:
import java.util.*;
public class Main {
public static void main (String[] args)
{
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
int d=s.nextInt();
System.out.println(UserMainCode.findDistance(a,b,c,d));
s.close();
}
}
UserMainCode:
public class UserMainCode {
public static int findDistance(int a,int b,int c,int d) {
long q=(int)Math.round(Math.sqrt(((a-c)*(a-c))+((b-d)*(b-d))));
return (int) q;
}
}
Main:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
UserMainCode.countWord(s1);
s.close();
}
}
UserMainCode:
import java.util.StringTokenizer;
public class UserMainCode {
public static void countWord(String s1){
StringTokenizer st=new StringTokenizer(s1," ");
int n=st.countTokens();
System.out.println(n);
}
}
UserMainCode:
public class UserMainCode {
public static int getSumMaxMin(int a,int b,int c)
{
int d=0;
if(a<b&&b<c)
{
d=a+c;
}
else if(a<b&&b>c)
{
d=b+c;
}
else if(a>b&&b<c)
{
d=a+b;
}
return d;
}}
MAIN
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println(UserMainCode.convertDecimalToBinary(n));
s.close();
}
}
UserMainCode
public class UserMainCode {
public static long convertDecimalToBinary(int n){
String s1=Integer.toBinaryString(n);
long y=Long.parseLong(s1);
return y;
}
}
90.String Processing - V
Write a program to read a string and also a number N. Form a new string made up of n
repetitions of the last n characters of the String. You may assume that n is between 1 and
the length of the string.
Include a class UserMainCode with a static method returnLastRepeatedCharacters
which
accepts the string and the number n. The return type is the string as per the problem
statement.
Create a Class Main which would be used to accept the string and integer and call the static
method present in UserMainCode.
Input and Output Format:
Input consists of a string and integer.
Output consists of a string.
Refer sample output for formatting specifications.
Sample Input 1:
Hello
2
Sample Output 1:
lolo
Sample Input 2:
Hello
3
Sample Output 2:
Llollollo
MAIN
import java.util.Scanner;
public class Main
{
USERMAINCODE
public class UserMainCode{
public static String returnLastRepeatedCharacters(String s1, int n1)
{
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < n1 ; i++)
sb.append(s1.substring(s1.length()-n1, s1.length()));
return sb.toString();
}
}
Sample Input 2:
72CAB
Sample Output 2:
FALSE
MAIN
import java.util.Scanner;
String s1=s.nextLine();
if(UserMainCode.validateString(s1)==true)
System.out.println("TRUE");
else System.out.println("FALSE");
s.close();
USERMAINCODE
public class UserMainCode {
public static boolean validateString(String s)
{
boolean b=false;
if(s.charAt(0)=='0'||s.charAt(0)=='1'||s.charAt(0)=='2'||s.charAt(0)=='3'||
s.charAt(0)=='4'||s.charAt(0)=='5'||s.charAt(0)=='6'||s.charAt(0)=='7'||s.charAt(0
)=='8'||s.charAt(0)=='9'){
b=false;
}
else
b=true;
return b;
}
}
MAIN
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
System.out.println(UserMainCode.getAlternateChars(s1));
s.close();
}
}
USERMAINCODE
public class UserMainCode{
public static String getAlternateChars(String s)
{
//String s1=s.replaceAll(“ “, “”);
StringBuffer sbf = new StringBuffer();
for(int i = 0; i < s.length() ; i=i+2)
{
sbf.append(s.charAt(i));
}
String str = sbf.toString();
return str;
}
}
MAIN CLASS
import java.util.Scanner;
USERMAINCODE
import java.util.StringTokenizer;
public class UserMainCode {
public static String fetchUserName(String s1) {
StringTokenizer st=new StringTokenizer(s1,"@");
String s2=st.nextToken();
return(s2);
}}
import java.util.Scanner;
String s1=s.nextLine();
String s2=s.nextLine();
int n=s.nextInt();
boolean output=UserMainCode.isEqual(s1,s2,n);
System.out.println(output);
s.close();
USERMAINCODE
boolean a=false;
if(n<s1.length()&&n<s2.length())
char c=s1.charAt(n);
char d=s2.charAt(s2.length()-n);
String s3=Character.toString(c);
//System.out.println(s3);
String s4=Character.toString(d);
//System.out.println(s4);
if(s3.equalsIgnoreCase(s4))
a=true;
}
else
a=false;
return a;
USERMAIN CODE
public class UserMainCode {
public static int checkDifference(int[] n1){
int n2,n3=0,n4=0,i;
for(i=0;i<n1.length-1;i++){
n2=Math.abs(n1[i]-n1[i+1]);
if(n2>n3){
n3=n2;
n4=i+1; }}
return n4;
}
}
1.Start Case
Write a program to read a sentence in string variable and convert the first letter of each
word to capital case. Print the final string.
Note: - Only the first letter in each word should be in capital case in final string.
Include a class UserMainCode with a static method printCapitalized which accepts a
string.
The return type (String) should return the capitalized string.
Create a Class Main which would be used to accept a string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a strings.
Output consists of a String (capitalized string).
Refer sample output for formatting specifications.
Sample Input:
Now is the time to act!
Sample Output:
Now Is The Time To Act!
MAIN CLASS
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
System.out.println(UserMainCode.printCapitalized(s1));
s.close();
}
}
USERMAIN CODE
import java.util.StringTokenizer;
public class UserMainCode{
public static String printCapitalized(String s1){
StringBuffer sb=new StringBuffer();
StringTokenizer t=new StringTokenizer(s1," ");
while(t.hasMoreTokens()){
String s2=t.nextToken();
String s3=s2.substring(0,1);
String s4=s2.substring(1, s2.length());
sb.append(s3.toUpperCase()).append(s4).append(" "); }
return sb.toString();
}
}
2. Maximum Difference
Write a program to read an integer array and find the index of larger number of the two
adjacent numbers with largest difference. Print the index.
Include a class UserMainCode with a static method findMaxDistance which accepts an
integer array and the number of elements in the array. The return type (Integer) should
return index.
Create a Class Main which would be used to accept an integer array and call the static
method present in UserMainCode.
Input and Output Format:
Input consists of n+1 integers, where n corresponds the size of the array followed by n
integers. Output consists of an Integer (index).
Refer sample output for formatting specifications.
Sample Input :
6
4
8
6
1
9
4
Sample Output :
4
[In the sequence 4 8 6 1 9 4 the maximum distance is 8 (between 1 and 9). The function
should return the index of the greatest of two. In this case it is 9 (which is at index 4). output
= 4.]
Main:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[20];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
int max=UserMainCode.findMaxDistance(a);
System.out.println(max);
s.close();
}
UserMainCode:
public class UserMainCode {
static int findMaxDistance(int[] a)
{
int max=0,index=0;
for(int i=0;i<19;i++)
{
int d=Math.abs(a[i]-a[i+1]);
if(d>max)
{
max=d;
if(a[i]>a[i+1])
{
index=i;
}
else
{
index=i+1;
}
}
}
return index;
}
}
3. Palindrome - In Range
Write a program to input two integers, which corresponds to the lower limit and upper limit
respectively, and find the sum of all palindrome numbers present in the range including the
two numbers. Print the sum.
Include a class UserMainCode with a static method addPalindromes which accepts two
integers. The return type (Integer) should return the sum if the palindromes are present,
else return 0.
Create a Class Main which would be used to accept two integer and call the static method
present in UserMainCode.
Note1 : A palindrome number is a number which remains same after reversing its digits.
Note2 : A single digit number is not considered as palindrome.
Input and Output Format:
Input consists of 2 integers, which corresponds to the lower limit and upper limit
respectively.
Output consists of an Integer (sum of palindromes).
Refer sample output for formatting specifications.
Sample Input :
130
150
Sample Output :
272
(131+141 = 272)
Main:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n1=s.nextInt();
int n2=s.nextInt();
System.out.println(UserMainCode.addPalindromes(n1,n2));
s.close();
}
}
UserMainCode:
public class UserMainCode {
public static int addPalindromes(int n1,int n2){
int sum=0;
for(int i=n1;i<=n2;i++){
int r=0,n3=i;
while(n3!=0){
r=(r*10)+(n3%10);
n3=n3/10;
}
if(r==i)
sum=sum+i;
}
return sum;
}
}
4. PAN Card
Write a program to read a string and validate PAN no. against following rules:
1. There must be eight characters.
2. First three letters must be alphabets followed by four digit number and ends with
alphabet
3. All alphabets should be in capital case.
Print “Valid” if the PAN no. is valid, else print “Invalid”.
Include a class UserMainCode with a static method validatePAN which accepts a string.
The
return type (Integer) should return 1 if the string is a valid PAN no. else return 2.
Create a Class Main which would be used to accept a string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a string, which corresponds to the PAN number.
Output consists of a string - "Valid" or "Invalid"
Refer sample output for formatting specifications.
Sample Input 1:
ALD3245E
Sample Output 1:
Valid
Sample Input 2:
OLE124F
Sample Output 2:
Invalid
Main:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
UserMainCode.validatePAN(s1);
s.close();
}
}
UserMainCode:
public class UserMainCode {public static void validatePAN(String s1) {
if(s1.matches("[A-Z]{3}[0-9]{4}[A-Z]{1}"))
{
System.out.println("Valid");
}
else
System.out.println("Invalid");
}
}
5. Fibonacci Sum
Write a program to read an integer n, generate fibonacci series and calculate the sum of
first n numbers in the series. Print the sum.
Include a class UserMainCode with a static method getSumOfNfibos which accepts an
integer n. The return type (Integer) should return the sum of n fibonacci numbers.
Create a Class Main which would be used to accept an integer and call the static method
present in UserMainCode.
Note: First two numbers in a Fibonacci series are 0, 1 and all other subsequent numbers are
sum of its previous two numbers. Example - 0, 1, 1, 2, 3, 5...
Input and Output Format:
Input consists of an integer, which corresponds to n.
Output consists of an Integer (sum of fibonacci numbers).
Refer sample output for formatting specifications.
Sample Input :
5
Sample Output :
7
[0 + 1 + 1 + 2 + 3 = 7]
Main:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println(UserMainCode.getSumOfNfibos(n));
s.close();
}
}
UserMainCode:
public class UserMainCode {
public static int getSumOfNfibos(int n){
int a=-1,b=1,c=0,d=0;
for(int i=0;i<n;i++)
{
c=a+b;
d=d+c;
a=b;
b=c;
}
return d;
}
6.Test Vowels
Write a program to read a string and check if given string contains exactly five vowels in any
order. Print “Yes” if the condition satisfies, else print “No”.
Assume there is no repetition of any vowel in the given string and all characters are
lowercase.
Include a class UserMainCode with a static method testVowels which accepts a string.
The
return type (Integer) should return 1 if all vowels are present, else return 2.
Create a Class Main which would be used to accept a string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of a string (“Yes” or “No”).
Refer sample output for formatting specifications.
Sample Input 1:
acbisouzze
Sample Output 1:
Yes
Sample Input 2:
cbisouzze
Sample Output 2:
No
Main:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
int b=UserMainCode.testVowels(s1);
if(b==1)
System.out.println("Yes");
else
System.out.println("No");
s.close();
}
}
UserMainCode:
public class UserMainCode {
7.Dash Check
Write a program to read two strings and check whether or not they have dashes in the same
places. Print “Yes” if the condition satisfies, else print “No”. Include a class UserMainCode
with a static method compareDashes which accepts two
strings. The return type (Integer) should return 1 if all dashes are placed correctly, else
return 2.
Create a Class Main which would be used to accept two strings and call the static method
present in UserMainCode.
Note: The strings must have exactly the same number of dashes in exactly the same
positions. The strings might be of different length.
Input and Output Format:
Input consists of two strings.
Output consists of a string (“Yes” or “No”).
Refer sample output for formatting specifications.
Sample Input 1:
hi—there-you.
12--(134)-7539
Sample Output 1:
Yes
Sample Input 2:
-15-389
-xyw-zzy
Sample Output 2:
No
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
String s2=s.nextLine();
int p=UserMainCode.compareDashes(s1,s2);
if(p==1)
System.out.println("Yes");
else
System.out.println("No");
s.close();
}
}
import java.util.ArrayList;
8.Reverse Split
Write a program to read a string and a character, and reverse the string and convert it in a
format such that each character is separated by the given character. Print the final string.
Include a class UserMainCode with a static method reshape which accepts a string and a
character. The return type (String) should return the final string.
Create a Class Main which would be used to accept a string and a character, and call the
static method present in UserMainCode.
Input and Output Format:
Input consists of a string and a character.
Output consists of a string (the final string).
Refer sample output for formatting specifications.
Sample Input:
Rabbit
-
Sample Output:
t-i-b-b-a-R
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
String s2=s.next();
System.out.println(UserMainCode.reShape(s1,s2));
s.close();
}
}
public class UserMainCode {
public static String reShape(String s,String s1){
StringBuffer sb=new StringBuffer(s);
StringBuffer sb2=new StringBuffer();
String s2=sb.reverse().toString();
for(int i=0;i<s2.length();i++)
{
sb2.append(s2.charAt(i));
sb2.append(s1);
}
sb2.deleteCharAt(sb2.length()-1);
//System.out.println(sb2.toString());
return sb2.toString();
}
9.Remove 10's
Write a program to read an integer array and remove all 10s from the array, shift the other
elements towards left and fill the trailing empty positions by 0 so that the modified array is
of the same length of the given array.
Include a class UserMainCode with a static method removeTens which accepts the
number
of elements and an integer array. The return type (Integer array) should return the final
array.
Create a Class Main which would be used to read the number of elements and the input
array, and call the static method present in UserMainCode.
Input and Output Format:
Input consists of n+1 integers, where n corresponds to size of the array followed by n
elements of the array.
Output consists of an integer array (the final array).
Refer sample output for formatting specifications.
Sample Input :
5
1
10
20
10
2
Sample Output :
1
20
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i;
int n = sc.nextInt();
int a[] = new int[n];
for (i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
UserMainCode.removeTens(a);
sc.close();
}
}
import java.util.*;
public class UserMainCode {
public static void removeTens(int a[]){
Scanner sc = new Scanner(System.in);
int i,k = 0;
int b[] = new int[a.length];
ArrayList<Integer> al = new ArrayList<Integer>();
for (i = 0; i <a.length; i++) {
if (a[i] != 10) {
al.add(a[i]);
}
}
if (al.size() < a.length) {
k = a.length- al.size();
for (i = 0; i < k; i++) {
al.add(0);
}
}
int b1[] = new int[a.length];
for (i = 0; i < a.length; i++) {
b1[i] = al.get(i);
System.out.println(b1[i]);
}
}}
10.Last Letters
Write a program to read a sentence as a string and store only the last letter of each
word of
the sentence in capital letters separated by $. Print the final string.
Include a class UserMainCode with a static method getLastLetter which accepts a
string.
The return type (string) should return the final string.
Create a Class Main which would be used to read a string, and call the static method
present
in UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of a string (the final string).
Refer sample output for formatting specifications.
Smaple Input :
This is a cat
Sample Output :
S$S$A$T
Main:
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String input=s.nextLine();
System.out.println(UserMainCode.getLastLetter(input));
}
UserMainCode:
import java.util.*;
public class UserMainCode {
public static String getLastLetter(String input){
String str1=null;
StringTokenizer st=new StringTokenizer(input," ");
StringBuffer sb=new StringBuffer();
while(st.hasMoreTokens()){
str1=st.nextToken();
/// String str2=Character.toString(str1.charAt(str1.length()-
1));
String str2=str1.substring(str1.length()-1);
String str3= str2.toUpperCase();
sb.append(str3).append("$");
}sb.deleteCharAt(sb.length()-1);
return sb.toString();
}
}
Write a program that construts a hashmap and returns the value corresponding to the
largest key.
Include a class UserMainCode with a static method getMaxKeyValue which accepts a
string.
The return type (String) should be the value corresponding to the largest key.
Create a Class Main which would be used to accept Input string and call the static
method
present in UserMainCode.
Input and Output Format:
Input consists of 2n+1 values. The first value corresponds to size of the hashmap. The
next n
pair of numbers equals the integer key and value as string.
Output consists of a string which is the value of largest key.
Refer sample output for formatting specifications.
Sample Input 1:
3
12
amron
9
Exide
7
SF
Sample Output 1:
Amron
Main:
import java.util.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer, String>hm=new HashMap<Integer, String>();
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=0;i<n;i++)
{
int a=s.nextInt();
String s1=s.next();
hm.put(a,s1);
}
System.out.println(UserMainode.getMaxKeyValue(hm));
}
}
Hashmap:
import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
public class UserMainode {
public static String getMaxKeyValue(HashMap<Integer, String> hm) {
int max=0;
String s3=null;
Iterator<Integer>itr=hm.keySet().iterator();
while(itr.hasNext())
{
int b=itr.next();
if(b>max)
{
max=b;
s3=hm.get(b);
}
}
return (s3);
}
}
12.All Numbers
Write a program to read a string array and return 1 if all the elements of the array are
numbers, else return -1.
Include a class UserMainCode with a static method validateNumber which accepts a
string
aray. The return type (integer) should be -1 or 1 based on the above rules.
Create a Class Main which would be used to accept Input string array and call the static
method present in UserMainCode.
The string array is said to be valid if all the elements in the array are numbers. Else it is
invalid.
Input and Output Format:
Input consists of an integer specifying the size of string array followed by n strings.
Refer sample output for formatting specifications.
Sample Input 1:
4
123
24.5
23
one
Sample Output 1:
invalid
Sample Input 2:
2
123
24.5
Sample Output 2:
valid
Main:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String[] s1 = new String[n];
for(int i=0;i<n;i++){
s1[i] = s.next();
}
int out=(userMainCode.validateNumber(s1));
System.out.println(out);
}
}
UserMainCode:
class userMainCode{
public static int validateNumber(String[] s1){
int b =0 ,count,out=0;
for(int i=0;i<s1.length;i++){
String s2 = s1[i];
if(s2.matches("[0-9.]{1,}"))
{ count =0;
for(int j=0;j<s2.length();j++)
{
char c = s2.charAt(j);
if(c=='.')
count++;
}
if(count>1)
b=1;
}
else
b=1;
}
if(b==0){
out=1;
}
else out=-1;
return out;
Main:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
}
UserMainCode:
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public class UserMainCode {
14.Max Substring
Write a program to accept two string inputs. The first being a source string and second
one
a delimiter. The source string contains the delimiter at various locations. Your job is to
return the substring with maximum number of characters. If two or more substrings
have
maximim number of characters return the substring which appears first. The size of the
delimiter is 1.
Include a class UserMainCode with a static method extractMax which accepts the
string.
The return type (string) should be the max substring.
Create a Class Main which would be used to accept Input string and call the static
method
present in UserMainCode.
Input and Output Format:
Input consists of a source string and delimiter.
Output consists of a string.
Refer sample output for formatting specifications.
Sample Input 1:
delhi-pune-patna
-
Sample Output 1:
Delhi\
Main:
import java.util.*;
public class Main {
public static void main(String[] args){
}
}
Usermaincode:
import java.util.StringTokenizer;
import java.util.*;
Main:
import java.util.HashMap;
import java.util.Scanner;
public class Main {
UserMainCode:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class UserMainCode {
public static String getCapital(HashMap<String,String>
hm,String sa)
{
String chan=null;
Iterator<String>it=hm.keySet().iterator();
StringBuffer sb=new StringBuffer();
while(it.hasNext()){
String a=it.next();
if(a.equals(sa))
{
chan=hm.get(a);
sb.append(chan).append("$").append(sa);
}
}
return sb.toString();
}
}
Main:
import java.util.*;
public class Main {
}
UserMainCode:
import java.util.*;
public class UserMainCode {
public static int calculateWordSum(String inp) {
int count=0;
String st[]=inp.split(" ");
String s1=st[0];
String slst=st[st.length-1];
if(s1.equals(slst))
{
count=s1.length();
}
else
{
count=s1.length()+slst.length();
}
return count;
}
}
int n;
Scanner sc=new Scanner(System.in);
n=Integer.parseInt(sc.nextLine());
String[] str=new String[n];
for(int i=0;i<n;i++)
{
str[i]=sc.nextLine();
}
ArrayList<String> arr=new ArrayList<String>();
arr=UserMainCode.matchCharacter(str);
Iterator<String> it=arr.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Usermaincode:
package vowels;
import java.util.*;
public class UserMainCode {
Write a program that constructs a hashmap with “employee id” as key and “name” as its
value. Based on the rules below, on being satisfied, the name must be added to the
arraylist.
i)First character should be small and the last character should be Capital.
ii)In name at least one digit should be there.
Include a class UserMainCode with a static method getName which accepts a hashmap.
The
return type is an arraylist as expected in the above statement
Create a Class Main which would be used to accept Input string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of 2n+1 values. The first value corresponds to size of the hashmap. The next n
pair of numbers contains the employee id and name.
Output consists of arraylist of strings as mentioned in the problem statement.
Refer sample output for formatting specifications.
Sample Input 1:
4
1
ravi5raJ
2
sita8gitA
3
ram8sitA
4
rahul
Sample Output 1:
ravi5raJ
sita8gitA
ram8sitA
main:
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) {
HashMap<Integer,String> hm1=new HashMap<Integer,String>();
int n;
Scanner sc=new Scanner(System.in);
n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++)
{
hm1.put(Integer.parseInt(sc.nextLine()),sc.nextLine());
}
ArrayList<String> al1=new ArrayList<String>();
al1=UserMainCode.getName(hm1);
Iterator<String> it=al1.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Usermaincode:
import java.util.*;
import java.text.*;
public class UserMainCode {
public static ArrayList<String> getName(HashMap<Integer,String> hm1)
{
ArrayList<String> al2=new ArrayList<String>();
Iterator<Integer> it =hm1.keySet().iterator();
while(it.hasNext())
{
int id=it.next();
String name=hm1.get(id);
if(name.matches("[a-z]{1,}.*[0-9]{1,}.*[A-Z]{1}"))
al2.add(name);
}
return al2;
}
}
19.Max Admissions
Write a program that reads details about number of admissions per year of a particular
college, return the year which had maximum admissions. The details are stored in an
arraylist with the first index being year and next being admissions count.
Include a class UserMainCode with a static method getYear which accepts a arraylist. The
return type is an integer indicating the year of max admissions.
Create a Class Main which would be used to accept Input string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of 2n+1 values. The first value corresponds to size of the data (year &
admissions). The next n pair of numbers contains the year and admissions count.
Output consists of an integer as mentioned in the problem statement.
Refer sample output for formatting specifications.
Sample Input 1:
4
2010
200000
2011
300000
2012
45000
2013
25000
Sample Output 1:
2011
USERMAINCODE:
import java.util.ArrayList;
int max=0,pos=0;
for(int i=1;i<a1.size();i+=2)
if(a1.get(i)>max)
max=a1.get(i);
pos=i;
return a1.get(pos-1);
MAIN:
import java.util.*;
class Main
n=n*2;
for(int i=0;i<n;i++)
a1.add(s.nextInt());
System.out.println(UserMainCode.year(a1));
s.close();
Write a program to calculate the sum of all the non prime positive numbers less than or
equal to the given number.
Note: prime is a natural number greater than 1 that has no positive divisors other than 1
and itself
Example:
input = 9
Prime numbers = 2,3,5 and 7
output = 1+4+6+8+9=28
Include a class UserMainCode with a static method “addNumbers” that accepts an
integer
arguement and returns an integer.
Create a class Main which would get an integer as input and call the static
method validateNumber present in the UserMainCode.
Input and Output Format:
Input consists of an integer.
Output consists of an integer.
Sample Input:
9
Sample Output:
28
Main:
import java.util.*;
public class Main {
public static void main(String[] args) {
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println(UserMainCode.addNumbers(n));
}
}
}
Usermaincode:
Given a date string in the format dd/mm/yyyy, write a program to convert the given date to
the format dd-mm-yy.
Include a class UserMainCode with a static method “convertDateFormat” that accepts
a
String and returns a String.
Create a class Main which would get a String as input and call the static
method convertDateFormat present in the UserMainCode.
Input and Output Format:
Input consists of a String.
Output consists of a String.
Sample Input:
12/11/1998
Sample Output:
12-11-98
Main:
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String s1=s.next();
UserMainCode.convertDateFormate(s1);
}
Usermaincode:
import java.util.*;
import java.text.*;
public class UserMainCode {
public static void convertDateFormate(String s1) {
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
try {
Date d1=sdf.parse(s1);
SimpleDateFormat sdf1=new SimpleDateFormat("dd-MM-yy");
String s2=sdf1.format(d1);
System.out.println(s2);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
22.Valid Date
Given a date string as input, write a program to validate if the given date is in any of the
following formats:
dd.mm.yyyy
dd/mm/yy
dd-mm-yyyy
Include a class UserMainCode with a static method “validateDate” that accepts a String
and
returns an integer. This method returns 1 if the date is valid, else return -1.
Create a class Main which would get a String as input and call the static
method validateDate present in the UserMainCode.
Input and Output Format:
Input consists of a String.
Output consists of a String that is either 'Valid' or 'Invalid'.
Sample Input 1:
12.03.2012
Sample Output 1:
Valid
Sample Input 2:
27#01#1977
Sample Output 2:
Invalid
UserMainCode:
if(s1.matches("[0-9]{2}[.]{1}[0-9]{2}[.]{1}[0-9]{4}"))
Date d=sdf.parse(s1);
s2=sdf.format(d);
n=1;
else if(s1.matches("[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{2}"))
Date d1=sdf1.parse(s1);
s2=sdf1.format(d1);
n=1;
else if(s1.matches("[0-9]{2}[-]{1}[0-9]{2}[-]{1}[0-9]{4}"))
Date d2=sdf2.parse(s1);
s2=sdf2.format(d2);
n=1;
else
n=-1;
return n;
}
MAIN:
import java.text.ParseException;
import java.util.*;
class Main
String s1=s.next();
int b=UserMainCode.dateformat(s1);
if(b==1)
System.out.println("Valid");
Else
System.out.println("Invalid");
s.close();
23.Convert Format
Given a 10 digit positive number in the format XXX-XXX-XXXX as a string input, write a
program to convert this number to the format XX-XX-XXX-XXX.
Include a class UserMainCode with a static method “convertFormat” that accepts a
String
argument and returns a String.
Create a class Main which would get a String as input and call the static
method convertFormat present in the UserMainCode.
Input and Output Format:
Input consists of a String.
Output consists of a String.
Sample Input:
555-666-1234
Sample Output:
55-56-661-234
Main:
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
System.out.println(UserMainCode.convertFormate(s));
}
Usermaincode:
import java.util.*;
import java.text.*;
public class UserMainCode {
public static String convertFormate(String s) {
StringTokenizer t=new StringTokenizer(s,"-");
String s1=t.nextToken();
String s2=t.nextToken();
String s3=t.nextToken();
StringBuffer sb=new StringBuffer();
sb.append(s1.substring(0, s1.length()-1)).append('-');
sb.append(s1.charAt(s1.length()-1)).append(s2.charAt(0)).append('-');
sb.append(s2.substring(1, s2.length())).append(s3.charAt(0)).append('-');
sb.append(s3.substring(1, s3.length()));
return sb.toString();
}
}
Given an int array and a number as input, write a program to add all the elements in the
array greater than the given number. Finally reverse the digits of the obtained sum and print
it.
Include a class UserMainCode with a static method “addAndReverse” that accepts 2
arguments and returns an integer.The first argument corresponds to the integer array and
the second argument corresponds to the number.
Create a class Main which would get the required input and call the static
method addAndReverse present in the UserMainCode.
Example:
Input Array = {10,15,20,25,30,100}
Number = 15
sum = 20 + 25 + 30 + 100 = 175
output = 571
Input and Output Format:
The first line of the input consists of an integer that corresponds to the number of elements
in the array.
The next n lines of the input consists of integers that correspond to the elements in the
array.
The last line of the input consists of an integer that corresponds to the number.
Output consists of a single integer.
Sample Input
6
10
15
20
25
30
100
15
Sample Output
571
Main:
import java.util.*;
int i=0,sum=0,r=0;
for(i=0;i<a.length;i++)
{
if(a[i]>b)
{
sum=sum+a[i];
}
}
System.out.println(sum);
while(sum!=0)
{
r=((r*10)+(sum%10));
sum=sum/10;
}
return r;
}
}
Given a date string in dd/mm/yyyy format, write a program to calculate the day which falls
on the same date next year. Print the output in small case.
The days are sunday, monday, tuesday, wednesday, thursday, friday and saturday.
Include a class UserMainCode with a static method “nextYearDay” that accepts a String
and
returns a String.
Create a class Main which would get a String as input and call the static
method nextYearDay present in the UserMainCode.
Input and Output Format:
Input consists of a String.
Output consists of a String.
Sample Input:
13/07/2012
Sample Output:
Saturday
Main:
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.next();
UserMainCode u=new UserMainCode();
{
System.out.println(u.nextYearDay(s1));
}
}
}
Usercodemain:
import java.util.*;
import java.text.*;
public class UserMainCode
{
public String nextYearDay(String s1)
{
String s=null;
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
try {
Date d1=sdf.parse(s1);
Calendar cal=Calendar.getInstance();
cal.setTime(d1);
cal.add(Calendar.YEAR, 1);
Date d2=cal.getTime();
SimpleDateFormat sdf1=new SimpleDateFormat("EEEEE");
s=sdf1.format(d2);
}
catch (ParseException e)
{
e.printStackTrace();
}
return s;
}
}
}
}
UserMainCode:-
import java.util.*;
public class UserMainCode {
public static void getSumOfSquaresOfDigits(int n) {
int a=n;
int rem=0;
int sum=0;
while(a!=0)
{
rem=a%10;
sum=sum+(rem*rem);
a=a/10;
}
System.out.println(sum);
}
}
Write a program that accepts a positive number as input and calculates the sum of digits at
even indexes (say evenSum) and sum of digits at odd indexes (say oddSum) in the given
number. If both the sums are equal , print 'yes', else print no.
Example:
input = 23050
evenSum = 2 + 0 + 0 = 2
oddSum = 3 + 5 = 8
output = no
Include a class UserMainCode with a static method “sumOfOddEvenPositioned” that
accepts an integer and returns an integer. The method returns 1 if the 2 sums are equal.
Else the method returns -1.
Create a class Main which would get an integer as input and call the static
method sumOfOddEvenPositioned present in the UserMainCode.
Input and Output Format:
Input consists of an integer.
Output consists of a string that is either “yes” or “no”.
Sample Input 1:
23050
Sample Output 1:
no
Sample Input 2:
231
Sample Output 2:
Yes
Main:-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
UserMainCode.sumOfOddEvenPositioned(n);
sc.close();
}
}
UserMainCode:-
import java.util.*;
public class UserMainCode {
public static void sumOfOddEvenPositioned(int n) {
int rem = 0, i = 0;
int a[] = new int[10];
while (n > 0) {
rem = n % 10;
a[i] = rem;
n = n / 10;
i++;
}
int sume = 0, sumo = 0;
for (int j = i - 1; j >= 0; j--) {
if(j%2!=0)
{
sumo = sumo + a[j];
}
else
{
sume = sume + a[j];
}
}
if (sume == sumo) {
System.out.println("Yes");
} else
System.out.println("No");
}
}
28.Remove 3 Multiples
Write a program that accepts an ArrayList of integers as input and removes every 3rd
element and prints the final ArrayList.
Suppose the given arrayList contains 10 elements remove the 3rd, 6th and 9th elements.
Include a class UserMainCode with a static method “removeMultiplesOfThree” that
accepts
an ArrayList<Integer> as arguement and returns an ArrayList<Integer>.
Create a class Main which would get the required input and call the static
method removeMultiplesOfThree present in the UserMainCode.
Input and Output Format:
The first line of the input consists of an integer n, that corresponds to the number of
elements to be added in the ArrayList.
The next n lines consist of integers that correspond to the elements in the ArrayList.
Output consists of an ArrayList of integers.
Sample Input:
6
3
1
11
19
17
19
Sample Output
3
1
19
17
Main:-
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Main{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
ArrayList<Integer> al=new ArrayList<Integer>();
ArrayList<Integer> al1=new ArrayList<Integer>();
int n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++)
{
al.add(sc.nextInt());
}
al1=UserMainCode.removeMultiplesOfThree(al);
Iterator it=al1.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
UserMainCode:-
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
public class UserMainCode
{
public static ArrayList<Integer> removeMultiplesOfThree(ArrayList<Integer> al)
{
ArrayList<Integer> al2=new ArrayList<Integer>();
for(int i=0;i<al.size();i++)
{
if((i+1)%3!=0)
al2.add(al.get(i));
}
return al2;
}
}
29.String Occurances - II
Obtain two strings S1,S2 from user as input. Your program should count the number of
Include a class UserMainCode with a static method getSubstring which accepts two string
Create a Class Main which would be used to accept two Input strings and call the static
Sample Input 1:
catcowcat
cat
Sample Output 1:
Sample Input 2:
catcowcat
CAT
Sample Output 2:
Main:-
MAIN
import java.util.Scanner;
public class Main {
String s=sc.nextLine();
String s1=sc.nextLine();
System.out.println(UserMainCode.getSubstring(s, s1));
sc.close();
USERMAINCODE
int t=s1.length();
int count=0;
for(int i=0;i<s.length()-t+1;i++)
String s3=s.substring(i,t+i);
if(s3.equals(s1))
count++;
return count;
}
30. Programming Logic
Write a Program that accepts three integer values (a,b,c) and returns their sum. However, if
one of the values is 13 then it does not count towards the sum and the next number also
does not count. So for example, if b is 13, then both b and c do not count.
Include a class UserMainCode with a static method getLuckySum which accepts three
Create a Class Main which would be used to accept the input integers and call the static
Sample Input 1:
Sample Output 1:
Sample Input 2:
13
Sample Output 2:
Sample Input 3:
13
3
Sample Output 3:
Main:-
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
System.out.println(UserMainCode.luckySum(a,b,c));
}
}
UserMainCode:-
public class UserMainCode{
public static int luckySum(int a, int b, int c)
{
if(a == 13)
return 0;
if(b == 13)
return a;
if(c == 13)
return (a + b);
return (a + b + c);
}
}
31.Triplets
Given an integer array, Write a program to find if the array has any triplets. A triplet is a
array. The return type is boolean stating whether its a triplet or not.
Create a Class Main which would be used to accept the input arrayand call the static method
present in UserMainCode.
Input consists of n+1 integers. The first integer would represent the size of array and the
Sample Input 1:
Sample Output 1:
TRUE
Sample Input 2:
2
3
Sample Output 2:
FALSE
Main:-
import java.util.Scanner;
int n;
n=sc.nextInt();
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
boolean s=UserMainCode.checkTripplets(a);
if(s==true)
System.out.println("TRUE");
else
System.out.println("FALSE");
UserMainCode:-
import java.util.*;
{
boolean b=false;
for(int i=0;i<a.length-2;i++)
if((a[i]==a[i+1])&&(a[i+1]==a[i+2]))
b=true;
return b;
32.Repeat Front
Given a string (s) and non negative integer (n) apply the following rules.
2. If the length of the string is less than 3, then consider the entire string as front and
repeat it n times.
accepts the string and integer. The return type is the string formed based on rules.
Create a Class Main which would be used to accept the string and integer and call the static
Sample Input 1:
Coward
Sample Output 1:
CowCow
Sample Input 2:
So
Sample Output 2:
SoSoSo
Main:-
import java.util.*;
String s=sc.nextLine();
int n=Integer.parseInt(sc.nextLine());
System.out.println(UserMainCode.repeatFirstThreeCharacters(s,n));
sc.close();
UserMaincode:-
import java.util.*;
{
StringBuffer sb=new StringBuffer();
if(s.length()>3)
{ sb.append(s.substring(0,3));
s=sb.toString();
for(int i=0;i<n;i++)
sb1.append(s);
return sb1.toString();
33.Sorted Array
Write a program to read a string array, remove duplicate elements and sort the array.
Note:
1. The check for duplicate elements must be case-sensitive. (AA and aa are NOT
duplicates)
2. While sorting, words starting with upper case letters takes precedence.
Include a class UserMainCode with a static method orderElements which accepts the
string
Create a Class Main which would be used to accept the string arrayand integer and call the
Input consists of an integer n which is the number of elements followed by n string values.
AAA
BBB
AAA
AAA
CCC
CCC
Sample Output 1:
AAA
BBB
CCC
Sample Input 2:
AAA
BBB
aaa
AAA
Abc
Sample Output 2:
AAA
Abc
BBB
aaa
Main:-
import java.util.*;
int n;
n = sin.nextInt();
for(int i=0;i<n;i++)
a1[i] = sin.next();
a1 = UserMainCode.orderElements(a1);
for(int i=0;i<a1.length;i++)
System.out.println(""+a1[i]);
UserMainCode:-
import java.util.*;
public class UserMainCode
for(int i=0;i<arr.length;i++)
al.add(arr[i]);
Iterator<String> itr=al.iterator();
int i =0 ;
while(itr.hasNext()){
ar[i] = itr.next();
i++;
Arrays.sort(ar);
return ar;
34.Pattern Matcher
Write a program to read a string and check if it complies to the pattern 'CPT-XXXXXX' where
XXXXXX is a 6 digit number. If the pattern is followed, then print TRUE else print FALSE.
Include a class UserMainCode with a static method CheckID which accepts the string. The
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Input and Output Format:
Sample Input 1:
CPT-302020
Sample Output 1:
TRUE
Sample Input 2:
CPT123412
Sample Output 2:
FALSE
Main:
import java.util.*;
String s = sc.next();
System.out.println(UserMainCode.CheckID(s));
sc.close();
}}
UserMainCode:
boolean b=false;
if(s.matches("(CPT)[-]{1}[0-9]{6}"))
b=true;
else
b=false;
return b;
Given a string array and non negative integer (n) apply the following rules.
1. Pick nth character from each String element in the String array and form a new String.
2. If nth character not available in a particular String in the array consider $ as the character.
Include a class UserMainCode with a static method formString which accepts the string
and
Create a Class Main which would be used to accept the string and integer and call the static
Input consists of a an integer which denotes the size of the array followed by the array of
Sample Input 1:
ABC
XYZ
EFG
MN
Sample Output 1:
CZG$
Main:
import java.util.Scanner;
int n=Integer.parseInt(s.nextLine());
for(int i=0;i<n;i++)
sc[i]=s.nextLine();
int a=Integer.parseInt(s.nextLine());
System.out.println(UserMainCode.formString(n,sc,a));
s.close();
}
UserMainCode:
for(int i=0;i<n;i++)
if(input[i].length()>=a)
String a1=input[i];
sb.append(a1.charAt(a-1));
else
sb.append('$');
return sb.toString();
}}
36.Regular Expression - 1
If all the conditions are satisifed then print TRUE else print FALSE.
Include a class UserMainCode with a static method validate which accepts the string. The
present in UserMainCode.
Sample Input 1:
vR4u
Sample Output 1:
TRUE
Sample Input 2:
vRau
Sample Output 2:
FALSE
Sample Input 3:
vrau
Sample Output 3:
FALSE
S.36) import java.util.Scanner;
String n=sc.nextLine();
System.out.println(UserMainCode.validate(n));
sc.close();
}
public class UserMainCode
String w="FALSE";
if(s.length()==4 &&
(Character.isDigit(s.charAt(0))||Character.isAlphabetic(s.charAt(0)))&&s.charAt(1)
=='R')
if(Character.isDigit(s.charAt(2)))
w="TRUE";
return w;
Given the age of a person as string, validate the age based on the following rules.
If all the conditions are satisifed then print TRUE else print FALSE.
Include a class UserMainCode with a static method ValidateAge which accepts the string.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
23
Sample Output 1:
TRUE
Sample Input 2:
-34
Sample Output 2:
FALSE
Sample Input 3:
3a
Sample Output 3:
FALSE
AcB/TRUE
Main:
import java.util.*;
String n=s.nextLine();
boolean b=UserMainCode.ValidateAge(n);
if(b==true)
System.out.println("TRUE");
else
System.out.println("FALSE");
s.close();
UserMainCode:
boolean b = false;
if(n.matches("[0-9]{2}"))
{ //Regular Expression – 2
(Age Validator) pg.No:150
int a=Integer.parseInt(n);
if(a>0&&a>=21&&a<=45)
b=true;
else
b=false;
return b;
}}
Given a phone number as string, validate the same based on the following rules.
Include a class UserMainCode with a static method validatePhone which accepts the
string.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
9987684321
Sample Output 1:
TRUE
Sample Input 2:
0014623452
Sample Output 2:
FALSE
Main:
import java.util.*;
public class Main {
public static void main(String[]args){
Scanner s=new Scanner(System.in);
String s1=s.nextLine();
boolean b1=UserMainCode.validatePhone(s1);
if(b1==true)
{ //phone validation pg.no:151
System.out.println("TRUE");
}
else
{
System.out.println("FALSE");
}
s.close();
}
}
UserMainCode:
39.String Splitter
Write a program which would accept a string and a character as a delimiter. Apply the
below rules
1. Using the delimiter, split the string and store these elements in array.
Include a class UserMainCode with a static method manipulateLiteral which accepts the
string and character. The return type is the string array formed.
Create a Class Main which would be used to accept the string and characterand call the
Sample Input 1:
AAA/bba/ccc/DDD
Sample Output 1:
aaa
abb
ccc
ddd
import java.util.*;
String ip1=s.next();
char ip2='/';
String op[]=UserMainCode.manipulateLiteral(ip1,ip2);
for(int i=0;i<op.length;i++)
System.out.println(op[i]);
s.close();
}}
import java.util.ArrayList;
import java.util.StringTokenizer;
while(t1.hasMoreTokens())
sb.append(t1.nextToken().toLowerCase());
lst.add(sb.reverse().toString());
for(int i = 0;i<lst.size();i++)
op[i] = lst.get(i);
return op;
40.Vowel Count
Write a program to read a string and count the number of vowels present in it.
Include a class UserMainCode with a static method tellVowelCount which accepts the
string. The return type is the integer giving out the count of vowels.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
NewYork
Sample Output 1:
Sample Input 2:
Elephant
Sample Output 2:
3
import java.util.*;
String s=sc.nextLine();
int max=UserMainCode.tellVowelCount(s);
System.out.println(max);
sc.close();
int max=0;
int count=0;
for(int i=0;i<s.length();i++)
char c=s.charAt(i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||
c=='O'||c=='U')
count++;
if(count>max)
{
max=count;
return max;
Write a program to accept a string array as input, convert all the elements into lowercase
Include a class UserMainCode with a static method sortArray which accepts the string
array.
Create a Class Main which would be used to accept the string array and call the static
Input consists of a an integer which denotes the size of the array followed by the array of
strings,
Sample Input 1:
AAA
BB
CCCC
ABCDE
Sample Output 1:
aaa
abcde
bb
cccc
import java.util.*;
int n=s.nextInt();
s1[i]=s.next();
s2=UserMainCode.sortArray(s1,n);
System.out.println(s2[i]);
s.close();
}}
import java.util.Arrays;
s2[i]=s1[i].toLowerCase();
Arrays.sort(s2);
return s2;
42.Median Calculation
Write a program to accept an int array as input, and calculate the median of the same.
2. The total number count is odd, Median will be the middle number.
The total number count is even, Median will be the average of two middle numbers, After
Include a class UserMainCode with a static method calculateMedian which accepts the int
array. The return type is the integer which would be the median.
Create a Class Main which would be used to accept the integer array and call the static
Input consists of a an integer which denotes the size of the array followed by the array of
integers.
Sample Input 1:
7
Sample Output 1:
Sample Input 2:
52
51
81
84
60
88
Sample Output 2:
71
Main
import java.util.*;
{
int n;
n = sin.nextInt();
for(int i=0;i<n;i++)
a1[i] = sin.nextInt();
System.out.println(""+UserMainCode.calculateMedian(a1));
sin.close();
UserMainCode
import java.util.Arrays;
Arrays.sort(a);
int result=0,mid=0,midNext=0;
if((length%2) != 0)
mid = (length/2)+1;
result = a[mid];
else
mid = length/2;
midNext = mid+1;
result = Math.round(div);
return result;
43.Sequence in Array
Write a program to accept an int array as input, and check if [1,2,3] appears somewhere in
Include a class UserMainCode with a static method searchSequence which accepts the
int
Create a Class Main which would be used to accept the integer array and call the static
Input consists of a an integer which denotes the size of the array followed by the array of
integers.
Sample Input 1:
11
-2
5
1
Sample Output 1:
TRUE
Sample Input 2:
-2
Sample Output 2:
FALSE
Main
import java.util.*;
int n=s.nextInt();
a[i]=s.nextInt();
System.out.println(UserMainCode.searchsequence(a));
s.close();
UserMainCode
public class UserMainCode {
boolean b = false;
b = true;
return b;
Write a program to read a string and return true or false based on the below rule:
1. Return true if for every '*' in the string, there are same characters both side immediately
string. The return type is the boolean TRUE or FALSE based on the rule.
present in UserMainCode.
Sample Input 1:
Hello*World
Sample Output 1:
FALSE
Sample Input 2:
Welcome*elizabeth
Sample Output 2:
TRUE
Main
import java.util.*;
String input=s.next();
UserMainCode
import java.util.StringTokenizer;
public class UserMainCode {
String s1=t.nextToken();
String s2=t.nextToken();
String s3=s1.substring(s1.length()-1);
String s4=s2.substring(0,1);
if(s3.equalsIgnoreCase(s4))
b=true;
return b;
45.Occurance Count
Write a program to read a string that contains a sentence and read a word. Check the
Include a class UserMainCode with a static method countWords which accepts the two
Create a Class Main which would be used to accept the two strings and call the static
world
Sample Output 1:
Sample Input 2:
hello world
World
Sample Output 2:
Main
import java.util.*;
String s1=s.nextLine();
String s2=s.nextLine();
int v=UserMainCode.countWords(s1,s2);
System.out.println(v);
s.close();
UserMainCode
import java.util.StringTokenizer;
int c=0;
while(t.hasMoreTokens())
String s3=t.nextToken();
if(s3.equals(s2))
c++;
return c;
Write a program to read two strings S1 & S2, compute the number of times that S2 appears
in S1.
Include a class UserMainCode with a static method searchString which accepts the two
Create a Class Main which would be used to accept the two strings and call the static
Sample Input 1:
Catcowcat
cat
Sample Output 1:
2
Sample Input 2:
Catcowcat
catp
Sample Output 2:
Main
import java.util.Scanner;
String s1=s.next();
String s2=s.next();
int v=UserMainCode.searchString(s1,s2);
System.out.println(v);
s.close();
UserMainCode
public class UserMainCode {
int c=0;
int t=s2.length();
for(int i=0;i<s1.length()-t+1;i++){
if(s2.equals(s1.substring(i,t+i))){
c++;
}
return c;
47.Strings Processing
Write a program to read a string that contains comma separated fruit names and also a
number N. Pick the nth fruit and return it. If the total number of elements are less than the
Include a class UserMainCode with a static method findFruitName which accepts the the
string and the number n. The return type is the string which has the fruit name.
Create a Class Main which would be used to accept the string and integer and call the static
Sample Input 1:
Apple,Banana,Orange
Sample Output 1:
Banana
Sample Input 2:
Apple,Banana,Orange
Sample Output 2:
Orange
Main
import java.util.Scanner;
str=sc.nextLine();
int n=sc.nextInt();
System.out.println(k);
sc.close();
UserMainCode
import java.util.StringTokenizer;
int i=0;
String h=null;
int max=st.countTokens();
while(st.hasMoreElements())
ss[i++]=st.nextToken();
}
if(n>max)
h=ss[i-1];
else
h=ss[n-1];
return h;
48.Proper Case
Write a program to read a string and convert the intial letter of each word to uppercase.
Include a class UserMainCode with a static method changeCase which accepts the string.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
Sample Output 1:
import java.util.*;
String s1=s.nextLine();
System.out.println(UserMainCode.changeCase(s1));
s.close();
UserMainCode
import java.util.StringTokenizer;
while(t.hasMoreTokens()){
String s2=t.nextToken();
String s3=s2.substring(0,1);
s5.append(s3.toUpperCase()).append(s4).append(" ");
return s5.toString();
Write a program to read a string containing multiple words find the first and last words, if
they are same, return the length and if not return the sum of length of the two words.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
Sample Output 1:
11
Sample Input 2:
Sample Output 2:
Main
import java.util.*;
String s1=sc.nextLine();
System.Out.Println(UserMainCode.compareLastWords(s1));
sc.close();
UserMainCode
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
while(t.hasMoreTokens())
String s2=t.nextToken();
l.add(s2);
String s3=l.get(0);
String s4=l.get(l.size()-1);
if(s3.equals(s4))
int n=s3.length();
System.out.println(n);
else
int n1=s3.length();
int n2=s4.length();
int n=n1+n2;
Return n;
50.Perfect Number
Write a program to that takes a positive integer and returns true if the number is perfect
number.
A positive integer is called a perfect number if the sum of all its factors (excluding the
For example, the number 6 is perfect because its proper divisors are 1, 2, and 3, and
6=1+2+3; but the number 10 is not perfect because its proper divisors are 1, 2, and 5, and
Include a class UserMainCode with a static method getPerfection which accepts the
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
28
Sample Output 1:
TRUE
import java.util.*;
int n=s.nextInt();
boolean j=(UserMainCode.getPerfection(n));
if(j==true)
System.out.println("TRUE");
else
System.out.println("FALSE");
boolean b=false;
int sum=0;
for(int i=1;i<n;i++){
int r=n%i;
if(r==0)
sum=sum+i;
b=(sum==n);
return b;
51.Find Digits
For a given double number with atleast one decimal value, Write a program to compute the
number of digits before and after the decimal point in the following format –
noOfDigitsBeforeDecimal:noOfDigitsAfterDecimal.
Note: Ignore zeroes at the end of the decimal (Except if zero is the only digit after decimal.
Include a class UserMainCode with a static method findNoDigits which accepts the
decimal
value. The return type is string.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
843.21
Sample Output 1:
3:2
Sample Input 2:
20.130
Sample Output 2:
2:2
Sample Input 3:
20.130
import java.util.*;
double d=s.nextDouble();
System.out.println(UserMainCode.findNoDigits(d));
}}
import java.util.StringTokenizer;
int n1=0,n2=0;
String s=String.valueOf(d);
String s1=t.nextToken();
String s2=t.nextToken();
n1=s1.length();
n2=s2.length();
if(s1.charAt(0)=='0')
n1=s1.length()-1;
if(n2!=1)
if(s2.charAt(s2.length()-1)=='0')
n2=s2.length()-1;
String s3=String.valueOf(n1)+":"+String.valueOf(n2);
return s3;
A Company wants to obtain employees of a particular designation. You have been assigned
as the programmer to build this package. You would like to showcase your skills by creating
Read Employee details from the User. The details would include name and designaton in
the given order. The datatype for name and designation is string.
Build a hashmap which contains the name as key and designation as value.
You decide to write a function obtainDesignation which takes the hashmap and
designation as input and returns a string List of employee names who belong to that
Create a Class Main which would be used to read employee details in step 1 and build the
Input consists of employee details. The first number indicates the size of the employees. The
next two values indicate the employee name employee designation. The last string would
Sample Input 1:
Manish
MGR
Babu
CLK
Rohit
MGR
Viru
PGR
MGR
Sample Output 1:
Manish
Rohit
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Scanner;
int k1=Integer.parseInt(sc.nextLine());
for(int i=0;i<k1;i++)
String k=sc.nextLine();
String s=sc.nextLine();
hm.put(k,s);
String n=sc.nextLine();
hm1=UserMainCode.obtainDesignation(hm,n);
Iterator<String> it=hm1.keySet().iterator();
while(it.hasNext())
{
String s2=it.next();
System.out.println(s2);
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
int k=0;
Iterator<String>it=h1.keySet().iterator();
while(it.hasNext())
String s2=it.next();
String s3=h1.get(s2);
if(s3.equals(n))
hm1.put(s2,s3);
return hm1;
}}
A School wants to give assign grades to its students based on their marks. You have been
assigned as the programmer to automate this process. You would like to showcase your
skills by creating a quick prototype. The prototype consists of the following steps:
Read student details from the User. The details would include name, mark in the given
You decide to build a hashmap. The hashmap contains name as key and mark as value.
BUSINESS RULE:
Store the result in a new Hashmap with name as Key and grade as value.
4. You decide to write a function calculateGrade which takes the above hashmap as input
and returns the hashmap as output. Include this function in class UserMainCode.
Create a Class Main which would be used to read student details in step 1 and build the
Input consists of student details. The first number indicates the size of the students. The
Sample Input 1:
3
Avi
76.36
Sunil
68.42
Raja
36.25
Sample Output 1:
Avi
PASS
Sunil
PASS
Raja
FAIL
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Scanner;
int k1=Integer.parseInt(sc.nextLine());
for(int i=0;i<k1;i++)
{
String k=sc.nextLine();
String s=sc.nextLine();
hm.put(k,s);
String n=sc.nextLine();
hm1=UserMainCode.obtainDesignation(hm,n);
Iterator<String> it=hm1.keySet().iterator();
while(it.hasNext())
String s2=it.next();
System.out.println(s2);
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
int k=0;
Iterator<String>it=h1.keySet().iterator();
while(it.hasNext())
String s2=it.next();
String s3=h1.get(s2);
if(s3.equals(n))
hm1.put(s2,s3);
return hm1;
}}
Write a program to validate the Date of Birth given as input in String format (MM/dd/yyyy)
as per the validation rules given below. Return true for valid dates else return false.
2. month should be between 1-12, date should be between 1-31 and year should be a four
digit number.
Include a class UserMainCode with a static method ValidateDOB which accepts the string.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
12/23/1985
Sample Output 1:
TRUE
Sample Input 2:
31/12/1985
Sample Output 2:
FALSE
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
str=sc.nextLine();
Boolean b=UserMainCode.ValidateDOB(str);
if(b==”true”)
System.out.println("TRUE");
if(b==”false”)
System.out.println("FALSE");
import java.text.SimpleDateFormat;
import java.util.Date;
sdf.setLenient(false);
try
Date d1=sdf.parse(str);
return b=”true”;
catch(Exception e)
return b=”false”;
55.Experience Validator
An employee who has recently joined the organization provides his year of passing and
total number of years of experience in String format. Write code to validate his experience
1) Input consists of two String first represent the year of passed out and the second string
2) create a function with name validateExp which accepts two string as input and boolean
as output.
3) The difference between current year and year of pass should be more than or equal to
Experience
Create a Class Main which would be used to accept the boolean and call the static method
present in UserMainCode.
Sample Input:
2001
Sample Output:
TRUE
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
String s=sc.nextLine();
String s1=sc.nextLine();
System.out.println(UserMainCode.validateExp(s,s1));
import java.util.Calendar;
import java.util.Date;
int y1=Integer.parseInt(s);
Calendar c=Calendar.getInstance();
int y2=c.get(Calendar.YEAR);
int y=Math.abs(y1-y2);
int e=Integer.parseInt(s1);
if(y>=e)
return true;
else
return false;
}}
The function should sort the elements (strings) present in the arraylist and convert them
Create a Class Main which would be used to read n strings and call the static method
present in UserMainCode.
Input consists of n+1 integers. The first integer denotes the size of the arraylist, the next n
strings are values to the arraylist.
Sample Input 1:
Sample Output 1:
import java.util.*;
int n=s.nextInt();
for(int i=0;i<n;i++)
l.add(s.next());
}
a=UserMainCode.convertToStringArray(l);
for(int j=0;j<n;j++)
System.out.println(a[j]);
import java.util.ArrayList;
import java.util.Collections;
class UserMainCode
Collections.sort(l);
return a;
57.State ID generator
Include UserMainCode Class With static method getStateId which accepts String array and
return a hashmap.
Create a Class Main which would be used to read n strings and call the static method
present in UserMainCode.
Sample Input 1:
Kerala
Gujarat
Goa
Sample Output 1:
KER:Kerala
GUJ:Gujarat
GOA:Goa
Main Class
import java.util.*;
int n=s.nextInt();
String[] s1=new String[n];
for(int i=0;i<n;i++)
s1[i]=s.next();
hm = UserMainCode.putvalues(s1);
System.out.println(ans.getKey()+":"+ans.getValue());
}}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
lst1.add(s.toUpperCase().substring(0,3));
for(String s : s1)
lst2.add(s);
for(int i=0;i<s1.length;i++)
hm.put(lst1.get(i),lst2.get(i));
return hm;
6.Remove all fruits whose name ends with 'a' or 'e' from first arrayList and remove all fruits
whose name begins with 'm' or 'a' from second arrayList then combine the two lists and
7.If the array is empty the program will print as “No fruit found”
Include a class UserMainCode with the static method fruitSelector which accepts the two
Create a Class Main which would be used to read n strings and call the static method
present in UserMainCode.
Input consists of an integer (m) denoting the size of first arraylist. The next m elements
would be the values of the first arraylist. The next input would be n denoting the size of the
second arraylist. The next n elements would be the values of the second arraylist.
Output consists of an array as per step 6. Refer sample output for formatting specifications.
Sample Input 1:
Apple
Cherry
Grapes
Orange
Mango
Melon
Apple
Sample Output 1:
Cherry
Grapes
Orange
USERMAINCODE:
import java.util.ArrayList;
import java.util.*;
for(int i=0;i<a1.size();i++)
String s1=a1.get(i);
if(s1.charAt(s1.length()-1)!='a'&&s1.charAt(s1.length()-1)!='e'&&s1.charAt(s1.length()-
1)!='A'&&s1.charAt(s1.length()-1)!='E')
a3.add(s1);
for(int j=0;j<a2.size();j++)
String s2=a2.get(j);
if(s2.charAt(0)!='m'&&s2.charAt(0)!='a'&&s2.charAt(0)!='M'&&s2.charAt(0)!='A')
a4.add(s2);
a3.addAll(a4);
Collections.sort(a3);
for(int k=0;k<a3.size();k++)
st[k]=a3.get(k);
return st;
}
MAIN:
import java.util.*;
import java.util.ArrayList;
int m=s.nextInt();
for(int i=0;i<m;i++)
aa1.add(s.next());
int n=s.nextInt();
for(int j=0;j<n;j++)
aa2.add(s.next());
int k;
String st[]=UserMainCode.fruitSelector(aa1,aa2);
for( k=0;k<st.length;k++)
System.out.println(st[k]);
if(st.length==0)
System.out.println("No Fruit Found");
s.close();
59)Elements in ArrayList
Use Collection Methods.
Write a program that takes two ArrayLists as input and finds out all elements present either
in A or B, but not in both.
Include a class UserMainCode with the static method arrayListSubtractor which accepts the
two arraylists and returns an array.
Create a Class Main which would be used to read the inputs and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of an integer (m) denoting the size of first arraylist. The next m elements
would be the values of the first arraylist. The next input would be n denoting the size of the
second arraylist. The next n elements would be the values of the second arraylist.
Output consists of an array. The elements in the output array need to be printed in sorted
order.
Refer sample output for formatting specifications.
Sample Input 1:
4
1
8
3
5
2
3
5
Sample Output 1:
1
8
Sample Input 2:
4
9
1
3
5
4
1
3
5
6
Sample Output 2:
6
9
MAIN:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n,m;
Scanner sin = new Scanner(System.in);
n = sin.nextInt();
ArrayList<Integer> a1 = new ArrayList<Integer>(n);
for(int i=0;i<n;i++)
{
int k = sin.nextInt();
a1.add(k);
}
m = sin.nextInt();
ArrayList<Integer> a2 = new ArrayList<Integer>(m);
for(int i=0;i<m;i++)
{
int k = sin.nextInt();
a2.add(k);
}
int[] result = UserMainCode.arrayListSubtractor(a1,a2);
Arrays.sort(result);
for(int i=0;i<result.length;i++)
System.out.println(result[i]);
}
}
USERMAINCODE:
import java.util.ArrayList;
public class UserMainCode
{
public static int[] arrayListSubtractor(ArrayList<Integer>
arrlist1,ArrayList<Integer>
arrlist2)
{
int count=0,key;
int max = arrlist1.size();
if(arrlist1.size() < arrlist2.size())
max = arrlist2.size();
ArrayList<Integer> temp = new ArrayList<Integer>(max);
for(int i=0;i<arrlist1.size();i++)
{
key = (int)arrlist1.get(i);
if(arrlist2.indexOf(key) == -1)
{
++count;
temp.add(key);
}
}
for(int i=0;i<arrlist2.size();i++)
{
key = (int)arrlist2.get(i);
if(arrlist1.indexOf(key) == -1)
{
if(!temp.contains(key))
{
++count;
temp.add(key);
}
}
}
int[] result = new int[count];
for(int i=0;i<count;i++)
result[i] = (int)temp.get(i);
return result;
}
}
60.Price Calculator - II
Write a small price calculator application with the below mentioned flow:
1. Read a value n indicating the total count of devices. This would be followed by the name
and price of the device. The datatype for name would be String and price would be float.
2. Build a hashmap containing the peripheral devices with name as key and price as value.
3. Read a value m indicating the number of devices for which the price has to be calculated.
This would be followed by device names.
4. For each devices mentioned in the array calcuate the total price.
5. You decide to write a function costEstimator which takes the above hashmap and array as
input and returns the total price (float) as output with two decimal points. Include this
function in class UserMainCode.
Create a Class Main which would be used to read details in step 1 and build the hashmap.
Call the static method present in UserMainCode.
Input and Output Format:
Input consists of device details. The first number indicates the size of the devices. The next
two values indicate the name,price.
This would be followed by m indicating the size of the device array. The next m values would
be the device names.
Output consists of the total price in float.
Refer sample output for formatting specifications.
Sample Input 1:
3
Monitor
1200.36
Mouse
100.42
Speakers
500.25
2
Speakers
Mouse
Sample Output 1:
600.67
MAIN:
import java.util.HashMap;
import java.util.Scanner;
USERMAINCODE:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
62.Leap Year
Write a program to read a string containing date in DD/MM/YYYY format and check if its a
leap year. If so, return true else return false.
Include a class UserMainCode with a static method isLeapYear which accepts the string.
The
return type is the boolean indicating TRUE / FALSE.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of TRUE / FALSE.
Refer sample output for formatting specifications.
Sample Input 1:
23/02/2012
Sample Output 1:
TRUE
Sample Input 2:
12/12/2011
Sample Output 2:
FALSE
MAIN:
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException, ParseException {
Scanner S=new Scanner(System.in);
String s1=S.next();
UserMainCode.isLeapyear(s1);
}
}
USERMAINCODE:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
Sample Input 1:
6
4
2
1
4
5
7
Sample Output 1:
4
MAIN:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.print(UserMainCode.getLargestSpan(a,n));
}}
USERMAINCODE:
public class UserMainCode {
public static int getLargestSpan(int[] x,int n)
{
int gap=0,max=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(x[i]==x[j])
{
gap=j;
}
}
if(gap-i>max)
max=gap-i;
}
return max+1;
}
}
MAIN:
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println(UserMainCode.sumElements(a));
}}
USERMAINCODE:
import java.util.Iterator;
import java.util.LinkedHashSet;
public class UserMainCode {
public static int sumElements(int a[])
{
LinkedHashSet<Integer>h1=new LinkedHashSet<Integer>();
int s=0;
for(int i=0;i<a.length;i++)
{
h1.add(a[i]);
}
Iterator<Integer> it=h1.iterator();
while(it.hasNext())
{
int k=it.next();
if(k%2==0)
{
s=s+k;
}
}
if(s>0)
return s;
else
return -1;
}
}
}
UserMainCode;
import java.util.LinkedHashMap;
Main;
import java.util.Scanner;
import java.util.Scanner;
}
UserMainCode;
import java.util.Iterator;
import java.util.LinkedHashSet;
Main;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println(UserMainCode.checkPalindrome(s));
sc.close();
}
}
UserMainCode;
import java.util.Iterator;
import java.util.LinkedHashSet;
}
UserMainCode;
4. Longest Word
Write a Program which finds the longest word from a sentence. Your program should read a
sentence as input from user and
return the longest word. In case there are two words of maximum length return the word
which comes first in the sentence.
Include a class UserMainCode with a static method getLargestWord which accepts a string
The return type is the longest
word of type string.
Create a Class Main which would be used to accept two Input strings and call the static
method present in UserMainCode.
Input and Output Format:
Input consists of a string with maximum size of 100 characters.
Output consists of a single string.
Refer sample output for formatting specifications.
Sample Input 1:
Welcome to the world of Programming
Sample Output 1:
Programming
Sample Input 2:
ABC DEF
Sample Output 2:
ABC
Main;
import java.util.Scanner;
import java.util.StringTokenizer;
5. String Occurences
Obtain two strings from user as input. Your program should count the number of occurences of second word of second
Include a class UserMainCode with a static method countNoOfWords which accepts two string variables. The return type is
Create a Class Main which would be used to accept two Input strings and call the static method present in UserMainCode.
Sample Input 1:
abc bcd abc bcd abc abc
av abc
Sample Output 1:
Sample Input 2:
w abc
Sample Output 2:
UserMainCode
import java.util.StringTokenizer;
int count=0;
String s3=st.nextToken();
String s4=st.nextToken();
//System.out.println(s4);
while(st1.hasMoreTokens())
String s5=st1.nextToken();
if(s4.equals(s5))
{
count++;
System.out.println(count);
Main
import java.util.*;
/**
* @param args
*/
String s1=s.nextLine();
String s2=s.nextLine();
UserMainCode.countNoOfWords(s1,s2);
s.close();
6. ArrayList Manipulation
4. The function fetch the odd index elements from first array list and even index elements from second array list and
Include a class UserMainCode with the static method generateOddEvenList which accepts two arraylist and returns an
arraylist.
Create a Class Main which would be used to read 2n integers and call the static method present in UserMainCode.
Note:
Input consists of 2n+1 integers. The first integer denotes the size of the arraylist, the next n integers are values to the first
arraylist, and the last n integers are values to the second arraylist.
Sample Input 1:
12
13
14
15
16
4
5
Sample Output 1:
13
15
UserMainCode
import java.util.ArrayList;
import java.util.Iterator;
ArrayList<Integer>al3=new ArrayList<Integer>();
for(int i=0;i<al1.size();i++)
if(i%2==0)
al3.add(al2.get(i));
else
al3.add(al1.get(i));
return al3;
}
}
Main
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
int s=Integer.parseInt(sc.nextLine());
ArrayList<Integer>al1=new ArrayList<Integer>();
ArrayList<Integer>al2=new ArrayList<Integer>();
for(int i=0;i<s;i++)
al1.add(sc.nextInt());
for(int i=0;i<s;i++)
al2.add(sc.nextInt());
ArrayList<Integer>al3=new ArrayList<Integer>();
al3=UserMainCode.generateOddEvenList(al1,al2);
Iterator<Integer> it=al3.iterator();
while(it.hasNext())
int n=it.next();
System.out.println(n);
sc.close();
}
}
7. Duplicate Characters
Write a Program which removes duplicate characters from the string. Your program should read a sentence (string) as input
from user and return a string removing duplicate characters. Retain the first occurance of the duplicate character. Assume the
Include a class UserMainCode with a static method removeDuplicates which accepts a string. The return type is the
Create a Class Main which would be used to accept the input string and call the static method present in UserMainCode.
Sample Input 1:
Sample Output 1:
hi tsample
Sample Input 2:
ABC DEF
Sample Output 2:
ABC DEF
UserMainCode
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.StringTokenizer;
char a[]=s1.toCharArray();
LinkedHashSet<Character>hs=new LinkedHashSet<Character>();
for(int i=0;i<a.length;i++)
hs.add(a[i]);
Iterator<Character>itr=hs.iterator();
while(itr.hasNext())
char o=itr.next();
if(o!=' ');
sb.append(o);
System.out.println(sb);
Main
import java.util.*;
public class Main {
String s1=s.nextLine();
UserMainCode.removeDuplicates(s1);
s.close();
8. Mastering Hashmap
You have recently learnt about hashmaps and in order to master it, you try and use it in all of your programs.
1. Read 2n numbers as input where the first number represents a key and second one as value. Both the numbers are
of type integers.
2. Write a function getAverageOfOdd to find out average of all values whose keys are represented by odd numbers.
Assume the average is an int and never a decimal number. Return the average as output. Include this function in
class UserMainCode.
Create a Class Main which would be used to read 2n numbers and build the hashmap. Call the static method present in
UserMainCode.
Input consists of a 2n+ 1 integers. The first integer specifies the value of n (essentially the hashmap size). The next pair of n
Sample Input 1:
2
34
12
22
Sample Output 1:
UserMainCode
import java.util.HashMap;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Iterator;
int av=0,c=0,s=0;
Iterator<Integer> it=h1.keySet().iterator();
while(it.hasNext())
int a=it.next();
if(a%2!=0)
int b=h1.get(a);
s=s+b;
c++;
av=s/c;
return av;
}}
Main
import java.util.*;
int n=sc.nextInt();
for(int i=0;i<n;i++)
h1.put(sc.nextInt(),sc.nextInt());
System.out.println(UserMainCode.getAverageOfOdd(h1));
sc.close();
would like to showcase your skills by creating a quick prototype. The prototype consists of the following steps:
1. Read Employee details from the User. The details would include id, designation and salary in the given order. The
2. You decide to build two hashmaps. The first hashmap contains employee id as key and designation as value, and the
second hashmap contains same employee ids as key and salary as value.
3. The company decides to hike the salary of managers by 5000. You decide to write a function increaseSalarieswhich
takes the above hashmaps as input and returns a hashmap with only managers id and their increased salary as output. Include
Create a Class Main which would be used to read employee details in step 1 and build the two hashmaps.
Input consists of employee details. The first number indicates the size of the employees. The next three values indicate the
Output consists of a single string. Refer sample output for formatting specifications.
SampleInput1:
programmer
3000
manager
50000
SampleOutput1:
55000
UserMainCode
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashMap;
HashMap<Integer,Integer>hm3=new HashMap<Integer,Integer>();
Iterator<Integer> it=hm.keySet().iterator();
while(it.hasNext())
int id=it.next();
String name=hm.get(id);
if(name.equals("manager"))
{int salary=hm1.get(id)+5000;
hm3.put(id,salary);
}}
return hm3;
Main
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
int s=Integer.parseInt(sc.nextLine());
HashMap<Integer,String>hm=new HashMap<Integer,String>();
HashMap<Integer,Integer>hm1=new HashMap<Integer,Integer>();
for(int i=0;i<s;i++)
int id=Integer.parseInt(sc.nextLine());
hm.put(id, sc.nextLine());
hm1.put(id,Integer.parseInt(sc.nextLine()));
HashMap<Integer,Integer>hm2=new HashMap<Integer,Integer>();
hm2=UserMainCode.display(hm,hm1);
Iterator<Integer> it=hm2.keySet().iterator();
while(it.hasNext())
int n=it.next();
int fac=hm2.get(n);
System.out.println(n);
System.out.println(fac);
Write a program to check if the first word and the last word in the input string match.
Include a class UserMainCode with a static method “check” that accepts a string argument and returns an int. If the first
word and the last word in the string match, the method returns the number of characters in the word. Else the method returns
the sum of the number of characters in the first word and last word.
Create a class Main which would get the input as a String and call the static method check present in the UserMainCode.
Output is an integer.
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
UserMainCode
import java.util.StringTokenizer;
int count=0;
String fin="";
String ini=st.nextToken();
while(st.hasMoreTokens())
{ fin=st.nextToken();
}
if(ini.equals(fin))
count=ini.length();
else
count=ini.length()+fin.length();
return count;
Main
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
String age=sc.nextLine();
System.out.println(UserMainCode.check(age));
sc.close();
}}
Given an array of Strings, write a program to take the last character of each string and make a new String by concatenating
it.
Include a class UserMainCode with a static method “concatCharacter” that accepts a String array as input and returns the
new String.
Create a class Main which would get the String array as input and call the static method concatCharacterpresent in the
UserMainCode.
The first line of the input consists of an integer n that corresponds to the number of strings in the input string array.
The next n lines of the input consist of the strings in the input string array.
Sample Input:
ab
abcd
Sample Output:
Bad
UserMainCode
for(int i=0;i<a.length;i++)
sb.append(a[i].charAt(a[i].length()-1));
return sb.toString();
Main
import java.util.*;
int s=Integer.parseInt(sc.nextLine());
for(int i=0;i<s;i++)
a[i]=sc.nextLine();
System.out.println(UserMainCode.concatCharacter(a));
sc.close();
12.Anagram
Write a program to check whether the two given strings are anagrams.
Note: Rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once
is called Anagram."
Include a class UserMainCode with a static method “getAnagram” that accepts 2 strings as arguments and returns an int.
The method returns 1 if the 2 strings are anagrams. Else it returns -1.
Create a class Main which would get 2 Strings as input and call the static method getAnagram present in the
UserMainCode.
Input consists of 2 strings. Assume that all characters in the string are lower case letters.
Output consists of a string that is either “Anagrams” or “Not Anagrams”.
Sample Input 1:
Sample Output 1:
Anagrams
Sample Input 2:
orchestra
carthorse
Sample Output 2:
Anagrams
Sample Input 3:
cognizant
technologies
Sample Output 3:
Not Anagrams
UserMainCode
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
String s5=s3.toUpperCase();
String s6=s4.toUpperCase();
l1.add(s5.charAt(i));
l2.add(s6.charAt(i));
Collections.sort(l1);
Collections.sort(l2);
// System.out.println(l1);
// System.out.println(l2);
if(l1.equals(l2))
System.out.println("Anagram");
else
System.out.println("Not Anagram");
Main
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
String s1=sc.nextLine();
String s2=sc.nextLine();
UserMainCode.getAnagram(s1,s2);
Given 2 strings corresponding to the previous meter reading and the current meter reading, write a program to calculate
electricity bill.
The input string is in the format ""AAAAAXXXXX"".
AAAAA is the meter code and XXXXX is the meter reading.
FORMULA: (XXXXX-XXXXX)*4
Hint: if AAAAA of input1 and input2 are equal then separate the XXXXX from string and convert to integer. Assume that
AAAAA of the 2 input strings will always be equal.
Include a class UserMainCode with a static method “calculateMeterReading” that accepts 2 String arguments and returns
an integer that corresponds to the electricity bill. The 1 st argument corresponds to the previous meter reading and the
2nd arguement corresponds to the current meter reading.
Create a class Main which would get 2 Strings as input and call the static method calculateMeterReading present in the
UserMainCode.
Input and Output Format:
Input consists of 2 strings. The first input corresponds to the previous meter reading and the second input corresponds to the
current meter reading.
Output consists of an integer that corresponds to the electricity bill.
Sample Input:
CSECE12390
CSECE12400
Sample Output:
40
import java.util.Scanner;
public class Main{
public static void main (String[] args)
{
// your code goes here
Scanner sc = new Scanner(System.in);
String input1=sc.next();
String input2=sc.next();
System.out.println(UserMainCode.calculateMeterReading(input1,input2));
sc.close();
}}
15.Kaprekar Number
Write a program to check whether the given input number is a Kaprekar number or not.
Note : A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that
has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits. If the sum of the two pieces is equal to the number,
then ‘n’ is a Kaprekar number.
If its Kaprekar number assign to output variable 1 else -1.
Example 1:
Input1:9
9^2 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.
Example 2:
Input1:45
Hint:
45^2 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar number."
Include a class UserMainCode with a static method “getKaprekarNumber” that accepts an integer argument and returns
an integer. The method returns 1 if the input integer is a Kaprekar number. Else the method returns -1.
Create a class Main which would get the an Integer as input and call the static method getKaprekarNumber present in the
UserMainCode.
Input and Output Format:
Input consists of an integer.
Output consists of a single string that is either “Kaprekar Number” or “Not A Kaprekar Number”
Sample Input 1:
9
Sample Output 1:
Kaprekar Number
Sample Input 2:
45
Sample Output 2:
Kaprekar Number
Sample Input 3:
4
Sample Output 3:
Not A Kaprekar Number
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int v=UserMainCode.getKaprekarNumber(n);
if (v==1)
System.out.println("Kaprekar Number");
else
System.out.println("Not a Kaprekar Number");
}}
16.Vowels
Given a String input, write a program to find the word which has the the maximum number of vowels. If two or more words
have the maximum number of vowels, print the first word.
Include a class UserMainCode with a static method “storeMaxVowelWord” that accepts a string argument and returns the
word containing the maximum number of vowels.
Create a class Main which would get the a String as input and call the static method storeMaxVowelWord present in the
UserMainCode.
Input and Output Format:
Input consists of a string. The string may contain both lower case and upper case letters.
Output consists of a string.
Sample Input :
What is your name?
Sample Output :
your
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1 =sc.nextLine();
UserMainCode.storeMaxVowelWord(s1);
}
}
import java.util.StringTokenizer;
Given a String as input , write a program to count and print the number of unique characters in it.
Include a class UserMainCode with a static method “checkUnique” that accepts a String as input and returns the number of
unique characters in it. If there are no unique characters in the string, the method returns -1.
Create a class Main which would get a String as input and call the static method checkUnique present in the
UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of an integer.
Sample Input 1:
HOWAREYOU
Sample Output 1:
7
(Hint :Unique characters are : H,W,A,R,E,Y,U and other characters are repeating)
Sample Input 2:
MAMA
Sample Output2:
-1
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s1=sc.next();
UserMainCode.checkUnique(s1);
}
}
import java.util.*;
public class UserMainCode {
Write a program to read an array and find average of all elements located at index i, where i is a prime number. Type cast the
average to an int and return as output. The index starts from 0.
Include a class UserMainCode with a static method addPrimeIndex which accepts a single integer array. The return type
(integer) should be the average of all elements located at index i where i is a prime number.
Create a Class Main which would be used to accept Input array and call the static method present in UserMainCode.
Input and Output Format:
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers
correspond to the elements in the array.
Output consists of a single Integer.
Refer sample output for formatting specifications.
Assume that the maximum number of elements in the array is 20 and minimum number of elements is 3.
Sample Input 1:
4
2
5
2
4
Sample Output 1:
3
import java.util.Scanner;
public class Main{
public static void main (String[] args)
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(UserMainCode.addPrimeIndex(n));
}}
import java.util.*;
public class UserMainCode {
public static int addPrimeIndex(int n) {
Scanner sc=new Scanner(System.in);
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
int sum=0;
int count=0;
int sum_count=0;
for(int i=0;i<a.length;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
sum=sum+a[i];
sum_count++;
}
}
int avg=sum/sum_count;
return avg;
}}
19. ArrayList and Set Operations
Main:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
ArrayList<Integer>a1=new ArrayList<Integer>();
ArrayList<Integer>a2=new ArrayList<Integer>();
for(int i=0;i<n;i++)
a1.add(Integer.parseInt(sc.nextLine()));
for(int i=0;i<n;i++)
a2.add(Integer.parseInt(sc.nextLine()));
char c=sc.nextLine().charAt(0);
System.out.println(UserMainCode.performSetOperations(a1,a2,c));
}
}
UserMainCode:
import java.util.ArrayList;
import java.util.ArrayList;
public class UserMainCode {
public static ArrayList<Integer>
performSetOperations(ArrayList<Integer>a1,ArrayList<Integer>a2,char c)
{
ArrayList<Integer>op1=new ArrayList<Integer>();
int k=0;
switch(c)
{
case '+':
a1.removeAll(a2);
a1.addAll(a2);
op1=a1;
break;
case '*':
a1.retainAll(a2);
op1=a1;
break;
case '-':
for(int i=0;i<a1.size();i++)
{
k=0;
for(int j=0;j<a2.size();j++)
{
if(a1.get(i)==a2.get(j))
k=1;
}
if(k==0)
op1.add(a1.get(i));
}
break;
}
return op1;
}}
return tm;
}
}
20.Largest Span
Write a program to read an array and find the size of largest span in the given array
""span"" is the number of elements between two repeated numbers including both numbers. An array with single element
has a span of 1.
.
Include a class UserMainCode with a static method getMaxSpan which accepts a single integer array. The return type
(integer) should be the size of largest span.
Create a Class Main which would be used to accept Input array and call the static method present in UserMainCode.
Input and Output Format:
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers
correspond to the elements in the array.
Output consists of a single Integer.
Refer sample output for formatting specifications.
Assume that the maximum number of elements in the array is 20.
Sample Input 1:
5
1
2
1
1
3
Sample Output 1:
4
Sample Input 2:
7
1
4
2
1
4
1
5
Sample Output 2:
6
MAIN:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.print(UserMainCode.getMaxSpan(a,n));
}}
USERMAINCODE:
class UserMainCode {
public static int getMaxSpan(int[] x,int n)
{
int gap=0,max=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(x[i]==x[j])
gap=j;
}
if(gap-i>max)
max=gap-i;
}
return max+1;
}
}
Sample Input 1:
3
sunil-56-88-23
bindul-88-70-10
john-70-49-65
Sample Output 1:
John
USERMAINCODE:
import java.util.ArrayList;
import java.util.StringTokenizer;
public class UserMainCode
{
public static String highestScorer(ArrayList<String>s1)
{
int max=0;
String s4=null;
for(int i=0;i<s1.size();i++)
{
String s2=s1.get(i);
StringTokenizer t=new StringTokenizer(s2,"-");
String s3=t.nextToken();
int n1=Integer.parseInt(t.nextToken());
int n2=Integer.parseInt(t.nextToken());
int n3=Integer.parseInt(t.nextToken());
int n=n1+n2+n3;
if(n>max)
{
max=n;
s4=s3;
}
}
return s4;
}
}
MAIN:
import java.util.*;
public class Main {
}
}
22. Max Vowels
Write a Program which fetches the word with maximum number of vowels. Your program
should read a sentence as input from user and return the word with max number of vowels.
In case there are two words of maximum length return the word which comes first in the
sentence.
Include a class UserMainCode with a static method getWordWithMaximumVowels which
accepts a string The return type is the longest word of type string.
Create a Class Main which would be used to accept two Input strings and call the static
method present in UserMainCode.
Input and Output Format:
Input consists of a string with maximum size of 100 characters.
Output consists of a single string.
Refer sample output for formatting specifications.
Sample Input 1:
Appreciation is the best way to motivate
Sample Output 1:
Appreciation
USERMAINCODE:
import java.util.StringTokenizer;
if(s3.charAt(i)=='a'||s3.charAt(i)=='e'||s3.charAt(i)=='i'||s3.charAt(i)=='
o'||s3.charAt(i)=='u'
||s3.charAt(i)=='A'||s3.charAt(i)=='E'||s3.charAt(i)=='I'||s3.charAt(i)=='O
'||s3.charAt(i)=='U')
count++;
}
if(count>max)
{
max=count;
s2=s3;
}
}
return s2;
}
}
MAIN:
import java.util.*;
public class Main {
Sample Output 2:
Invalid
USERMAINCODE:
String s2="aeiou";
StringBuffer sb=new StringBuffer();
for(int i=0;i<s1.length();i++)
{
for(int j=0;j<s2.length();j++)
{
if(s1.charAt(i)==s2.charAt(j))
{
sb.append(s1.charAt(i));
}
}
}
if(sb.toString().equals(s2))
{
return 1;
}
return -1;
}
}
MAIN:
import java.util.*;
public class Main {
import java.util.*;
public class UserMainCode {
public static String swapPairs(String s1)
{
int i;
StringBuffer sb=new StringBuffer();
for(i=0;i<s1.length()-1;i=i+2)
{
if(i%2==0)
{
char a=s1.charAt(i);
char b=s1.charAt(i+1);
sb.append(b).append(a);
}
else
for(i=0;i<s1.length()-1;i=i+2)
{
char a=s1.charAt(i);
char b=s1.charAt(i+1);
sb.append(b).append(a);
sb.append(s1.charAt(s1.length()-1));
}
}
return sb.toString();
}
}
MAIN:
import java.util.*;
public class Main {
USERMAINCODE:
import java.util.*;
public class Main {
26. Password
Validation Rule:
Atleast 8 characters
Atleast 1 number(1,2,3...)
Atleast 1 alphabet(a,B...)
Include a class UserMainCode with a static method “validatePassword” that accepts a String argument and returns a
boolean value. The method returns true if the password is acceptable. Else the method returns false.
Create a class Main which would get a String as input and call the static method validatePassword present in the
UserMainCode.
Sample Input 1:
cts@1010
Sample Output 1:
Valid
Sample Input 2:
punitha3
Sample Output 2:
Invalid
USERMAINCODE:
boolean b=false;
if(s1.length()>=8)
b=true;
if(b=true)
if(s1.matches(".*[0-9]{1,}.*")&&s1.matches(".*[a-zA-Z]{1,}.*")&&s1.matches(".*[@#%]{1,}.*"))
b=true;
else
b=false;
return b;
MAIN:
import java.util.*;
boolean b=(UserMainCode.validatePassword(s1));
if(b==true)
System.out.println("Valid");
else
System.out.println("Invalid");
s.close();
A Company wants to give away bonus to its employees. You have been assigned as the programmer to automate this
process. You would like to showcase your skills by creating a quick prototype. The prototype consists of the following steps:
1. Read Employee details from the User. The details would include id, DOB (date of birth) and salary in the given
order. The datatype for id is integer, DOB is string and salary is integer.
2. You decide to build two hashmaps. The first hashmap contains employee id as key and DOB as value, and the
second hashmap contains same employee ids as key and salary as value.
3. If the age of the employee in the range of 25 to 30 years (inclusive), the employee should get bonus of 20% of his
salary and in the range of 31 to 60 years (inclusive) should get 30% of his salary. store the result in TreeMap in
which Employee ID as key and revised salary as value. Assume the age is caculated based on the date 01-09-2014.
4. Other Rules:
c. a takes more priority than b i.e both if a and b are true then store -100.
5. You decide to write a function calculateRevisedSalary which takes the above hashmaps as input and returns the
Create a Class Main which would be used to read employee details in step 1 and build the two hashmaps. Call the static
method present in UserMainCode.
Input consists of employee details. The first number indicates the size of the employees. The next three values indicate the
employee id, employee DOB and employee salary. The Employee DOB format is “dd-mm-yyyy”
Sample Input 1:
1010
20-12-1987
10000
2020
01-01-1985
14400
Sample Output 1:
1010
12000
2020
17280
USERMAINCODE:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.TreeMap;
class UserMainCode {
public static TreeMap<Integer,Integer>
calculateRevisedSalary(LinkedHashMap<Integer,String>a1,LinkedHashMap<Integer,Integer>a2) throws
ParseException
{TreeMap<Integer,Integer>ans=new TreeMap<Integer,Integer>();
ArrayList<String>al=new ArrayList<String>();
Iterator <Integer>it=a1.keySet().iterator();
while(it.hasNext())
{int s=it.next();
String s1=a1.get(s);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
sdf.setLenient(false);
try{
Date d=new Date();
Date d1=new Date();
String a=s1;
String b="01-09-2014";
d=sdf.parse(a);
d1=sdf.parse(b);
long t=d.getTime();
long t1=d1.getTime();
long t3=t1-t;
long l1=(24 * 60 * 60 * 1000);
long l=l1*365;
long res=t3/l;
//System.out.println("Result="+res);
if(res>=25 && res<=30)
{
float bonus=(float)((0.2*a2.get(s))+a2.get(s));
int r=(int)bonus;
ans.put(s,r );
}else if(res>30 && res<=60)
{
float bonus=(float)((0.3*a2.get(s))+a2.get(s));
int r=(int)bonus;
ans.put(s,r );
}
else if(a2.get(s)<5000)
{
ans.put(s, -100);
}
else if(res<25 ||res>60)
{
ans.put(s, -200);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
return ans;
}
}
MAIN:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.TreeMap;
public class Main
{
public static void main(String args[]) throws ParseException{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
LinkedHashMap<Integer,String>a1=new LinkedHashMap<Integer,String>();
LinkedHashMap<Integer,Integer>a2=new LinkedHashMap<Integer,Integer>();
TreeMap<Integer,Integer>ans=new TreeMap<Integer, Integer>();
for(int i=0;i<n;i++)
{int id=sc.nextInt();
a1.put(id,sc.next());
int salary=sc.nextInt();
a2.put(id,salary);
}
ans=UserMainCode.calculateRevisedSalary(a1,a2);
Iterator <Integer>it=ans.keySet().iterator();
while(it.hasNext())
{
int a=it.next();
int b=ans.get(a);
System.out.println(a);
System.out.println(b);
}
}
}
A School wants to assign grades to its students based on their marks. You have been assigned as the programmer to
automate this process. You would like to showcase your skills by creating a quick prototype. The prototype consists of the
following steps:
1. Read student details from the User. The details would include roll no, mark in the given order. The datatype for id
2. You decide to build a hashmap. The hashmap contains roll no as key and mark as value.
3. BUSINESS RULE:
2. If Mark is less then to 80 and greater than or equal to 60 store medal as ""SILVER"".
3 .If Mark is less then to 60 and greater than or equal to 45 store medal as ""BRONZE"" else store ""FAIL"".
Store the result in TreeMap in which Roll No as Key and grade as value.
4. You decide to write a function calculateGrade which takes the above hashmaps as input and returns the treemap as
Create a Class Main which would be used to read employee details in step 1 and build the two hashmaps. Call the static
Input consists of employee details. The first number indicates the size of the students. The next two values indicate the roll
id, mark.
1010
80
100
40
Sample Output 1:
100
FAIL
1010
GOLD
USERMAINCODE:
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
public class UserMainCode
{
public static TreeMap<Integer,String>calculateGrade(HashMap<Integer,Integer>hm)
{
TreeMap<Integer,String>tm=new TreeMap<Integer,String>();
Iterator<Integer> it=hm.keySet().iterator();
while(it.hasNext())
{
int id=it.next();
int mark=hm.get(id);
if(mark>=80)
tm.put(id,"GOLD");
else if(mark<80 && mark>=60)
tm.put(id,"SILVER");
else if(mark<60 && mark>=45)
tm.put(id,"BRONZE");
else
tm.put(id,"FAIL");
}
return tm;
}}
MAIN:
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Scanner;
public class Main {
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
HashMap<Integer,Integer>hm=new HashMap<Integer,Integer>();
for(int i=0;i<s;i++)
{
hm.put(sc.nextInt(),sc.nextInt());
}
TreeMap<Integer,String>tm=new TreeMap<Integer,String>();
tm=UserMainCode.calculateGrade(hm);
Iterator<Integer> it=tm.keySet().iterator();
for(int i=0;i<s;i++)
{
int n=it.next();
String fac=tm.get(n);
System.out.println(n);
System.out.println(fac);
}
}
}
29.Digits - II
Write a program to read a non-negative integer n, compute the sum of its digits. If sum is
greater than 9 repeat the process and calculate the sum once again until the final sum comes
to single digit.Return the single digit. Include a class UserMainCode with a static method
getDigitSum which accepts the integer value. The return type is integer. Create a Class Main
which would be used to accept the string and call the static method present in UserMainCode.
Input and Output Format: Input consists of a integer. Output consists of integer. Refer sample
output for formatting specifications.
MAIN:
import java.util.Scanner;
int sum=UserMainCode.getDigitSum(a);
System.out.println(sum);
USERMAINCODE:
int sum = 0 ;
int n1=n;
while(n>10)
int a = 0 ; sum = 0;
while(n!=0)
a = n%10;
sum+=a;
n=n/10;
n=sum;
}
return sum;
30.Anagrams
Write a program to read two strings and checks if one is an anagram of the other. An anagram
is a word or a phrase that can be created by rearranging the letters of another given word or
phrase. We ignore white spaces and letter case. All letters of 'Desperation' can be rearranged
to the phrase 'A Rope Ends It'. Include a class UserMainCode with a static method
checkAnagram which accepts the two strings. The return type is boolean which is TRUE /
FALSE. Create a Class Main which would be used to accept the two strings and call the static
method present in UserMainCode.
Input and Output Format: Input consists of two strings. Output consists of TRUE / FALSE.
Refer sample output for formatting specifications. Sample Input 1: tea eat Sample Output 1:
TRUE
MAIN:
import java.util.*;
String s1=s.nextLine();
String s2=s.nextLine();
System.out.println(UserMainCode.checkAnagram(s1,s2));
USERMAINCODE:
import java.util.*;
import java.text.*;
boolean b=false;
String aj2=s2.toLowerCase();
for(int i=0;i<aj1.length();i++)
char c=aj1.charAt(i);
if(c!=' ')
{
a1.add(c);
for(int j=0;j<aj2.length();j++)
char c=aj2.charAt(j);
if(c!=' ')
a2.add(c);
if(a1.size()==a2.size())
if(a1.containsAll(a2))
b= true;
return b;
31.Shift Left
Write a program to read a integer array of scores, and return a version of the given array
where all the 5's have been removed. The remaining elements should shift left towards the
start of the array as needed,
and the empty spaces at the end of the array should be filled with 0.
Include a class UserMainCode with a static method shiftLeft which accepts the integer array.
The return type is modified array.
Create a Class Main which would be used to accept the integer array and call the static
method present in UserMainCode.
Input consists of an integer n which is the number of elements followed by n integer values.
Sample Input 1: 7 1 5 2 4 5 3 5
Sample Output 1: 1 2 4 3 0 0 0
MAIN:
import java.util.Scanner;
int size=sc.nextInt();
int[]m=new int[size];
int[]n=new int[size];
for(int i=0;i<size;i++)
{
n[i]=sc.nextInt();
m=UserMainCode.shiftLeft(n);
for(int i=0;i<size;i++)
System.out.println(m[i]);
USERMAINCODE:
int j=0;
int[]m=new int[n.length];
for(int i=0;i<n.length;i++)
if(n[i]!=5)
m[j]=n[i];
j++;
return m;
}
32.Word Count
Given a string array (s) with each element in the array containing alphabets or digits. Write a
program to add all the digits in every string and return the sum as an integer. If two digits
appear simultaneously do not consider it as one number. Ex- For 'Hyderabad 21' consider 2
and 1 as two digits instead of 21 as a number.
Include a class UserMainCode with a static method sumOfDigits which accepts the string
array. The return type is the integer formed based on rules. Create a Class Main which would
be used to accept the string and integer and call the static method present in UserMainCode.
Input and Output Format: Input consists of a an integer indicating the number of elements in
the string array. Output consists of a integer . Refer sample output for formatting
specifications.
MAIN:
import java.util.Scanner;
int n=s.nextInt();
for(int i=0;i<n;i++)
{
a[i]=s.next();
System.out.println(UserMainCode.sumOfDigits(a));
USERMAINCODE:
int sum = 0;
for(int i=0;i<s1.length;i++)
String s = s1[i];
for(int j = 0;j<s.length();j++)
Character c = s.charAt(j);
if(Character.isDigit(c))
sum+=Integer.parseInt(s.valueOf(c));
return sum;
}
33.Prefix Finder
Given a string array (s) with each element in the array containing 0s and 1s. Write a program
to get the number of strings in the array where one String is getting as prefixed in other String
in that array . Example 1: Input: {10,101010,10001,1111} Output =2 (Since 10 is a prefix of
101010 and 10001) Example 2: Input: {010,1010,01,0111,10,10} Output =3(01 is a prefix of
010 and 0111. Also, 10 is a prefix of 1010) Note: 10 is NOT a prefix for 10.
Include a class UserMainCode with a static method findPrefix which accepts the string array.
The return type is the integer formed based on rules. Create a Class Main which would be
used to accept the string and integer and call the static method present in UserMainCode.
Input and Output Format: Input consists of a an integer indicating the number of elements in
the string array followed by the array. Output consists of a integer . Refer sample output for
formatting specifications.
MAIN:
import java.util.Scanner;
int n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++)
s[i]=sc.nextLine();
System.out.println(UserMainCode.findPrefix(s));
USERMAINCODE:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
LinkedHashSet<String>l1=new LinkedHashSet<String>();
ArrayList<String>a1=new ArrayList<String>();
int c=0;
for(int i=0;i<s.length;i++)
l1.add(s[i]);
Iterator<String> it=l1.iterator();
while(it.hasNext())
a1.add(it.next());
for(int i=0;i<a1.size();i++)
String s2=a1.get(i);
for(int j=0;j<a1.size();j++)
String s3=a1.get(j);
if(i!=j&&s3.length()>s2.length())
String s4=s3.substring(0,s2.length());
if(s2.equals(s4))
c++;
}
return c;
34.Commons
Given two arrays of strings,return the count of strings which is common in both arrays.
Duplicate entries are counted only once. Include a class UserMainCode with a static method
countCommonStrings which accepts the string arrays. The return type is the integer formed
based on rules. Create a Class Main which would be used to accept the string arrays and
integer and call the static method present in UserMainCode.
Input and Output Format: Input consists of a an integer indicating the number of elements in
the string array followed by the array. Output consists of a integer . Refer sample output for
formatting specifications.
Sample Input 2: 5 ba ba black sheep wool 5 ba ba have any wool Sample Output 2: 2
MAIN:
import java.util.Scanner;
int n1 = sc.nextInt();
s1[i] = sc.next();
}
int n2 = sc.nextInt();
s2[i] = sc.next();
System.out.println(UserMainCode.countCommonStrings(s1,s2));
USERMAINCODE:
import java.util.ArrayList;
int count=0;
if(s1[i].equals(s2[j])){
if(!al.contains(s1[i])){
count++;
al.add(s1[i]);
}
return count;
35.Sequence Sum
Write a program to read a non-negative integer n, and find sum of fibonanci series for n
number..
Include a class UserMainCode with a static method getFibonacciSum which accepts the
integer value. The return type is integer.
The fibonacci seqence is a famous bit of mathematics, and it happens to have a recursive
definition.
Each subsequent value is the sum of the previous two values, so the whole seqence is
0,1,1,2,3,5 and so on.
You will have to find the sum of the numbers of the Fibonaaci series for a given int n.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
Sample Output 1:
7
MAIN:
import java.util.Scanner;
int n=s.nextInt();
System.out.println(UserMainCode.getFibonacciSum(n));
USERMAINCODE:
import java.util.ArrayList;
import java.util.Scanner;
int a=0,b=1,c=0,d=1;
for(int i=3;i<=n;i++){
c=a+b;
a=b; b=c;
d=d+c;
return d;
36.E-Mail Validation
Write a program to read a string and validate the given email-id as input. Validation Rules: 1.
Ensure that there are atleast 5 characters between '@' and '.' 2. There should be only one '.'
and one '@' symbol. 3. The '.' should be after the '@' symbol. 4. There must be atleast three
characters before '@'. 5. The string after '.' should only be 'com'
Include a class UserMainCode with a static method ValidateEmail which accepts the string.
The return type is TRUE / FALSE as per problem. Create a Class Main which would be used
to accept the string and call the static method present in UserMainCode.
Input and Output Format: Input consists of a string. Output consists of TRUE / FALSE. Refer
sample output for formatting specifications.
MAIN:
import java.util.*;
String ip;
ip=s.next();
boolean b=UserMainCode.ValidateEmail(ip);
if(b==true)
System.out.println("TRUE");
else
System.out.println("FALSE");
}}
USERMAINCODE:
import java.util.StringTokenizer;
int i=0;
boolean b=false;
String s1=t.nextToken();
String s2=t.nextToken();
String s3=t1.nextToken();
String s4=t1.nextToken();
i++;
if(i==1)
if(s3.length()==5)
if(s1.length()>=3)
if(s4.equals("com"))
b=true;
return b;
37.Symmetric Difference
Write a program to read two integer array and calculate the symmetric difference of the two
arrays. Finally Sort the array.
Union operation merges the two arrays and makes sure that common elements appear only
once. Intersection operation
A U B ={ 7,11,12,14,24,26,36} and
A ^ B = {7,14}
Create a Class Main which would be used to accept the two integer arrays and call the static
method present in
UserMainCode.
Input consists of an integer n which is the number of elements followed by n integer values.
The same sequnce is followed
Sample Input 1:
5
11
14
26
Sample Output 1:
11
14
26
MAIN:
import java.util.*;
int n,m;
for(int i=0;i<n;i++)
a1[i] = sin.nextInt();
m = sin.nextInt();
for(int i=0;i<m;i++)
a2[i] = sin.nextInt();
for(int i=0;i<result.length;i++)
System.out.println(result[i]);
USERMAINCODE:
import java.util.*;
{
//int[] a1 = new int[]{11,5,14,26,3};
int[] union,inter,result;
int count=0;
/*union*/
for(int i=0;i<a1.length;i++)
if(!temp.contains(a1[i]))
++count;
temp.add(a1[i]);
for(int i=0;i<a2.length;i++)
if(!temp.contains(a2[i]))
++count;
temp.add(a2[i]);
for(int i=0;i<count;i++)
{
union[i] = (int)temp.get(i);
Arrays.sort(union);
/*intersection*/
count =0;
Arrays.sort(a2);
for(int i=0;i<a1.length;i++)
if(Arrays.binarySearch(a2,a1[i]) >= 0)
++count;
temp.add(a1[i]);
for(int i=0;i<count;i++)
inter[i] = (int)temp.get(i);
Arrays.sort(inter);
/*difference */
count =0;
Arrays.sort(inter);
for(int i=0;i<union.length;i++)
if(Arrays.binarySearch(inter,union[i]) < 0)
++count;
temp.add(union[i]);
for(int i=0;i<count;i++)
result[i] = (int)temp.get(i);
Arrays.sort(result);
return result;
38.Day of Week
Write a program to read a string containing date in DD/MM/YYYY format and prints the day
of the week that date falls on.
Include a class UserMainCode with a static method getDayOfWeek which accepts the string.
The return type is the string.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
02/04/1985
Sample Output 1:
Tuesday
MAIN:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
System.out.println(UserMainCode.getDayofWeek(s1));
USERMAINCODE:
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
Date d=sdf.parse(s1);
String s=sdf1.format(d);
return s.toLowerCase();
}
39.Add Time
Write a program to read two String variables containing time intervals in hh:mm:ss format.
Add the two time intervals and
Include a class UserMainCode with a static method addTime which accepts the string values.
The return type is the string.
Create a Class Main which would be used to accept the two string values and call the static
method present in
UserMainCode.
Sample Input 1:
12:45:30
13:50:45
Sample Output 1:
1:2:36:15
Sample Input 2:
23:59:59
23:59:59
Sample Output 2:
1:23:59:58
MAIN:
import java.util.*;
import java.io.IOException;
import java.text.*;
String s1=s.next();
String s2=s.next();
System.out.println(UserMainCode.addTime(s1,s2));
USERMAINCODE:
import java.util.*;
import java.io.IOException;
import java.text.*;
public class UserMainCode {
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
sdf.setTimeZone(TimeZone.getTimeZone("s1"));
sdf.setTimeZone(TimeZone.getTimeZone("s2"));
Date d1=sdf.parse(s1);
Date d2=sdf.parse(s2);
long add=d1.getTime()+d2.getTime();
String s=sdf.format(add);
Calendar cal=Calendar.getInstance();
cal.setTime(sdf.parse(s));
int FindDay=cal.get(Calendar.DAY_OF_MONTH);
if(FindDay>1)
FindDay=FindDay-1;
String op=FindDay+":"+s;
return op;
40.ISBN Validation
Write a program to read a string and validate the given ISBN as input.
Validation Rules:
1. An ISBN (International Standard Book Number) is a ten digit code which uniquely
identifies a book.
2. To verify an ISBN you calculate 10 times the first digit, plus 9 times the second digit, plus
8 times the third ..all the way
If the final number leaves no remainder when divided by 11 the code is a valid ISBN.
Example 1:
Input:0201103311
Calculation: 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55.
55 mod 11 = 0
Output: true
Include a class UserMainCode with a static method validateISBN which accepts the string.
The return type is TRUE /
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Sample Input 1:
0201103311
Sample Output 1:
TRUE
MAIN:
import java.util.*;
String ip=s.next();
System.out.println(UserMainCode.ISBNnumber(ip));
USERMAINCODE:
import java.util.*;
import java.text.*;
int sum=0;
for(int i=0,j=ip.length();i<ip.length();i++,j--){
String s=String.valueOf(ip.charAt(i));
int n=Integer.parseInt(s);
sum+=(n*j); }
//System.out.println(sum);
if(sum%11==0)
b="TRUE";
return b;
41.Date Format
Write a program to read two String variables in DD-MM-YYYY.Compare the two dates and
return the older date in
'MM/DD/YYYY' format.
Include a class UserMainCode with a static method findOldDate which accepts the string
values. The return type is the
string.
Create a Class Main which would be used to accept the two string values and call the static
method present in
UserMainCode.
Input and Output Format:
Sample Input 1:
05-12-1987
8-11-2010
Sample Output 1:
12/05/1987
MAIN:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
String s1=s.next();
String s2=s.next();
System.out.println(UserMainCode.findOldDate(s1,s2));
}
USERMAINCODE:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
Date d1=sdf.parse(s1);
Date d2=sdf.parse(s2);
Calendar cal=Calendar.getInstance();
cal.setTime(d1);
long y=cal.getTimeInMillis();
cal.setTime(d2);
long y1=cal.getTimeInMillis();
String s3=sdf1.format(d1);
String s4=sdf1.format(d2);
if(y<y1)
return s3;
else
return s4;
42.Interest Calculation
Write a program to calculate amount of the acccount holders based on the below mentioned prototype:
1. Read account details from the User. The details would include id, DOB (date of birth) and amount in the given order. The
2. You decide to build two hashmaps. The first hashmap contains employee id as key and DOB as value, and the second
a. If the age greater than or equal to 60 then interest rate is 10% of Amount.
b.If the age less then to 60 and greater than or equal to 30 then interest rate is 7% of Amount.
5. You decide to write a function calculateInterestRate which takes the above hashmaps as input and returns the treemap
Create a Class Main which would be used to read employee details in step 1 and build the two hashmaps. Call the static
method present in UserMainCode.
Input consists of account details. The first number indicates the size of the acoount. The next three values indicate the user
Output consists of the user id and the amount for each user one in a line.
Sample Input 1:
SBI-1010
20-01-1987
10000
SBI-1011
03-08-1980
15000
SBI-1012
05-11-1975
20000
SBI-1013
02-12-1950
30000
Sample Output 1:
SBI-1010:10400
SBI-1011:16050
SBI-1012:21400
SBI-1013:33000
MAIN
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
int s=Integer.parseInt(sc.nextLine());
HashMap<String,String>hm=new HashMap<String,String>();
HashMap<String,Integer>hm1=new HashMap<String,Integer>();
for(int i=0;i<s;i++)
String id=sc.nextLine();
hm.put(id, sc.nextLine());
hm1.put(id,Integer.parseInt(sc.nextLine()));
TreeMap<String,Integer>tm=new TreeMap<String,Integer>();
tm=UserMainCode.calculateInterestRate(hm,hm1);
Iterator<String> it=tm.keySet().iterator();
while(it.hasNext())
String n=it.next();
int fac=tm.get(n);
System.out.println(n+":"+fac);
USERMAINCODE
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
int year=0,amount=0;
double dis=0;
String now="01/01/2015";
TreeMap<String,Integer>tm=new TreeMap<String,Integer>();
Iterator<String> it=hm.keySet().iterator();
while(it.hasNext())
String id=it.next();
String dor=hm.get(id);
amount=hm1.get(id);
try
Date d=sdf.parse(dor);
Date d1=sdf1.parse(now);
sdf.setLenient(false);
int y=d.getYear();
int y1=d1.getYear();
int m=d.getMonth();
int m1=d1.getMonth();
int day=d.getDay();
int day1=d1.getDay();
year=y1-y;
if(m>m1)
year--;
else if(m==m1)
{if(day<day1)
year--;
}
if(year>=60)
dis=0.1*amount+amount;
dis=0.07*amount+amount;
else
dis=0.04*amount+amount;
tm.put(id,(int)dis);
catch(Exception e)
e.printStackTrace();
return tm;
Write a program to calculate discount of the acccount holders based on the transaction amount and registration date using
1. Read account details from the User. The details would include id, DOR (date of registration) and transaction amount in the
given order. The datatype for id is string, DOR is string and transaction amount is integer.
2. You decide to build two hashmaps. The first hashmap contains employee id as key and DOR as value, and the second
a. If the transaction amount greater than or equal to 20000 and registration greater than or equal to 5 year then discount
b. If the transaction amount greater than or equal to 20000 and registration less then to 5 year then discount rate is 10%
of transaction amount.
c. If the transaction amount less than to 20000 and registration greater than or equal to 5 year then discount rate is 15%
of transaction amount.
d. If the transaction amount less than to 20000 and registration less then to 5 year then discount rate is 5% of transaction
amount.
4. You decide to write a function calculateDiscount which takes the above hashmaps as input and returns the treemap as
Create a Class Main which would be used to read employee details in step 1 and build the two hashmaps. Call the static
Input consists of transaction details. The first number indicates the size of the employees. The next three values indicate the
user id, user DOR and transaction amount. The DOR (Date of Registration) format is “dd-mm-yyyy”
Output consists of a string which has the user id and discount amount one in a line for each user.
Sample Input 1:
A-1010
20-11-2007
25000
B-1011
04-12-2010
30000
C-1012
11-11-2005
15000
D-1013
02-12-2012
10000
Sample Output 1:
A-1010:5000
B-1011:3000
C-1012:2250
D-1013:500
MAIN:
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Scanner;
int s=Integer.parseInt(sc.nextLine());
HashMap<String,String>hm=new HashMap<String,String>();
HashMap<String,Integer>hm1=new HashMap<String,Integer>();
for(int i=0;i<s;i++)
{
String id=sc.nextLine();
hm.put(id, sc.nextLine());
hm1.put(id,Integer.parseInt(sc.nextLine()));
TreeMap<String,Integer>tm=new TreeMap<String,Integer>();
tm=UserMainCode.calculateDiscount(hm,hm1);
Iterator<String> it=tm.keySet().iterator();
while(it.hasNext())
String n=it.next();
int fac=tm.get(n);
System.out.println(n+":"+fac);
USERMAINCODE
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.*;
int amount=0;
double dis=0;
String now="01/01/2015";
TreeMap<String,Integer>tm=new TreeMap<String,Integer>();
Iterator<String> it=hm.keySet().iterator();
while(it.hasNext())
String id=it.next();
String dor=hm.get(id);
amount=hm1.get(id);
sdf.setLenient(false);
try{
String a=dor;
String b="01-01-2014";
d=sdf.parse(a);
d1=sdf.parse(b);
long t=d.getTime();
long t1=d1.getTime();
long t3=t1-t;
long l=l1*365;
long year1=t3/l;
//System.out.println("Result="+year1);
dis=0.2*amount;
dis=0.1*amount;
dis=0.15*amount;
else
dis=0.05*amount;
tm.put(id,(int)dis);
catch(Exception e)
e.printStackTrace();
return tm;