Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8837437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:43:25+00:00 2026-06-14T09:43:25+00:00

I have a homework problem in which I need to remove all the green

  • 0

I have a homework problem in which I need to remove all the green items from the stack.

Here is my code:

import java.util.*;
import java.io.*;

public class Pex
{
public static void main(String[] args)
{
Stack stack1 = new Stack(); 

addPez(stack1);
removeGreen(stack1);
System.out.println(printStack(stack2));
}

public void addPez(Stack stack1)
{
    stack1.push("yellow");
    stack1.push("red");
    stack1.push("green");
    stack1.push("green");
    stack1.push("yellow");
    stack1.push("yellow");
    stack1.push("red");
    stack1.push("green");
}

public static void removeGreen(Stack stack1)
{
Stack temp = new Stack();
while (!stack1.isEmpty()){
  String check = (String)(stack1.pop());
    if(check.equals("green")){
    stack1.pop();}
    else{
    temp.push(check);}
}
Stack stack2 = new Stack();
while (!temp.isEmpty()){
  String tmp = stack1.pop();
  stack2.push(tmp);}
}

public static void printStack(Stack stack2)
{
Stack xerox = stack2.clone();
while (!xerox.isEmpty()){
  System.out.println(xerox.pop());}
}
}

Can someone point me in the right direction? I think I’m almost there.

I also need to figure out why I am getting some errors.

Here are the errors:

Pex.java:10: non-static method addPez(Stack) cannot be referenced from a static context
addPez(stack1);
^
Pex.java:12: cannot find symbol
symbol  : variable stack2
location: class Pex
System.out.println(printStack(stack2));
                              ^

Pex.java:39: incompatible types
found   : java.lang.Object
required: java.lang.String
  String tmp = stack1.pop();
                         ^

Pex.java:45: incompatible types
found   : java.lang.Object
required: Stack
Stack xerox = stack2.clone();
                          ^

./Stack.java:69: cannot find symbol
symbol  : variable stack1
location: class Stack
    stack2 = (Stack)(stack1.clone());
                     ^

5 errors
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:43:27+00:00Added an answer on June 14, 2026 at 9:43 am
    public static void main(String[] args) {
        Stack stack1 = new Stack();
    
        addPez(stack1);
        removeGreen(stack1);
        printStack(stack1); //stack2 is not defined and remove println statement
    }
    
    public static void addPez(Stack stack1) {//make addPez as static
        stack1.push("yellow");
        stack1.push("red");
        stack1.push("green");
        stack1.push("green");
        stack1.push("yellow");
        stack1.push("yellow");
        stack1.push("red");
        stack1.push("green");
    }
    
    public static void removeGreen(Stack stack1) {
        Stack temp = new Stack();
        while (!stack1.isEmpty()) {
            String check = (String) (stack1.pop());
            if (check.equals("green")) {
                //stack1.pop();
            } else {
                temp.push(check);
            }
        }
        //Stack stack2 = new Stack();
        while (!temp.isEmpty()) {
            String tmp = (String)temp.pop();
            stack1.push(tmp);
        }
    }
    
    public static void printStack(Stack stack1) {
        Stack xerox = (Stack)stack1.clone();
        while (!xerox.isEmpty()) {
            System.out.println(xerox.pop());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this homework problem where I need to use regex to remove every
I have a homework problem here I've been working on; here is the description:
I have a question about my homework problem. Here is the problem: Write a
I have a homework assignment to write a multi-threaded sudoku solver, which finds all
This was a homework assignment problem which I know I have incorrectly answered. I
Okay, so I have another question on a prolog homework problem I am struggling
I have a homework question that says: Problem 1: Given the array [ 22
I am doing homework but I have problem with non-RestFul routes. My spec is:
I am learning Ruby and have to do some homework, but have problem with
This is my homework, but please read my problem description first. I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.