-2

Below is the code for a Stack program. My question specifically is about the push method, where at the beginning, it checks if (pContent != null). Why does it do that? I commented the if statement out and it still worked fine, so whats the reason for the usage of it. Also , what is the difference between pContent and ContentType here?

Im trying to understand this code I got, Im very thankful for any help.

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)




public class Stacko<ContentType> extends Actor {

  /* --------- Anfang der privaten inneren Klasse -------------- */

  private class StackNode {

    private ContentType content = null;
    private StackNode nextNode = null;

    public StackNode(ContentType pContent) {
      content = pContent;
      nextNode = null;
    }

    public void setNext(StackNode pNext) {
      nextNode = pNext;
    }

    public StackNode getNext() {
      return nextNode;
    }

    public ContentType getContent() {
      return content;
    }
  }

  /* ----------- Ende der privaten inneren Klasse -------------- */

  private StackNode head;

  public void Stack() {
    head = null;
  }

  public boolean isEmpty() {
    return (head == null);
  }

  public void push(ContentType pContent) {
    if (pContent != null) {
      StackNode node = new StackNode(pContent);
      node.setNext(head);
      head = node;
    }
  }

  public void pop() {
    if (!isEmpty()) {
      head = head.getNext();
    }
  }

  public ContentType top() {
    if (!this.isEmpty()) {
      return head.getContent();
    } else {
      return null;
    }
  }
}
diatomym
  • 163
  • 1
  • 2
  • 11
  • Oh and I'm pretty sure you've mistaken helpful and thankful ;) – FMaz Jan 15 '17 at 16:49
  • No need for profanity. – duffymo Jan 15 '17 at 16:49
  • I'm sorry I didnt write it, I got it from my teacher. I actually mention this below, but seems like I used the wrong word in the beginning – diatomym Jan 15 '17 at 16:49
  • 1
    Adding the null check before you push guarantees that you don't push null references onto the stack. Seems reasonable to me. – duffymo Jan 15 '17 at 16:50
  • @duffymo Ah so its there so I cant put nothing onto the stack! THANKS this seems logical, I am not a smart person, dangit now that its obvious I feel dumb – diatomym Jan 15 '17 at 16:52
  • It's called "programming by contract". Your contract for stack says it only contains non-null references. The if check enforces it. – duffymo Jan 15 '17 at 16:53
  • @duffymo Thanks got it! Another quickie, what is the difference between pContent and ContentType? – diatomym Jan 15 '17 at 17:03
  • 1
    (sigh) Now you're being a bother. ContentType is the type of the parameter passed to the method; pContent is the name of the parameter. You cannot learn how to write Java this way. – duffymo Jan 15 '17 at 17:04
  • Im so sorry, but I got a test this wednesday I have to be able to completely recreate these thing from scratch, so normally I'll have to be able to understand everything. nonetheless, thanks for the help, really really appreciate it! – diatomym Jan 15 '17 at 17:07

1 Answers1

1

There is a possibility that it is null (=undefined). This happens when you tell it "Put nothing in there". The program is not able to add "nothing" and throws an error.

So one should check whether it is null first.

kleopi
  • 460
  • 3
  • 13
  • Yes this seems to be it, thank you very much for helping! I'm learning Java right now and always I choke at the smallest things and just cant learn. thank god for stackoverflow – diatomym Jan 15 '17 at 16:55
  • Stack overflow is not for thinking or investigating the things you seem unwilling or unable to learn for yourself. – duffymo Jan 15 '17 at 17:05
  • Come on we all started somewhere, I'm really just at the beginning and I'm actually trying to improve in this language and its hard, so I need all the help i can get and I really appreciate you guys helping! – diatomym Jan 15 '17 at 17:12