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...
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...
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
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
java Problem 3: An Interesting Problem Write a program that accepts two positive integers: a deposited...
java Problem 3: An Interesting Problem Write a program that accepts two positive integers: a deposited amount of money and an interest rate, as an annual percentage rate. Your program will calculate the number of years that will take for the account balance to reach $1, 000,000. You can assume that the initial deposit is less than $1,000,000 Input The input will begin with a single line containing T , the number of test cases to follow. The remaining lines...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT