Question

In: Computer Science

First time working with stack implementation. Newbie to java Two stacks of positive integers are needed,...

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

Solutions

Expert Solution

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


Related Solutions

Suppose that two stacks of positive integers are needed for a project. Each stack is to...
Suppose that two stacks of positive integers are needed for a project. Each stack is to contain integers that are less than or equal to 500. One stack is to contain even integers; the other stack is to contain odd integers. Also, the total number of values in the combined stacks at any given time will not exceed 200. ▪ Design a way to implement both stacks in only one(pay attention!!!) 1-D array. ▪ Write a Python class definition(s) for...
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Assume you have a stack of integers. The stack contains same number of positive and negative...
Assume you have a stack of integers. The stack contains same number of positive and negative integers. You want to organize it such that negative and positive integers alternate (+-+-.., or -+-+,..). A. Write a Java code that uses no more than two additional Stacks to solve the problem. Note: You do not need to write the code for Stacks, you are using a Stack from the library with a name ourStack and has the following interface: ourStack() constructor, pop,...
please in java ! Assume you have a stack of integers. The stack contains same number...
please in java ! Assume you have a stack of integers. The stack contains same number of positive and negative integers. You want to organize it such that negative and positive integers alternate (+-+-.., or -+-+,..). A. Write a Java code that uses no more than two additional Stacks to solve the problem. Note: You do not need to write the code for Stacks, you are using a Stack from the library with a name ourStack and has the following...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX) (i) Push an Element on to stack (ii) Pop an Element from stack (iii) Demonstrate how stack can be used to check Palindrome (iv) Display the status of stack (v) Exit
Find two positive integers such that the sum of the first number and four times the...
Find two positive integers such that the sum of the first number and four times the second number is 100 and the product of the numbers is as large as possible. please double check answer
JAVA Stack - Implementation. You will be able to use the push, pop and peek of...
JAVA Stack - Implementation. You will be able to use the push, pop and peek of Stack concept. Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT