9 Replies - 1176 Views - Last Post: 04 December 2011 - 04:22 PM Rate Topic: -----

#1 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Using a Stack in an Application

Posted 04 December 2011 - 01:56 PM

Should be a simple class assignment but for some reason I have struggling with it...any help would be great. Thanks.

Assignment:
Write Java code that displays all of the objects in a stack s in the order in which they were pushed onto the stack. After all of the objects are displayed, s should have the same contents as when you started.

This is what I have so far:

public class Main

{

	public static void main(String[] args)

	{

		Stack s = new Stack();



		System.out.println("Insertion of 10 characters in s");

		for (int i = 0; i < 10; i++)

		{

			int x = 32 + (int)(Math.random()*95);

			System.out.println(x + " --> " + (char)x);

			s.push((char)x);
			s.pop();

		}



		System.out.println("\nDisplaying elements of stack s:");

		for (int i = 0; i < 10; i++)

		{
			s.peek();
			
			
			System.out.println("Item at the top: " + s);

			
			
			s.pop();

		}

	}

}


//week 6 - lecture: Stack class



public class Stack

{

	public Stack()

	{

		size = 100;

		list = new char[size];

		n = 0;

	}



	public Stack(int s)

	{

		size = s;

		list = new char[size];

		n = 0;

	}



	public void push(char c)

	{

		list[n] = c;

		n++;

	}



	public void pop()

	{

		n--;

	}



	public char peek()

	{

		return list[n-1];

	}



	public boolean isEmpty()

	{

		return n==0;

	}



	private char[] list;

	private int size;

	private int n;

}








Is This A Good Question/Topic? 0
  • +

Replies To: Using a Stack in an Application

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Using a Stack in an Application

Posted 04 December 2011 - 02:18 PM


          s.push((char)x);
          s.pop();


you push() on the stack, but you pop() right after
Your stack will always be empty :)

Also to verify that your codwe works better not to push() on the stack randomly generated characters, when you will poke() back you won't be able to validate that you retreive in good order. Better to

for(char c = 'A'; c <= 'I'; c++)

then you will know that you should retreive A through I in which ever order

This post has been edited by pbl: 04 December 2011 - 02:28 PM

Was This Post Helpful? 1
  • +
  • -

#3 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Using a Stack in an Application

Posted 04 December 2011 - 02:23 PM

View Postpbl, on 04 December 2011 - 02:18 PM, said:


          s.push((char)x);
          s.pop();


you push() on the stack, but you pop() right after
Your stack will always be empty :)



Your right! OK after removing "s.pop", this is my output, which is still incorrect:

Insertion of 10 characters in s
73 --> I
110 --> n
115 --> s
56 --> 8
77 --> M
58 --> :
124 --> |
65 --> A
104 --> h
84 --> T

Displaying elements of stack s:
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749
Item at the top: Stack@c3c749



Any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Using a Stack in an Application

Posted 04 December 2011 - 02:30 PM

You print the whole stack

System.out.println("Item at the top: " + s);

not the elemnt you just pop()/peek()

Also better to use generic


Stack<Character> s = new Stack<Character>();
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5578
  • View blog
  • Posts: 11,918
  • Joined: 02-June 10

Re: Using a Stack in an Application

Posted 04 December 2011 - 02:30 PM

009 Stack s = new Stack();
038 System.out.println("Item at the top: " + s);

That's what it looks like when you try to output/display/print an object instead of a string

Its sort of like trying to display a Button instead of the text on a Button.

This post has been edited by tlhIn`toq: 04 December 2011 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Using a Stack in an Application

Posted 04 December 2011 - 02:32 PM

Actually a Stack object not a Character in the stack
Was This Post Helpful? 0
  • +
  • -

#7 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Using a Stack in an Application

Posted 04 December 2011 - 02:48 PM

OK I understand...so what do I need to do so that it displays all of the objects in stack s in the order in which they were pushed onto the stack??

This is what I think needs to happen but I'm unsure how to implement:
Pop all the elements of the stack and Insert them in a second stack. Then peek/print/pop the items of this second stack. As you print, insert back in the initial stack.

I think its the last sentence that is throwing me off.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Using a Stack in an Application

Posted 04 December 2011 - 02:55 PM

To display them in the order they were inserted you can't pop them

You will have to get the stack size and poke() at a ceetain index in reverse order so from size() - 1 to 0

Happy coding

Hint: Stack is a subclass of Vector which has a get(atIndex) method :)
Was This Post Helpful? 0
  • +
  • -

#9 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Using a Stack in an Application

Posted 04 December 2011 - 03:11 PM

View Postpbl, on 04 December 2011 - 02:55 PM, said:

To display them in the order they were inserted you can't pop them

You will have to get the stack size and poke() at a ceetain index in reverse order so from size() - 1 to 0

Happy coding

Hint: Stack is a subclass of Vector which has a get(atIndex) method :)



I'm sorry...for my lack of knowledge on this, but I'm still not getting it. I have tried so many combination's here and I've been at it for hours.

My latest attempt:

//Week 6 - lecture: driver program (stack)



public class Main

{

	public static void main(String[] args)

	{

		Stack s = new Stack();
		


		System.out.println("Insertion of 10 characters in s:");

		for (int i = 0; i < 10; i++)

		{

			int x = 32 + (int)(Math.random()*95);

			System.out.println(x + " --> " + (char)x);

			s.push((char)x);
			

		}



		System.out.println("\nDisplaying elements of stack s:");

		for (int i = 0; i < 10; i++)

		{
			s.peek();
			
			
			
			System.out.println("Item at the top: ");

			
			
		

		}

	}

}

//week 6 - lecture: Stack class



public class Stack

{

	public Stack()

	{

		size = 100;

		list = new char[size];

		n = 0;

	}



	public Stack(int s)

	{

		size = s;

		list = new char[size];

		n = 0;

	}



	public void push(char c)

	{

		list[n] = c;

		n++;

	}



	public void pop()

	{

		

	}



	public char peek()

	{

		return list[n-1];

	}



	public boolean isEmpty()

	{

		return n==0;

	}



	private char[] list;

	private int size;

	private int n;

}




Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Using a Stack in an Application

Posted 04 December 2011 - 04:22 PM

You missed the whhole concept :) Sorry

Why do you clone the Stack methods in your class ? You would never call them anyhow :v:

Here is your homework to close that thread

	public static void main(String[] args) {
		Stack<Character> stack = new Stack<Character>();
		
		// filing up the stack
		for(char c = 'A'; c <= 'I'; c++)
			stack.push(c);
		
		// displaying what is on the stack in order they were pushed
		System.out.println("Stack content");
		for(int i = 0; i < stack.size(); ++i)
			System.out.println("[" + i + "] " + stack.get(i));
		
		// in order they will be pop() from the stack
		System.out.println("Poping fro the stack");
		while(!stack.isEmpty())
			System.out.println(stack.pop());
	}


You do not need any other method, Stack/Vector have them for you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1