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
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...
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)
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...
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 and test a user-defined class (requiring conditions). Write an application (client) program that uses an...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an instance(s) of a user-defined class. The federal income tax that a person pays is a function of the person's taxable income. The following table contains formulas for computing a single person's tax. Bracket Taxable Income Tax Paid 1 $22,100 or less 15% 2 More than $22,100 but $53,500 or less $3,315 plus 28% of the taxable income over $22,100 3 More than $53,500 but...
In an application write a method filterStack that takes a stack of integers as a parameter...
In an application write a method filterStack that takes a stack of integers as a parameter and filters its elements (in a new Stack) in a way that places the even elements at the bottom and the odd ones at the top. The original stack should remain unchanged. You should use a queue (only one queue) as a temporary storage. Use stack and queue operations only to solve this problem. No need to write the main method. For example, if...
Using the Stack class (it's on the class web page) construct a program that will evaluate...
Using the Stack class (it's on the class web page) construct a program that will evaluate postfix (reverse Polish, more properly reverse Lukasiewicz`notation). First, create a class called Postfix. It should have one static function public static double postfix(String s) { This should take a string of tokens consisting of either double constants or the operators + - / *. It should return the value of the postfix expression as a double. The algorithm is 1) Get the next token...
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT