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
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
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...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface can be found below: List.java public interface List<T> extends Iterable<T> { /** * Insert an element at a specified location. * @param index * @param obj * @throws IndexOutOfBoundsException */ public void add(int index, T obj); /** * Append an object to the end of the list. * @param obj */ public boolean add(T obj); public void clear(); public boolean contains(T obj); /** *...
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.
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...
Write a java program that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
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
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
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)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT