You are on page 1of 8

Skilling week-2(2100032102)

1) Implement Generic Interface to get the minimum and maximum number in


the given array.

Input:

3, 6, 2, 8, 6

Output:

Minimum value: 2

Maximum value: 8

Code:

package p1;

import p1.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 = arr.length;
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) Write a generic method to count the number of elements in a collection
that have a specific property (for example, odd integers, prime numbers,
palindromes)

Input:

1, 2, 3, 4, 11, 17

Output:

Number of odd integers = 4

Code:

package p1;
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) Write a generic Java program to find the first integer in a list that is relatively
prime to a list of specified integers

Input:

3, 4, 6, 8, 11, 15, 28, 32


Output:

11 is relatively prime

Code:
package p1;
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) Write a Java generic program to find palindrome of a number

Input:

16361

Output:

16361

Code:

package p1;

public class Palindrome


{
public static void main(String[] args)
{
int num =16361, 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