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 6859017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:13:54+00:00 2026-05-27T02:13:54+00:00

Okay, so I’ve written my own stack class, not using the built in one.

  • 0

Okay, so I’ve written my own stack class, not using the built in one. The stack class holds a List of Entry objects and the size of the stack ( both instance fields). In the stack class i have a push method which adds an Entry object onto the stack. When added the size of stack should increment by one.

This works fine I’ve tested it and it works. I should probably also explain what my Entry class contain. The Entry class basically has a constructor which takes a number and holds this.

So for the Stack class my push method looks like this:

public class Stack{

private List<Entry> entries = new ArrayList<Entry>();
private int size;

public void push(Entry entry)
{
  if(entry == null)
  String message = "The Parameter given was illegal(null)";
  throw new IllegalArgumentException(message);
}
entries.add(entry) // adds the entry object to the stack (or entries List)
size ++ // increments the size by 1. 
}

Again, this works perfectly fine, im able to add Entry objects to my Stack and the size increments as it should. Now, we’re required to make a new class called NumberStack which basically hides the detail and calls the push method in the stack class from that class. I’ll show you how my NumberStack class looks like:

public class NumberStack {


private Stack numStack = new Stack();


public void push(final float i) {


numStack.push(new Entry(i));
} 

So, basically it does the same as the push method in the Stack class, but hides the details. Now THIS doesn’t work! Nothing gets added to the Stack (entries List) when i call this method. If i try to push two numbers via my NumberStack push method. it should call the push method in the Stack class and add the Entry objects. But nothing at all happens. Again, if I simply push directly in my Stack class, it works perfectly fine.

Why doesn’t the push method in my NumberStack class work? It has no effect, nothings gets added to the Stack.

EDIT:

I tried testing it:

public static void main(String[] args){

System.out.println("Debugging...");
NumStack numStack = new NumStack();
Stack stack = new Stack();
System.out.println("Size before pushing from NumStack = " + stack.size());
numStack.push(5);
System.out.println("Size after pushing from NumStack method push =  " + stack.size());
stack.push(new Entry(5));
System.out.println("Size after pushing directly from Stack  = " + stack.size());
}

The above returned:

Size before pushing from NumStack = 0

Size after pushing from NumStack method push = 0

Size after pushing directly from Stack = 1

Any help much appreciated
Thanks.

  • 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-05-27T02:13:55+00:00Added an answer on May 27, 2026 at 2:13 am

    Inside the NumberStack class you create a Stack object called numStack.When numStack.push(5); is called it goes to the NumberStack class push method and pushed into the stack object called numStack.So instance variable size is incremented in that object. But in testing you are creating another instance of Stack class called stack. But instance variable size is still 0 in that object.

    But inside the main method you print the size from stack instance.That is why you get answer 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
Okay I do not even know where to begin when it comes to this
Okay, when something is draggable, it is given the class .ui-draggable And when something
Okay, this one is pretty obvious to everyone who use Django and frequently asked
Okay I have created an application where in one of the screens I have
Okay so I have a simple HTML Input fragment: <input id=txtFirstName type=text class=txtBox runat=server
Okay So I create a ListView and my own ListAdapter but whenever I add
Okay i know i have to use the JDBC etc, but im not sure
Okay, I'm reading about Linux kernel development and there are some code snippets using
Okay, so I've tried to use sort to vector of items so the size

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.