0% found this document useful (0 votes)
190 views

Java Lab Exercise - 6

The document contains 3 Java code examples: 1. A generic method to count elements in a collection that match a given predicate. It takes a collection and predicate as parameters and returns the count. 2. A generic method to swap the positions of two elements in an array. It takes an array and two indices as parameters. 3. A generic Stack class implementing push, pop, and exception handling for empty and full stacks. It uses a private array to store elements and tracks the top index.

Uploaded by

Dhanush Dhoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views

Java Lab Exercise - 6

The document contains 3 Java code examples: 1. A generic method to count elements in a collection that match a given predicate. It takes a collection and predicate as parameters and returns the count. 2. A generic method to swap the positions of two elements in an array. It takes an array and two indices as parameters. 3. A generic Stack class implementing push, pop, and exception handling for empty and full stacks. It uses a private array to store elements and tracks the top index.

Uploaded by

Dhanush Dhoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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();
}

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy