LAB EXERCISE – 6
1. 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).
import java.util.*;
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 Oddcounter implements UnaryPredicate<Integer>
{
public boolean test(Integer i){ return i%2 != 0; }
}
class Number
{
public static void main(String...args)
{
Collection<Integer> ci = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
int count = Algorithm.countIf(ci,new Oddcounter());
System.out.println("Number of odd integers : "+count);
}
}
2. Write a generic method to exchange the positions of two different
elements in an array.
import java.util.*;
class Array
{
public static <T> void swap(T[] a, int i, int j)
{
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String...args)
{
Integer[] arr = {1,2,3,4,5,6,7,8,9,10};
Double[] arrd = {1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.2};
swap(arr,0,2);
swap(arrd,0,2);
for(int i : arr)
System.out.print(i + " ");
System.out.println();
for(double i : arrd)
System.out.print(i + " ");
}
}
3. Write a generic class to implement the stack operation (Push, Pop
and delete).
class Stack<E> {
private final int size;
private int top;
private E[] elements;
public Stack() {
this(10);
}
public Stack(int s) {
size = s > 0 ? s : 10;
top = -1;
elements = (E[]) new Object[size]; // create array
}
public void push(E pushValue) {
if (top == size - 1) // if stack is full
throw new FullStackException(String.format("Stack is full, cannot push
%s", pushValue));
elements[++top] = pushValue; // place pushValue on Stack
}
public E pop() {
if (top == -1) // if stack is empty
throw new EmptyStackException("Stack is empty, cannot pop");
return elements[top--]; // remove and return top element of Stack
}
}
class EmptyStackException extends RuntimeException {
public EmptyStackException() {
this("Stack is empty");
}
public EmptyStackException(String exception) {
super(exception);
}
}
class FullStackException extends RuntimeException {
public FullStackException() {
this("Stack is full");
}
public FullStackException(String exception) {
super(exception);
}
}
class MainClass {
public static void main(String args[]) {
double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
int[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
Stack<Double> doubleStack = new Stack<Double>(5);
Stack<Integer> integerStack = new Stack<Integer>(10);
// test Push Double
try {
System.out.println("\nPushing elements onto doubleStack");
for (double element : doubleElements) {
System.out.printf("%.1f ", element);
doubleStack.push(element);
}
} catch (FullStackException fullStackException) {
System.err.println();
fullStackException.printStackTrace();
}
// test Pop Double
try {
System.out.println("\nPopping elements from doubleStack");
double popValue;
while (true) {
popValue = doubleStack.pop(); // pop from doubleStack
System.out.printf("%.1f ", popValue);
}
} catch (EmptyStackException emptyStackException) {
System.err.println();
emptyStackException.printStackTrace();
}
// test push method with integer stack
try {
System.out.println("\nPushing elements onto integerStack");
for (int element : integerElements) {
System.out.printf("%d ", element);
integerStack.push(element);
}
} catch (FullStackException fullStackException) {
System.err.println();
fullStackException.printStackTrace();
}
// test pop method with integer stack
try {
System.out.println("\nPopping elements from integerStack");
int popValue; // store element removed from stack
// remove all elements from Stack
while (true) {
popValue = integerStack.pop();
System.out.printf("%d ", popValue);
}
} catch (EmptyStackException emptyStackException) {
System.err.println();
emptyStackException.printStackTrace();
}
}
}