Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: how to do this ? stack question

  1. #1
    june_c21 is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default how to do this ? stack question

    hi , i got 2 source code but i don't know how to integrate this . can someone help me in this ?


    import static javax.swing.JOptionPane.*;

    public class Coursework {

    public static void main(String[] args){
    int key;
    do{
    String text = "Please select";
    text += "\n(1) Push Item";
    text += "\n(2) Pop Item";
    text += "\n(3) Check item on top";
    text += "\n(4) See all items in stack";
    text += "\n(0) Exit";
    String strKey = showInputDialog(null,text);
    key = Integer.parseInt(strKey);

    }while(key != 0);


    }

    }




    public class Stack {
    public static char[] Array1 = new char[4];
    public static int top = -1; //no item in stack

    //push
    public static void push(char item){
    //check whether the stack is full or not
    if(top==Array1.length-1){
    System.out.println("Stack is full!");
    }
    else{ //if not full, then can push
    top++;
    Array1[top] = item;
    }
    }

    //pop
    public static void pop(){
    //check whether the array is empty or not
    if(top==-1){
    System.out.println("Stack is empty");
    }
    else{ //if not empty, then can pop
    System.out.print(Array1[top]);
    top--;
    }
    }

    //peek
    public static void peek(){
    //check whether the array is empty or not
    if(top==-1){
    System.out.println("Stack is empty");
    }
    else{ //if not empty, then can pop
    System.out.println("\nThe top item is: "+Array1[top]);
    }
    }

    public static void main(String[] args){
    push('A');
    push('B');
    push('C');
    pop();
    push('E');
    pop();
    peek();
    //display stack
    System.out.println("\nItem in the stack");
    for(int i=0; i<=top; i++){
    System.out.print(Array1[i]+" ");
    }
    System.out.println();
    }
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: how to do this ? stack question

    When posting code please wrap it in [code] tags [/code] so it retains its formatting.

    Looking at that code you need to create an instance of Stack in Coursework (Stack stack = new Stack()) before the while loop and then call the various methods on it depending on what the user selects.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    june_c21 is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Re: how to do this ? stack question

    Quote Originally Posted by Tolls View Post
    When posting code please wrap it in [code] tags [/code] so it retains its formatting.

    Looking at that code you need to create an instance of Stack in Coursework (Stack stack = new Stack()) before the while loop and then call the various methods on it depending on what the user selects.

    hmmmm...... can you show me how ?
    i very new to java and don't know how ....

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: how to do this ? stack question

    Try and then we can work from there.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    june_c21 is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Re: how to do this ? stack question

    Why when i enter a char after select 1 , then see item in stack , the stack is empty ....

    Java Code:
    package datacw;
    import static javax.swing.JOptionPane.*;
    public class Coursework {
        public static void main(String[] args){
            int key;
               do{
                   //Selection for user
                   String text = "Please select";
                   text += "\n(1) Push Item";
                   text += "\n(2) Pop Item";
                   text += "\n(3) Check item on top";
                   text += "\n(4) See all items in stack";
                   text += "\n(0) Exit";
                   String strKey = showInputDialog(null,text);
                   key = Integer.parseInt(strKey);
                   if(key==1){
                       //Print out a dialog box allowing user to key in value
                       String getChar = showInputDialog("Enter a character");
                        if(getChar.length()==0){
                            showMessageDialog(null,"You have keyed in an invalid word");
                        }
                        else{
                            char userInput=getChar.charAt(0);
                            push(userInput);
                        }
                   }
                   if(key==2){
                       pop();
                   }
                   else if(key==3){
                       peek();
                   }
                   else if(key==4){
                       display();
                   }
                   
                   
               }while(key != 0);
           if(key==0){
                showMessageDialog(null,"Press OK to exit");
        }
        }       
    public static char[] userInput = new char[10];
    public static int top = -1; //no item in stack
    
        //Push method
    public static void push(char item){
        //check whether the stack is full or not
        if(top==userInput.length-1){
            showMessageDialog (null, "The stack is full!" );
        }
    }
    //Pop method
    public static void pop(){
        // check whether the array is empty or not
        if(top==-1){
            showMessageDialog(null,"The stack is empty");
            
        }
        else{  //if not empty, then will pop
            showMessageDialog(null,"Popped a character : " +userInput[top]);
            top--;
        }
        }
    
    //Peek Method
    public static void peek(){
        //check whether the array is empty or not
        if(top==-1){
            showMessageDialog(null,"Stack is empty");
        }
        else{ //if not empty then will pop
            showMessageDialog(null,"\nThe top item is: " +userInput[top] );
             
        }
    }
    //Display all characters in the stack
    public static void display(){
        if(top==-1){
            showMessageDialog(null,"Stack is empty!");
        }
        else{
            
            for(int i=0; i<=top; i++){
                showMessageDialog(null,"Item in the stack is : "+userInput[i]);
                
            }
        }
    System.out.println();
    }
    }

  6. #6
    june_c21 is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Re: how to do this ? stack question

    Problem Solved !! Thanks all

Similar Threads

  1. Replies: 2
    Last Post: 08-23-2013, 06:15 AM
  2. Stack
    By billjohn2 in forum New To Java
    Replies: 1
    Last Post: 10-03-2012, 02:33 AM
  3. Replies: 2
    Last Post: 01-14-2012, 09:56 PM
  4. Stack with nodes question
    By mackavelirip in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2011, 02:59 PM
  5. How big is the stack??
    By mgeno216 in forum New To Java
    Replies: 6
    Last Post: 03-04-2011, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •