views:

220

answers:

5

Question :

b) A Stack is a last-in first-out (LIFO) data structure. Write a Java class Stacklnt that stores a stack of integers using an alray as its private data structure. A Stacklnt has:

  • A maximum size, which is set when a Stacklnt object is created. An exception should be thrown if the size is outside the range 10 to . 1000

  • A method push, to add a value to the top of the stack. An exception should be thrown if the stack is full when push is called.

  • A method pop, to remove and return the value at the top of the stack. An exception should be thrown if the stack is empty when pop is called.

c) Write some example code to show how your class Stacklnt from part (b) should be used. Include examples of normal usage and for all cases when exceptions are thrown.

Ok so basically this is a question im trying to solve and would really really appreciate some help.

exceptions are in this form

// Throw an exception!
public T pop() throws EmptyStackException
{
   if (contents.size() == 0) 
   { throw new EmptyStackException(); } 
   else
   { return contents.remove(0); }
}

me so far :

public class Stack {
  private int top;
  private int[] storage; 

  Stack(int capacity) {
        if (capacity <= 0)
              throw new IllegalArgumentException(
                         "Stack's capacity must be positive");
        storage = new int[capacity];
        top = -1;
  }

  void push(int value) {
        if (top == storage.length)
              throw new StackException("Stack's underlying storage is overflow");
        top++;
        storage[top] = value;
  }

  int peek() {
        if (top == -1)
              throw new StackException("Stack is empty");
        return storage[top];
  }
}
+2  A: 

read Fundamentals of OOP and Data Structures in Java - Richard Wiener

Chapter 11 is all about stacks and queues

Robert
Read chapter 11 for the code examples, not the theoretical discussion. The OP needs a working familiarity with writing, compiling and running programs, and that includes seeing examples of code with proper syntax.
weiji
A: 

Here's what you should do:

  • Create a class named Stacklnt**.
  • Use an array of integers int[] as an instance variable.
  • Use the constructor of the class to initiate the array with a given size as the constructor's parameter. You have to check that this size is in the range (10 to 1000)
  • make the two instance methods push and pop. You can use an integer variable as a pointer to the current array position, so you increase it when push is called, and you decrease it when pop is called. You have to make sure this pointer doesn't exceed the limits (i.e. doesn't go lower than zero, and doesn't go higher than the size).
Aziz
yeah I mean I know the basics how to do it but I just cant seem to put it into code :
A: 

Read over your question once more and look for all of the things you have not done.

  • you have not put in the method pop
  • you have not followed the example of exceptions given by the sample pop()
  • you have not check for range properly
  • you have not written a main method which instantiates a stack and tests it
Robert
i like ur bullet points - cheers
+1  A: 

As an answer to your last comment to your question: your syntax is not bad. I cleaned your code up a bit and, once you've implemented the StackException class and the pop method, it should compile correct:

public class Stack {

    private int top;
    private int[] storage;

    public Stack(int capacity) {
     if (capacity <= 0) {
      throw new IllegalArgumentException(
        "Stack's capacity must be positive");
     }

     top = -1;
     storage = new int[capacity];
    }

    public void push(int value) {

     if (top == storage.length) {
      throw new StackException("Stack's underlying storage is overflow");
     }
     top++;
     storage[top] = value;

    }

    public int peek() {

     if (top == -1) {
      throw new StackException("Stack is empty");
     }

     return storage[top];
    }

    public int pop() {
     // TODO
    }
}
Andreas_D
thank you - ummm er what about the public static void man damn im dumb :)
+5  A: 

You're trying to do the whole program at once, which is a bit difficult because there could be many trivial syntax errors, and any ONE of them will cause it to not compile.

So, the recommendation is to take baby steps - you'll probably hear this a lot. It goes like this (assuming you haven't written any code yet):

1) Edit your StackInt.java file so that it only contains the following:

class StackInt {
}

2) Compile it.
2a) If it doesn't compile correctly, fix those errors FIRST before adding new code.
3) Add a small amount of new code. Say, a main() method. Your class now looks like this:

class StackInt {
    public static void main(String[] args) {
        System.out.println("hello world!");
    }
}

4) Compile it. Then run it. If it doesn't compile, fix those errors before continuing. If it compiles, then run it. You should see it print out "hello world!". This tells you it was run successfully. If there's no output, then you know somethings wrong, and you'll have to fix that before continuing.

In this way, you take "baby steps" - you add just a small amount of code each time, and then compile it and run it to make sure it works the way you expect.

Doing programs this way has been really helpful to me. You can work on one method at a time, instead of typing ALL of them in and wondering where it fails. I recommend it.

Edit:
Since you already have code written, you could adapt this "baby steps" approach by commenting out most of your code so that you don't waste it. Use /* and */ to comment out entire sections - this way the compiler ignores it and you can go about trying out one piece at a time. It looks like this:

class StackInt {
    /*
    this is "commented out"
    push(int number) {

    }
    */

    public static void main(String[] args) {
        System.out.println("hello world!");
    }
}

Hope this helps.

weiji
thnx a lot cheers :)
@rachel If you thought this was the best answer to your question you can mark it as the accepted answer.
ESRogs