You are on page 1of 9

SKILL-2

2100031820
M.Himani
S-12(AOOP)
1.Generic Interface

package pack1;

import pack1.GenericInterface.Pair;

public class GenericInterface


{

static class Pair


{
int min;
int max;
}
static Pair getMinMax(int arr[], int n)
{
Pair minmax = new Pair();
int i;

if (n == 1)
{
minmax.max = arr[0];
minmax.min = arr[0];
return minmax;
}

if (arr[0] > arr[1])


{
minmax.max = arr[0];
minmax.min = arr[1];
}
else
{
minmax.max = arr[1];
minmax.min = arr[0];
}
for (i = 2; i < n; i++)
{
if (arr[i] > minmax.max)
{
minmax.max = arr[i];
}
else if (arr[i] < minmax.min)
{
minmax.min = arr[i];
}
}

return minmax;
}

public static void main(String args[])


{
int arr[] = {3, 6, 2, 8, 6};
int arr_size = 5;
Pair minmax = getMinMax(arr, arr_size);
System.out.printf("\nMinimum element is %d", minmax.min);
System.out.printf("\nMaximum element is %d", minmax.max);
}
}

OUTPUT
2.Specific Property

package pack1;
import java.util.Arrays;
import java.util.Collection;
final class Algorithm
{
public static <T> int countIf(Collection<T> c, UnaryPredicate<T> p) {

int count = 0;
for (T elem : c)
if (p.test(elem))
++count;
return count;
}
}
interface UnaryPredicate<T>
{
public boolean test(T obj);
}
class OddPredicate implements UnaryPredicate<Integer>
{
public boolean test(Integer i) { return i % 2 != 0; }
}

public class Specificproperty {


public static void main(String[] args)
{
Collection<Integer> ci = Arrays.asList(1, 2, 3, 4, 11, 17);
int count = Algorithm.countIf(ci, new OddPredicate());
System.out.println("Number of odd integers = " + count);
}
}
OUTPUT

3.Relatively Prime

package pack1;
import java.util.*;
import java.util.*;
final class Algorithm1
{
public static <T>
int findFirst(List<T> list, int begin, int end, UnaryPredicate1<T> p)
{
for (; begin < end; ++begin)
if (p.test(list.get(begin)))
return begin;
return -1;
}
public static int gcd(int x, int y) {
for (int r; (r = x % y) != 0; x = y, y = r) { }
return y;
}
}
interface UnaryPredicate1<T>
{
public boolean test(T obj);
}
class RelativelyPrimePredicate implements UnaryPredicate1<Integer>
{
public RelativelyPrimePredicate(Collection<Integer> c)
{
this.c = c;
}
public boolean test(Integer x)
{
for (Integer i : c)
if (Algorithm1.gcd(x, i) != 1)
return false;
return c.size() > 0;
}
private Collection<Integer> c;
}
public class Relativelyprime
{
public static void main(String[] args) throws Exception
{
List<Integer> li = Arrays.asList(3, 4, 6, 8, 11, 15, 28, 32);
Collection<Integer> c = Arrays.asList(7, 18, 19, 25);
UnaryPredicate1<Integer> p = new RelativelyPrimePredicate(c);

int i = Algorithm1.findFirst(li, 0, li.size(), p);

if (i != -1) {
System.out.print(li.get(i) + " is relatively prime to ");
for (Integer k : c)
System.out.print(k + " ");
System.out.println();
}
}
OUTPUT

4.Palindrome of a Number

package pack1;

public class Palindrome


{
public static void main(String[] args)
{
int num =32468, reversed = 0;
System.out.println("Original Number: " + num);
while(num != 0)
{
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
}}
OUTPUT

You might also like