Name -Sammed Vijay Biraje Core Java
CORE JAVA LAB Batch – B
Que.1 Two strings are called anagrams if they contain all the same characters in
the same frequencies. For this challenge, the test is not case-sensitive.
For example, the anagrams of CAT are CAT, ACT, tac, TCA, aTC, and CtA.
Returns Boolean. If and are case-insensitive anagrams, return true. Otherwise,
return false.
Input Format
The first line contains a string .
The second line contains a string .
Sample Input 0
anagram
margana
Code-
package core_java_lab;
import [Link];
import [Link];
public class Quation_1 {
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter 1st String ");
String s1 = [Link]();
[Link]("Enter 2nd String ");
String s2 = [Link]();
// replacing the space and converting all the
letters to lower-case for better comparison
s1 = [Link](" ", "");
s2 = [Link](" ", "");
s1 = [Link]();
s2 = [Link]();
// converting strings to character array and storing
it into a char variable
char a[] = [Link]();
char b[] = [Link]();
[Link](a);
[Link](b);
// comparing both the character arrays
boolean c = [Link](a, b);
if(c == true)
{
[Link]("Both the strings are
anagram");
}
else
{
[Link]("Both the strings are not
anagram");
}
}
}
Output-
Que. 2 Java interface can only contain method signatures and fields. The
interface can be used to achieve polymorphism. In this problem, you will
practice your knowledge on interfaces.
You are given an interface which contains a method signature. You
need to write a class called MyCalculator which implements the interface.
function just takes an integer as input and return the sum of all its divisors. For
example divisors of 6 are 1, 2, 3 and 6, so should return 12. The value of n
will be at most 1000.
You just need to write the MyCalculator class only.
Code –
package core_java_lab;
import [Link].*;
interface Math{
int signature(int a);
}
class MyCalculator implements Math
{
public int signature(int a) {
int sum = 0;
for(int i = 1; i <= a; i++)
{
if(a%i == 0)
{
sum += i;
}
}
return sum;
}
}
public class Quation_2
{
public static void main(String args[])
{
MyCalculator c = new MyCalculator();
int a = [Link](6);
[Link](a);
}
}
Output –
Que. 3 Given an array, print all the elements which are leaders. A Leader is an
element that is greater than all of the elements on its right side in the array.
Example 1:
Input:
arr = [4, 7, 1, 0]
Output:
710
Explanation: Rightmost element is always a leader. 7 and 1 are greater than
the elements in their right side.
Example 2:
Input:
arr = [10, 22, 12, 3, 0, 6]
Output:
22 12 6
Explanation: 6 is a leader. In addition to that, 12 is greater than all the
elements in its right side (3, 0, 6), also 22 is greater than 12, 3, 0, 6.
Code –
package core_java_lab;
public class Quation_3 {
public static void main(String[] args)
{
int arr[] = {10, 22, 12, 3, 0, 6};
for(int i = 0; i < [Link]; i++)
{
for(int j = i; j < [Link]; j++)
{
if(arr[i] < arr[j])
{
break;
}
if(j == [Link] - 1)
[Link](arr[i]+" ");
}
}
}
}
Output –