Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need help fast!!! 2 hours left!!! simple stack ADT

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Need help fast!!! 2 hours left!!! simple stack ADT

    I am sooooooooo sorry if you don't like posts like this but I am in trouble i have homework due in 2 hours need help fast:

    Add two new methods to class stack:

    isEmpty() // returns true if the stack is empty; false otherwise
    isFull() // returns true if the stack is full; false otherwise

    Modify the switch statement in class Main to handle exercising of these two new methods by adding new cases for calling methods isEmpty() and isFull()
    Also modify the cases in the switch statement that call push() and pop() so that the call is not made to push() if stack is full and the call to pop() is not made if the stack is empty

    I am obviously in over my head with this class but im stuck because if i drop it i drop bellow 12 credits and god knows what finical aid will do to me if that happens please help me with my homework tonite i struggled for like 3 hours trying to figure it out on my own and I just couldn't get it to work!!!

    import java.util.Scanner;
    public class firstproject {
    	public static void main(String[] args) 
    	 {
    	     int choice, pushValue;
    	     boolean done = false;
    	     Stack stack1 = new Stack();
    	     Scanner keyboard = new Scanner(System.in);
     
    	     while (!done)
    	     {
    	         System.out.print("Enter 1 to push, 2 to pop, and 3 to quit: ");
    	         choice = keyboard.nextInt();
    	         System.out.println();
     
    	         switch (choice)
    	         {
    	             case 1:
    	                 System.out.print("Enter an integer to push: ");
    	                 pushValue = keyboard.nextInt();
    	                 stack1.push(pushValue);
    	                 System.out.println();
    	                 break;
    	             case 2:
    	                 System.out.println("The top value on the stack was: " +
    	                                    stack1.pop());
    	                 System.out.println();
    	                 break;
    	             case 3:
    	                 done = true;
    	                 break;
    	             default:
    	                 System.out.println("The number you entered, "+ choice +
    	                                    ", + is not 1, 2, or 3. Try again!");
    	                 System.out.println();
    	                        break;
     
    	         }
     
    	     }
    	     System.out.println("...quitting");
    	    }
    	}



    Second class:


    public class Stack {
        private int[] stackArray = new int[100];
     
        private int top = 0;
     
        public void push(int pushValue)
        {
            stackArray[top] = pushValue;
     
            top++;
     
        }
     
        public int pop()
        {
     
            top--;
     
            return stackArray[top];
        }
    }
    Last edited by helloworld922; September 16th, 2012 at 03:25 AM. Reason: please use [code] tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help fast!!! 2 hours left!!! simple stack ADT

    It looks like you're assignment is already due, but here are some suggestions for you so that you can receive useful help in the future:

    1. Start early. This can't be over-stated enough. Internet forums have relatively slow response times. We can't spend all of our time sitting at our computers hitting the refresh button to answer your question right when you ask it. A good rule of thumb to follow is post at least 48 hrs before your assignment is due, with the longer the better.

    2. Seek help from fellow classmates/the teaching staff first. They will have the most experience with what you are working on at the time. We will try our best to help you, but these other sources can be faster and provide more relevant information for your assignment.

    3. You said your program doesn't work. Why doesn't it work? Is there a compiler error? Runtime exception? Just not giving you the right output? Explain with as much detail what the problem is. Posting the exact output from the compiler/program is usually a good way to do this.

    4. Drop the sense of urgency. This goes along with no. 1. You're right that some members don't like answering questions which say "urgent" or "due in 2 hrs". The fact is it doesn't add any useful information to us helping you solve the problem. It also is discourteous to other users who have questions as you're attempting to say "my question is more important/urgent than your question". Even if it may be urgent, post as if it isn't.

    5. Format your post in a "professional manner". This means use proper English (this is not the place for abbreviations/word miss-spellings like lol, wat, hlp, etc.), format your code using [code] tags, and stick with standard text as much as possible (you don't need to re-size your text, give it funky colors, use all-caps, etc.).

    There are two good articles under the forum rules which will help you get the most use out of online forums:

    How to program (Stuck on homework? Read this before posting)
    How to help yourself get help
    Last edited by helloworld922; September 16th, 2012 at 03:47 AM.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    copeg (September 16th, 2012)

  4. #3
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Need help fast!!! 2 hours left!!! simple stack ADT

    Quote Originally Posted by helloworld922 View Post
    It looks like you're assignment is already due, but here are some suggestions for you so that you can receive useful help in the future:

    1. Start early. This can't be over-stated enough. Internet forums have relatively slow response times. We can't spend all of our time sitting at our computers hitting the refresh button to answer your question right when you ask it. A good rule of thumb to follow is post at least 48 hrs before your assignment is due, with the longer the better.

    2. Seek help from fellow classmates/the teaching staff first. They will have the most experience with what you are working on at the time. We will try our best to help you, but these other sources can be faster and provide more relevant information for your assignment.

    3. You said your program doesn't work. Why doesn't it work? Is there a compiler error? Runtime exception? Just not giving you the right output? Explain with as much detail what the problem is. Posting the exact output from the compiler/program is usually a good way to do this.

    4. Drop the sense of urgency. This goes along with no. 1. You're right that some members don't like answering questions which say "urgent" or "due in 2 hrs". The fact is it doesn't add any useful information to us helping you solve the problem. It also is discourteous to other users who have questions as you're attempting to say "my question is more important/urgent than your question". Even if it may be urgent, post as if it isn't.

    5. Format your post in a "professional manner". This means use proper English (this is not the place for abbreviations/word miss-spellings like lol, wat, hlp, etc.), format your code using [code] tags, and stick with standard text as much as possible (you don't need to re-size your text, give it funky colors, use all-caps, etc.).

    There are two good articles under the forum rules which will help you get the most use out of online forums:

    How to program (Stuck on homework? Read this before posting)
    How to help yourself get help
    Yeah I am sorry guys. I do post in a lot of forums, and so I know the rules (in general) but I was definitely freaked out. The ironic thing is that I found out the homework is not due until Monday night. So here is the thing the program as written up top is working. However, I haven't been able to get the new add-on's that he wants to work. So I was hoping someone could tell me how I might go about doing it. I understand that I need to add an "if" statement but I tried a few things and they did not work. For instance I tried (in public class stack):

    public void isEmpty(){
    if (top < 0)
    system.out.println("Stack is Empty")}

    and the case I tried to add in the public class firstproject to handle the new method was:

     case 4:
    isEmpty() = true

    That among a few other things that did not work. I tried looking at like four or five examples of stacks that I found on the internet. Some of which had similar "isEmpty" and "isFull" methods but I could not seem to apply them to my code correctly.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help fast!!! 2 hours left!!! simple stack ADT

    Try to write out what it is you want to do on a piece of paper. Start with the broader, more abstract ideas and work down to the details:

    You need two new methods, an isEmpty and isFull method. It looks like you're isEmpty() example provides direct user feedback through the command-line, but it doesn't provide a way for your main method code to tell if the stack is empty.

    The best way to do this is by returning a value. Conveniently, there is a boolean data type in Java. Booleans are variables which can hold the value true or false so they are ideally suited for answering yes/no type questions.

    To declare a method with a return value:

    method_modifiers return_type method_name(params)
    {
        // method code goes here
    }

    Here, method_modifiers are any keyword modifiers to the method such as public/protected/private, static, etc. return_type is the type of type of value the method is expected to return.

    Here's an example method:

    public int length()
    {
        // method code goes here
    }

    The name of the method is length, it returns an int, and it takes no method parameters.

    In your code, you can return values using the return keyword. You can use return statements as much as you would like in a method, however you must satisfy this one conditions:

    Every possible code path must end in either an throw or a return statement.

    Once you have code implementing the isFull() and isEmpty() methods, create a test application which tests to make sure these work correctly. For beginners one of the best ways to do this is to create a new class with it's own main method. In this class you can create a stack and push/pop values from the stack, calling the isFull() or isEmpty() methods each time you modify the stack. Work out what the correct return values are suppose to be and compare these to the test program's outputs.

    Ex. test class:

    public class TestClass
    {
        public static void main(String[] args)
        {
            Stack s = new Stack();
            // stack should be empty, check to see that it is
            System.out.println("stack should be empty. isEmpty() returned: " + s.isEmpty());
            // stack should not be full
            System.out.println("stack should not be full. isFull() returned: " + s.isFull());
     
            // add a value to the stack
            System.out.println("pushing a value.");
            stack.push(1);
            // stack should not be empty
            System.out.println("stack should not be empty. isEmpty() returned: " + s.isEmpty());
            // stack should not be full
            System.out.println("stack should not be full. isFull() returned: " + s.isFull());
     
            // push back 99 more values
            System.out.println("pushing 99 more values.");
            for(int i = 0; i < 99; ++i)
            {
                stack.push(i);
            }
            // stack should not be empty
            System.out.println("stack should not be empty. isEmpty() returned: " + s.isEmpty());
            // stack should be full
            System.out.println("stack should be full. isFull() returned: " + s.isFull());
     
            // pop back 99 values
            System.out.println("Popping 99 values.");
            for(int i = 0; i < 99; ++i)
            {
                stack.pop();
            }
     
            // stack should not be empty
            System.out.println("stack should not be empty. isEmpty() returned: " + s.isEmpty());
            // stack should not be full
            System.out.println("stack should not be full. isFull() returned: " + s.isFull());
     
            // pop the last value
            System.out.println("popping last value.");
     
            // stack should be empty, check to see that it is
            System.out.println("stack should be empty. isEmpty() returned: " + s.isEmpty());
            // stack should not be full
            System.out.println("stack should not be full. isFull() returned: " + s.isFull());
        }
    }

    If you still are having problems implementing the isFull() or isEmpty() methods, let us know. Post any code you have as well as any outputs from the compiler (errors) or console.

  6. #5
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Need help fast!!! 2 hours left!!! simple stack ADT

    Quote Originally Posted by helloworld922 View Post
    If you still are having problems implementing the isFull() or isEmpty() methods, let us know. Post any code you have as well as any outputs from the compiler (errors) or console.
    Here is what I have so far:

    import java.util.Scanner;
    public class firstproject {
    	public static void main(String[] args) 
    	 {
    	     int choice, pushValue;
    	     boolean done = false;
    	     Stack stack1 = new Stack();
    	     Scanner keyboard = new Scanner(System.in);
     
    	     while (!done)
    	     {
    	         System.out.print("Enter 1 to push, 2 to pop, and 3 to see if stack is empty and 4 to see if stack is full. : ");
    	         choice = keyboard.nextInt();
    	         System.out.println();
     
    	         switch (choice)
    	         {
    	             case 1:
    	                 System.out.print("Enter an integer to push: ");
    	                 pushValue = keyboard.nextInt();
    	                 stack1.push(pushValue);
    	                 System.out.println();
    	                 break;
    	             case 2:
    	                 System.out.println("The top value on the stack was: " +
    	                                    stack1.pop());
    	                 System.out.println();
    	                 break;
     
    	             case 3: if (stack1.isEmpty())
    	            		 System.out.println("The stack is empty.");
    	            	 else 
    	            		 System.out.println("The stack is not empty.");
    	            	 break;
     
    	             case 4: if (stack1.isFull())
    	            	 System.out.println("The stack is full.");
    	             else
    	            	 System.out.println("The stack is not full.");
    	             break;
     
    	             case 5:
    	                 done = true;
    	                 break;
     
     
    	             default:
    	                 System.out.println("The number you entered, "+ choice +
    	                                    ", + is not 1, 2, or 3. Try again!");
    	                 System.out.println();
    	                        break;
     
    	         }
     
    	     }
    	     System.out.println("...quitting");
    	    }
    	}

    and for class stack:

    public class Stack {
        private int[] stackArray = new int[100];
     
        private int top = 0;
     
        public void push(int pushValue)
        {
            stackArray[top] = pushValue;
     
            top++;
     
        } 
     
        public int pop()
        {
     
            top--;
     
            return stackArray[top];
        }
     
        public boolean isEmpty()
        {
        	return (top==0);
        }   
     
        public boolean isFull()
        {
        	return (top==100);
        	}
    }

    It seems to be working however I am still having trouble, cause I am not sure if I already did this or if he is asking for something else:

    "Also modify the cases in the switch statement that call push() and pop() so that the call is not made to push() if the stack is full and the call to pop() is not made if the stack is empty."
    Last edited by vysero; September 17th, 2012 at 06:25 PM.

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need help fast!!! 2 hours left!!! simple stack ADT

    Have you tried adding to the stack until it is full? ...and then tried to add again, what happens?
    Does your code prevent the addition of a new element when the stack is full from inside the switch statement?
    Run some tests and see if the code performs to the specifications you were given. Testing your code is just as important, if not more important, than writing the code.

Similar Threads

  1. Sending data too fast
    By polyfrag in forum Java Networking
    Replies: 1
    Last Post: August 10th, 2012, 04:28 PM
  2. need HELP FAST PLEASE HELP ME
    By britben95 in forum Member Introductions
    Replies: 1
    Last Post: November 14th, 2011, 08:05 PM
  3. need help fast
    By coke32 in forum Loops & Control Statements
    Replies: 6
    Last Post: October 31st, 2011, 11:04 PM
  4. need help fast!!
    By nwollis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2010, 05:12 PM
  5. Code stopping, need help fast.
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2010, 09:00 AM