0% found this document useful (0 votes)
16 views12 pages

OOPPS

The document contains Java programs demonstrating various concepts such as stack implementation, multithreading, and a generic method for finding the maximum of three values. The StackADT class provides methods for stack operations like push, pop, and peek, while the multithreading example generates random numbers and determines if they are even or odd. Additionally, the MaximumTest class showcases a generic method to find the maximum value among three inputs of any comparable type.

Uploaded by

ds9006801
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

OOPPS

The document contains Java programs demonstrating various concepts such as stack implementation, multithreading, and a generic method for finding the maximum of three values. The StackADT class provides methods for stack operations like push, pop, and peek, while the multithreading example generates random numbers and determines if they are even or odd. Additionally, the MaximumTest class showcases a generic method to find the maximum value among three inputs of any comparable type.

Uploaded by

ds9006801
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PROGRAM:

import java.util.*;

/* Class arrayStack */
class StackADT
{
int array[];
int top, size, len;
Scanner in=new
Scanner(System.in)
;
/* Constructor for arrayStack */
public StackADT(int n)
{
size = n;
len = 0;
array = new
int[size]; top = -1;
}
/* Function to check if stack is empty
*/ public boolean isEmpty()
{
return top == -1;
}
/* Function to check if stack is full
*/ public boolean isFull()
{
return top == size -1 ;
}
/* Function to get the size of the stack */
public int getSize()
{
return len ;
}
/* Function to check the top element of the stack
*/ public int peek()
{

211423243501 YUGANDRAN D
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return array[top];
}
/* Function to add an element to the stack
*/ public void push()
{
if(top + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
if(top + 1 < size )
{
System.out.println("Enter integer element to push"); array[++top]
= in.nextInt();
}
len++ ;
}
/* Function to delete an element from the
stack */ public int pop()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
len-- ;
return array[top--];
}
/* Function to display the status of the stack */
public void display()
{
System.out.print("\nStack =
"); if (len == 0)
{
System.out.print("Empty\
n"); return ;
}
for (int i = top; i >= 0; i--)
System.out.print(array[i]+"
");
System.out.println();
}
}

211423243501 YUGANDRAN D
public class Stack_Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Stack Test\n");

System.out.println("Enter Size of Integer Stack ");


int n = scan.nextInt();
/* Creating object of class arrayStack
*/ StackADT stk = new StackADT(n);
/* Perform Stack Operations
*/ char ch;
do{
System.out.println("\nStack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :

try
{
stk.push();
}
catch (Exception e)
{
System.out.println("E
rror : " +
e.getMessage());
}
break;

211423243501 YUGANDRAN D
case 2 :
try
{
System.out.println("Popped Element = " + stk.pop());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 3 :
try
{
S
yst
em.
out.
pri
ntln
("P
eek
Ele
me
nt
="
+
stk.
pee
k())
;
}
cat
ch
(Exce
ption
e)
{
S
yst
em.
out.
pri
ntln
("E
rror
:"
+
e.g
et
211423243501 Me
ssa YUGANDRAN D
OUTPUT:

211423243501 YUGANDRAN D
211423243501 YUGANDRAN D
211423243501 YUGANDRAN D
PROGRAM
import java.util.Random;

class EvenNum implements Runnable { int


x;

public EvenNum(int x) {
this.x = x;
}

public void run() {


System.out.println("Number Generated " + x + " is Even; Square of the number ="+
x * x);
}
}

class OddNum implements Runnable { int


x;

public OddNum(int x) { this.x


= x;
}

public void run() {


System.out.println("Number Generated
" + x + " is Odd; Cube of the number
="+x*x
* x);
}
}

class Thread1 extends Thread


{ Random r = new Random();
int iterations;

public Thread1(int iterations) {


this.iterations = iterations;
}

211423243501 YUGANDRAN D
public void run() {
for (int i = 0; i < iterations; i++) {
int num = r.nextInt(11); // Generate a number between 0 and 10 if
(num % 2 == 0) {
new Thread(new EvenNum(num)).start();
} else {
new Thread(new OddNum(num)).start();
}
try {
Thread.sleep(100); // Sleep for 100ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public class MultiThread {


public static void main(String[] args) {
int iterations = 10; // Number of times
the loop will run Thread1 t1
= new Thread1(iterations);
t1.start();
}
}

211423243501 YUGANDRAN D
OUTPUT

211423243501 YUGANDRAN D
PROGRAM
import java.io.*;
import
java.util.*;

public class
MaximumTest {
public static <T
extends
Comparable<T>
> T maximum(T
x, T y, T z) {
T max = x; // Assume x is the largest initially
if (y.compareTo(max) > 0) {
max = y; // y is the largest so far
}
if (z.compareTo(max) > 0)
{ max = z; // z is the largest
now
}
return max; // returns the largest
object
}

public static void main(String args[]) {


int a, b, c;
Scanner s = new Scanner(System.in);
System.out.println("Enter 3 numbers:");

a = s.nextInt();
b = s.nextInt();
c = s.nextInt();

System.out.println("Maximum value is " + maximum(a, b, c)); System.out.println("Maximum


value is " + maximum(6.6, 8.8, 7.7)); System.out.println("Maximum value is " +
maximum("pear", "apple", "orange"));
}
}

211423243501 YUGANDRAN D
OUTPUT

211423243501 YUGANDRAN D

You might also like