Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Two stacks of positive integers are needed, both containing integers with values

ID: 3745532 • Letter: T

Question


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) (which is stack in this case 2. write an application, generate random 500 numbers between 0 and 5000, then push them (valid numbers) to their corresponding stack 2. print the numbers stored in each stack Classe file include (you must use the names as specified) NumberStack.java NumberStack Tester java Delete PgUp F8 #2 F3 Nu Backspace Lo 5 6

Explanation / Answer

import java.util.Random;

class NumberStack{

private int arr[];

private int top=-1;

private int capacity;

public NumberStack(int size){

arr = new int [size];

capacity=size;

}

public void push(int number) {

if(this.top>=this.capacity-1)return;

arr[++top]=number;

}

public boolean isempty () {

if(top==-1)return true;

return false;

}

public void pop() {

if(this.isempty())return;

top--;

}

public int top () {

return arr[top];

}

}

public class NumberStackTester {

public static void main(String args[]) {

NumberStack s1=new NumberStack (500);

NumberStack s2=new NumberStack (500);

Random rand = new Random();

int value;

for(int i=0;i<200;i++) {

value = rand.nextInt(1001);

if(value%2==1)s1.push(value);

else s2.push(value);

}

  

for(int i=0;i<500;i++) {

value = rand.nextInt(5001);

if(value%2==1)s1.push(value);

else s2.push(value);

}

System.out.println("stack containing odd elements");

  

while(s1.isempty()!=true) {

System.out.print(s1.top()+" ");

s1.pop();

}

System.out.println(" stack containing even elements");

while(s2.isempty()!=true) {

System.out.print(s2.top()+" ");

s2.pop();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote