# Working with stacks, methods, and switch statements

Page 1 of 1

## 11 Replies - 5807 Views - Last Post: 17 September 2012 - 10:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291856&amp;s=df9447246c2555085a42859d3f58d051&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hcs0112358

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

# Working with stacks, methods, and switch statements

Posted 13 September 2012 - 10:50 AM

Here is the code so far. Most of it was given, and I have to modify it.

```//Class Main
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();
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”); }
}

//Class Stack (assertions go in the blank lines)
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];
}
}

```

What I need help with:

-Adding 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

```

-Modifying the switch statement in class Main to handle the methods by adding new cases for calling methods isEmpty() and isFull().

-Also modifying 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.

-Including assertions in the class Stack that show that it's correct.

*I'm not looking for answers, but I would love help understanding the concepts that are being asked and how to implement them. I'm still relatively brand new to Java.

Is This A Good Question/Topic? 0

## Replies To: Working with stacks, methods, and switch statements

### #2 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 11:08 AM

The two methods your professor wants you to add, are helper methods. The names of them should be fairly obvious as to what they need to be checking. isEmpty(), this should method should tell you whether or not there are any elements in your stack. isFull(), means there is some capacity to your stack, and you need to determine if the number of elements in your stack is equal to that capacity. In both cases, your going to want to return a boolean from them.

Once you have these methods implemented, you prof wants you to go back through your code and utilize them in the apropriate spots. When talking in terms of algorithms, there is 2 main components. the Main part and an Edge case. A classic example is the division operation. 4 / 2 is 2. that is a main operation, but an edge case would be 4 / 0. We know we can't divide by zero, so we have to do something special. That concept applies to adding/removing items from a stack. An edge case would be when calling remove() with an empty list. What should happen, how can we plan for it?

### #3 hcs0112358

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

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 11:23 AM

Okay, that makes sense. Some more specific questions:

1) I'm not sure how switches work... I know it's similar to an if/then or if/else if statement, but with multiple cases. I don't really understand what it actually does. Also in the context of my code, I think I need to add new cases for if it's full or if it's empty, right? Similar to the default part.

2) For the stack, the array has 100 elements, so to make a method that says it's full, I need to check if all the elements are filled? And if it's empty, I need to do the opposite? If so, I'm not really sure how to do that. I'm also really fuzzy on arrays.

"-Also modifying 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."

I think it also has something to do with that, where the method has to either not push or not pop, but I don't know how to integrate that in the switch statement.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 11:30 AM

```   switch(x) {
case 0:
...
break;
case 1:
...
break;
case 2:
...
break;
default:
...
break;
}

```

is equivalent to
```     if(x == 0) {
...
} else if(x == 1) {
...
} else if(x == 2) {
...
} else {
...
}

```

hcs0112358, on 13 September 2012 - 02:23 PM, said:

2) For the stack, the array has 100 elements, so to make a method that says it's full, I need to check if all the elements are filled? And if it's empty, I need to do the opposite? If so, I'm not really sure how to do that. I'm also really fuzzy on arrays.

You have a vatriable top for that. When top == 0 stack if empty
I let you guess what is its value when the stack is full

### #5 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 11:35 AM

1) Yes switch and if/else are very similar. Most argument between them is usually code readability. The way you are doing it makes it very easy to maintain a multiple choice user-input. the two pieces of code evaluate to the same thing:
```switch( choice )
{
case 1:
//code
break;
case 2:
//code
break;
default:
//Not an option
break;
}

```

```if( choice == 1 )
{
//code
}
else if( coice == 2 )
{
//code
}
else
{
//Not an option
}

```

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

This post has been edited by SwiftStriker00: 13 September 2012 - 11:36 AM

### #6 hcs0112358

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

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 11:39 AM

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.

### #7 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 11:45 AM

Quote

Or am I supposed to access the methods isEmpty() or isFull()?

yes, your switch statement should have no visibility to the array.

The way you access a particular element in an array is by using bracket notation:
```int[] arr = new int[]{ 1, 2, 3};
//get 1st element
int first = arr[0];
```

When working with arrays, remember that we start at index 0. So if the size is 3, then the following indexes will exist: 0,1,2

### #8 hcs0112358

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

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 11:48 AM

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.

### #9 hcs0112358

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

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 11:56 AM

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.

### #10 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Working with stacks, methods, and switch statements

Posted 13 September 2012 - 12:07 PM

hcs0112358, on 13 September 2012 - 02:56 PM, said:

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?

If you were to create a function that prints out our arrays then,
Mine : [1, 2, 3]
Yours: [0, 0, 0] // 100 times
A better example would be Object[] because it would print out: [null, null, null]
Basically my code was a shorthand way of adding some number to the array. When you say new int[100], Java will allocate (set aside) space in memory for 100 integers. It will not actually put anything in those spots (int is an exception and insert a default value of 0).

----

In your stack class you need to create these two methods:
public boolean isEmpty()
public boolean isFull()

Once you have done that, in your swtich statement code you can then follow this logic:
```case 1:
if( stack is full )
{
}else{
get push value
push the value into the stack
}

```

### #11 hcs0112358

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

## Re: Working with stacks, methods, and switch statements

Posted 17 September 2012 - 10:08 PM

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.

### #12 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Working with stacks, methods, and switch statements

Posted 17 September 2012 - 10:50 PM

Its throwing those errors, because there are possible scenarios where the code will return nothing. Lets say you call isEmpty, and top != 0. The code will skip the "empty = true". Also your if statement should be if( top == 0 ), otherwise top will be set to 0 every time you call it. Lastly, regardless of what variables you set, you still need to return a boolean value.
```public boolean isEmpty()
{
if( top == 0 )
return true;
return false;
}

```