hcs0112358's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
9 (0.01 per day)
Joined:
13-September 12
Profile Views:
538
Last Active:
User is offline Oct 08 2012 12:02 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   hcs0112358 has not set their status

Posts I've Made

  1. In Topic: Check values in array, empty or full

    Posted 17 Sep 2012

    This is all of the code I have so far:
    public class Stack 
    {
        private int[] stackArray = new int[100]; 
        private int top = 0;
        private boolean full = false;
        private boolean empty = false;
        
        public void push(int pushValue) 
        {
            stackArray[top] = pushValue;
            top++;
        }
    
        public int pop()
        {
            top--;
            return stackArray[top];
        } 
        
        public void isFull()
        {
            if (stackArray.length == 100)
                full = true;
        }
        
        public void isEmpty()
        {
            if (top == 0)
                empty = true;
        }
    }
    
    


    However, I'm also trying to access it with a switch statement in another class, with this code:
    import java.util.Scanner;
    public class Main
    {
        public static void main() 
        {
            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();
                Stack test = new Stack();
                
                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 (test.isFull() == true)
                    {
                        System.out.println("Sorry, you can't add any more!");
                        System.out.println();
                    }
                    else
                    {
                        pushValue = keyboard.nextInt();
                        stack1.push(Pushvalue);
                        System.out.println();
                    }
                    case 4:
                    if (empty == true)
                    {
                        System.out.println("There's nothing to pop!");
                        System.out.println();
                    }
                    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"); }
    }
    


    Particularly the part that is difficult is cases 3 & 4, because I'm not sure how to access the method using them appropriately. It says I can't use void there, but if I change it to public boolean, it says I need a return. If I add a return, it still says I need one, so I'm confused.

    I put the different ways I have so far in the code so you can see.
  2. In Topic: Working with stacks, methods, and switch statements

    Posted 17 Sep 2012

    I'm not sure why this code isn't working/compiling. It's the methods for isEmpty and isFull

        public boolean isEmpty()
        {
            if (top = 0)
                empty = true;
        }
        
        public boolean isFull()
        {
            if (stackArray.length == 100)
                full = true;
        }
    
    


    I'm fairly sure I know how to edit the cases now though, but I'm still having problems checking. It says it's expecting a boolean and a return statement.
  3. In Topic: Working with stacks, methods, and switch statements

    Posted 13 Sep 2012

    Okay, I'm still confused though. I think I either make a boolean method or a loop statement, saying if the first element is null, then I can't pop. I don't understand how to make that work for mine though, probably because I don't understand the different parts of how to access it.

    Can you explain this part:
    int[] arr = new int[]{ 1, 2, 3};
    //get 1st element
    int first = arr[0];
    
    


    so, mine is:
    private int[] stackArray = new int[100]; //makes a new array
    
    


    That makes the new array, but yours does that too, so how do I use that to access the elements?

    In class they talked about a loop statement that does something like if (top == null or top ==100-1) then do nothing but print that you can't do that.

    I feel like a boolean would be simpler, but I'm not sure how the code works for that.

    I have the concept down, but don't know how to translate that to code.
  4. In Topic: Working with stacks, methods, and switch statements

    Posted 13 Sep 2012

    2) You have two options for checking to see if the array is full. You could loop through the array, and count the number of elements that are not null, if that number doesn't equal the .length, then its not full. However a better options is to create a class variable called something like counter(Edit: you already have top to do it for you). Then increment that during push, and decrement it during pop. This way all you'll have to do is just check the counter number against the .length. Get creative this can also help you track where you should insert your next element on a push.

    If your stack is empty, should the first element be null? (do the rest matter?)

    -- Lastly your prof wants you to use the isFull() empty to determine if you can or cannot use the push method. if its full tell user otherwise insert the new number

    How do I loop through an array using top?

    Top is already the variable used for the stack, so that makes sense. How do I make a method that counts it during push and pop?

    Also, the way I think of the method to check is if the number of elements is null or is 100, so I don't know how to use the .length.

    So, I'd only need to check if the first element is null for the isEmpty() method, right?

    The method for isFull() has to figure out if the number of elements == 100 - 1, I think. Then it doesn't do anything except print that it's full? That's the boolean method.
  5. In Topic: Working with stacks, methods, and switch statements

    Posted 13 Sep 2012

    Okay, that makes sense. How do I access the array in the switch statement? Like, how do I make the case (stackArray == [0])? Or case (stackArray == [100])

    I know that can't be right, but I'm trying to figure out how to access how many elements are filled in the array... In each case.

    Or am I supposed to access the methods isEmpty() or isFull()? I still don't know how to make those figure out how many elements there are in the array.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

hcs0112358 hasn't added any friends yet.

Comments

hcs0112358 has no profile comments yet. Why not say hello?