Question

In: Computer Science

Suppose the interface and the class of stack already implemented, Write application program to 1- insert...

Suppose the interface and the class of stack already implemented, Write application program to

1- insert 100 numbers to the stack

2- Print the even numbers

3- Print the summation of the odd numbers

Solutions

Expert Solution

Java code

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
        Stack st = new Stack();
        Stack temp = new Stack();
        Scanner scan = new Scanner(System.in);
        int num,sum = 0;
      
        System.out.println("Enter 100 elements in stack");
        for(int i=0;i<100;i++){
            System.out.print("Enter number : ");
            num = scan.nextInt();
            st.push(num);
        }
      
        System.out.println("Even numbers in stack are");
       while(!st.isEmpty()){
            num = st.top();
            st.pop();
          
            if(num % 2 == 0)System.out.println(num);
            sum = sum + num;
            temp.push(num);
       }
      
       while(!temp.isEmpty()){
            st.push(temp.top());
            temp.pop();
       }
      
       System.out.println("Sum of all odd numbers is " + sum);
       scan.close();
   }
}


Related Solutions

Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
Write a program that prompts the user to insert an item into a stack, delete an...
Write a program that prompts the user to insert an item into a stack, delete an item from the stack, or display all the items in the stack. Assume that the stack maximum capacity is 10. Here is sample run . Stack operation menu: 1. Insert 2. Delete 3. Display 4. Quit
Write a program that reverses a text file by using a stack. The user interface must...
Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...
The < and == operators for the class Record have already been implemented for you.
The < and == operators for the class Record have already been implemented for you. Write the code necessary to complete the >, <=,>= and != operators. (hint: you do not need to know anything about the Record class to complete)
Given a class Stack with the interface public void push(char n) // pushes n onto stack...
Given a class Stack with the interface public void push(char n) // pushes n onto stack public char pop() // return the top of the stack, removing element from stack public boolean isEmpty() // return true if stack is empty Write a method public int removeX(Stack<Character> stack) which takes a stack of Characters, removes the occurrences of ‘X’ and returns the count of the number of Xs removed. It must restore the stack to its original order (less the Xs)....
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
Implementing a Stack Write a program that implements a stack of integers, and exercises the stack...
Implementing a Stack Write a program that implements a stack of integers, and exercises the stack based on commands read from cin. To do this, write a class called Stack with exactly the following members: class Stack { public: bool isEmpty(); // returns true if stack has no elements stored int top(); // returns element from top of the stack // throws runtime_error("stack is empty") int pop(); // returns element from top of the stack and removes it // throws...
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a program that implements a stack of integers, and exercises the stack based on commands...
Write a program that implements a stack of integers, and exercises the stack based on commands read from cin. To do this, write a class called Stack with exactly the following members: class Stack { public: bool isEmpty(); // returns true if stack has no elements stored int top(); // returns element from top of the stack // throws runtime_error("stack is empty") int pop(); // returns element from top of the stack and removes it // throws runtime_error("stack is empty")...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT