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
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
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 C++ or Java application to create BOTH Stack & Queue data structures. The application...
Write a C++ or Java application to create BOTH Stack & Queue data structures. The application also creates a "DisplayStackElement" and "DisplayQueueElement" routine. The application must be menu driven (with an option to terminate the application) and provide the following features. Allow insertion of a "Circle" object/structure in the Stack data structures. The Circle contains a "radius" data member. The Circle also uses functions/methods "setRadius", "getRadius" and calculateArea (returns a double data type). Allow insertion of a "Circle" object/structure in...
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...
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...
******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.
Write the program in Java (with a graphical user interface) and have it calculate and display...
Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. You need to include Calculate, Reset, and Exit buttons on your GUI. Please insert comments in the program to document the program. Allow the user to enter...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT