Question

In: Computer Science

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

Solutions

Expert Solution

import java.util.Collections;
import java.util.Stack;

public class Main
{
    //method to print even numbers of the stack 
    public static void printEven(Stack<Integer> s1){
        
        //initialize ans array
        int[] ans = new int[50];
        int i = 0;
        
        //loop runs untill all ements of the stack are popped out
        while(s1.size() > 0){
            int n = s1.pop();
            //check is popped element is even or odd
            //modulo operator returns the remainder of integer division
            //if number is even then it is divisible by 2 so remainder is 0
            if( n % 2 == 0)
            {
                //add number to the stack
                ans[i] = n;
                i++;
            }
            
        }
        
        //prints all the even numbers
        System.out.print("Even numbers: ");
        for(int j = 0 ; j < i ; j++){
            System.out.print(ans[j] + " ");
        }
       
    }
    
    //method to print the sum of odd numbers of the stack 
    public static void printOddSum(Stack<Integer> s2){
        
        int sum = 0;
        while(s2.size() > 0){
            int n = s2.pop();
            //if number is even then add then prints the sum of all odd numbers
            if( n % 2 != 0)
            {
               sum = sum + n;
            }
            
        }
        
        //now the sum of all even numbers are stored in sum 
        System.out.println("Sum of Odd numbers:  " + sum);
       
    }   
    
        public static void main(String[] args) {
        
            //initialize Stack s1 and s2
            Stack<Integer> s1 = new Stack<>();
            Stack<Integer> s2 = new Stack<>();

        //push 100 elements in stack s1 and s2
        for(int i = 0 ; i< 100 ; i++){
            s1.push(i);
            s2.push(i);
        }

        //prints the stack content
        System.out.println("Stack" + s1);
        
        //call printOddSum
        printOddSum(s1);
        
        // call printEven method
        printEven(s2);        
        
    }
    
}

OUTPUT:


Related Solutions

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
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...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
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)....
Write a java program to reverse element of a stack. For any given word (from input),...
Write a java program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process. 1. Push into stack 2. Pop from stack 3. Enqueue into queue 4. Dequeue from queue 5. Push into stack 6. Pop from stack and display java
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)
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Java Generics (Javas built-in Stack) What are the problems?    class genStck {         Stack stk...
Java Generics (Javas built-in Stack) What are the problems?    class genStck {         Stack stk = new Stack ();         public void push(E obj) {                         push(E);                 }         public E pop() {        Object obj = pop();         }    }        class Output {         public static void main(String args[]) {             genStck <> gs = new genStck ();             push(36);             System.out.println(pop());         }    }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT