3 Replies - 3851 Views - Last Post: 18 September 2012 - 07:20 AM Rate Topic: -----

#1 hcs0112358  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 12

Check values in array, empty or full

Posted 17 September 2012 - 10:18 PM

I've built a stack program in Java, where you input 1, 2, or 3, to push, pop, or exit, respectively. I need to add methods to check if the array is either empty or full as exceptions.

so far I have:

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



When I try to compile, it says it expects a return or a boolean. I'm not really sure how to fix it. I understand the idea of what I'm trying to do, just not quite sure how it translates.

Is This A Good Question/Topic? 0
  • +

Replies To: Check values in array, empty or full

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Check values in array, empty or full

Posted 17 September 2012 - 10:47 PM

Have you declared a boolean empty and full in your stack class? If you haven't then it is telling you that they are missing

You don't have to assign the boolean result to a variable before you return it, you can simply write return true/return false

The last thing I wanna note is this line
if (top = 0)



You are assigning top to 0 instead of checking it with ==

public boolean isEmpty()
{
	return top == 0;
}

public boolean isFull()
{
	return stackArray.length == 100;
}



Also I have my doubt that isFull should be checking on stackArray.length == 100, but I will let that be up to you :)

This post has been edited by CasiOo: 17 September 2012 - 10:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 hcs0112358  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 12

Re: Check values in array, empty or full

Posted 17 September 2012 - 11:09 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Check values in array, empty or full

Posted 18 September 2012 - 07:20 AM

Let's take a look at the original code for the isFull() method for your stack:
    public boolean isFull()
    {
        if (stackArray.length == 100)
            return true; //Notice I modified this
    }



What happens here is the following: suppose we have a full array (one which contains 100 elements), the method then returns true.
However, what if the array is not full (let's say it contains less than 100 elements), then the if statement results in false and your method ends. Basically, your method can be written as this:

    public boolean isFull()
    {
        if (stackArray.length == 100)
            return true;
	else
		//??? What should I return (we know this should be a false
    }



Therefore, we can conclude that the method does not return a (boolean) value when the stack is not full. And because methods always need to return a value, even when if statements are false, this gives you your compiling error.

So, how do we fix this? Well, we fill in the else part. Many methods are possible, including the one CasiOo proposed.

To give you a sample:

// Method #1
// Return value based on the if evaluation
public boolean isFull()
{
	if (stackArray.length == 100)
		return true; 
	else
		return false;
}

// Method #2
// Return values based on the temporary variable
public boolean isFull()
{
	bool isFull = false; //Assume that the stack is not full
	if (stackArray.length == 100)
		isFull = true; //if it passes the if statement, the stack is full

	//Return our findings
	return isFull;
}


Hope this helps you understanding it

Edit
Also, following points:
if (test.isFull() == true)



if you look at line 16, you are creating a new stick every iteration of your while loop. Therefore no matter what, your stack will not get full as you are never adding the elements to the same stack object.

Another point I found was
if (empty == true)



Will give you a compilation error, as the variable empty is never initialiyed or declared. What you want to do is call the isEmpty() method on your stack called "stack1" or "test"

This post has been edited by karabasf: 18 September 2012 - 07:28 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1