In: Computer Science
First time working with stack implementation. Newbie to java Two stacks of positive integers are needed, both containing integers with values less than or equal to 1000. One stack contains even integers, the other contains odd integers. The total number of elements in the combined stacks is never more than 200 at anytime. but we cannot predict how many are in each stack. (All of the elements could be in one stack, they could be evenly divided, both stacks could be empty, and so on). 1. Design ADT (named NumberStack.java). 2. write an application, generate random 2000 numbers between 0 and 5000, then push them (valid numbers) to their corresponding stack. 2. print the numbers stored in each stack. Class file include (you must use the names as specified): NumberStack.java NumberStackTester.java
import java.util.*;
class NumberStack
{
Stack st1= new Stack(); //stack objects
Stack st2= new Stack();
int n;
public void readNumbers()
{
System.out.println("Input Elements in First Stack");
for(int i=1;i<=2000;i++)
{
n=(int )(Math.random()*((5000-0)+1))+0; //generate random number
between 0 and 5000
System.out.print("\t"+n);
st1.push(new Integer(n)); //pushing element into stack
}
System.out.println("\nInput Elements in Secound Stack");
for(int i=1;i<=2000;i++)
{
n=(int)(Math.random()*((5000-0)+1))+0;
System.out.print("\t"+n);
st2.push(new Integer(n));
}
}
public void printNumbers()
{
System.out.println("\nPoped Elements in stack 1 are : ");
while(!st1.empty()) // verifying is stack empty
{
System.out.print("\t"+st1.pop()); //printing stack top
element
}
System.out.println("\nPoped Elements in stack 2 are : ");
while(!st2.empty())
{
System.out.print("\t"+st2.pop());
}
}
}
public class NumberStackTester
{
public static void main(String args[])
{
NumberStack s=new NumberStack();
s.readNumbers();
s.printNumbers();
}
}