Solved

Storing point coordinates in a Stack

Posted on 2011-09-14
10
502 Views
Last Modified: 2012-05-12
Hi Experts,

I am trying to store some coordinates (from a grid) into a Stack data structure.
I have two classes: MyStack.java and MyPoints.java, the latter extends Point
public class MyStack
{
     static void showPush(Stack stack, Mypoint pt)
      {
            stack.push(pt);
            
            System.out.println("push(" + pt + ")");
            System.out.println("stack: " + stack);
      }
      static void showPop(Stack st)
      {
            System.out.print("pop -> ");
            
            Mypoint pt = (Mypoint) st.pop();

            System.out.println(pt);
            System.out.println("stack: " + st);
      }
}

From main, I am having the following implementation:
public static void main(String args[])
      {
            Stack st = new Stack();
            Mypoint point = new Mypoint(3,1);
            Mypoint point2 = new Mypoint(3,2);
            Mypoint point3 = new Mypoint(5,1);
            
            System.out.println("stack: " + st);

            showPush(st, point);
            showPush(st, point2);
            showPush(st, point3);
            showPop(st);
            showPop(st);
            showPop(st);
            
            try {
            showPop(st);
            } catch (EmptyStackException e) {
            System.out.println("empty stack");
            }
      }

I am getting weird output rather... Could you please guide me achieve this. Thanks.

OUTPUT:
stack: []
push(org.me.stack.Mypoint[x=0,y=0])
stack: [org.me.stack.Mypoint[x=0,y=0]]
push(org.me.stack.Mypoint[x=0,y=0])
stack: [org.me.stack.Mypoint[x=0,y=0], org.me.stack.Mypoint[x=0,y=0]]
push(org.me.stack.Mypoint[x=0,y=0])
stack: [org.me.stack.Mypoint[x=0,y=0], org.me.stack.Mypoint[x=0,y=0], org.me.stack.Mypoint[x=0,y=0]]
pop -> org.me.stack.Mypoint[x=0,y=0]
stack: [org.me.stack.Mypoint[x=0,y=0], org.me.stack.Mypoint[x=0,y=0]]
pop -> org.me.stack.Mypoint[x=0,y=0]
stack: [org.me.stack.Mypoint[x=0,y=0]]
pop -> org.me.stack.Mypoint[x=0,y=0]
stack: []
org.me.stack.Mypoint[x=0,y=0]
pop -> empty stack
0
Comment
Question by:Smanyx
10 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36535215
can you share the Stack class as well?
0
 

Author Comment

by:Smanyx
ID: 36535493
Sorry for the delay...
I have supplied the MyStack class, may be you mean MyPoint class ??
It's just a simple class

import java.awt.*;
public class MyPoint extends Point
{
     private int X;
     private int Y;
     //constructor
      public Point (int a, int b)
      {

      }

}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 36535537
You're not setting the instance variables to the value of the parameters
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Assisted Solution

by:Smanyx
Smanyx earned 0 total points
ID: 36535832
I think I was getting confused myself a bit.
I realized I didn't need to have MyPoint class at all.
So, I solved my problem by getting rid of MyPoint class and  just by having only ONE class MyStack.java and importing java.awt.*;
 package org.me.stack;

import java.util.*;
import java.awt.*;
public class Mystack 
{
	
	@SuppressWarnings({ "rawtypes", "unchecked" }) 
	static void showPush(Stack stack, Point pt)
	{
		stack.push(pt);
		
		System.out.println("push(" + pt + ")");
		System.out.println("stack: " + stack);
	}
	@SuppressWarnings("rawtypes")
	static void showPop(Stack st) 
	{
		System.out.print("pop -> ");
		
		Point pt = (Point) st.pop();
		System.out.println(pt);
		System.out.println("stack: " + st);
	}
	
		
}

Open in new window


And implementing that in main :
package org.me.stack;

import java.util.EmptyStackException;
import java.awt.*;
import java.util.Stack;

public class Main extends Mystack {

	/**
	 * @param args
	 */
	@SuppressWarnings("rawtypes")
	public static void main(String args[]) 
	{
		Stack st = new Stack();
		Point pts = new Point(6,1);
		Point pts2 = new Point(5,1);
		Point pts3 = new Point(5,2);
		Point pts4 = new Point(5,3);
		Point pts5 = new Point(6,2);
				
		System.out.println("stack: " + st);

		showPush(st, pts);
		showPush(st, pts2);
		showPush(st, pts3);
		showPush(st, pts4);
		showPush(st, pts5);
		showPop(st);
		showPop(st);
		showPop(st);
		showPop(st);
		showPop(st);
		
		try {
		showPop(st);
		} catch (EmptyStackException e) {
		System.out.println("empty stack");
		}
	}

}

Open in new window


OUTPUT:
stack: []
push(java.awt.Point[x=6,y=1])
stack: [java.awt.Point[x=6,y=1]]
push(java.awt.Point[x=5,y=1])
stack: [java.awt.Point[x=6,y=1], java.awt.Point[x=5,y=1]]
push(java.awt.Point[x=5,y=2])
stack: [java.awt.Point[x=6,y=1], java.awt.Point[x=5,y=1], java.awt.Point[x=5,y=2]]
push(java.awt.Point[x=5,y=3])
stack: [java.awt.Point[x=6,y=1], java.awt.Point[x=5,y=1], java.awt.Point[x=5,y=2], java.awt.Point[x=5,y=3]]
push(java.awt.Point[x=6,y=2])
stack: [java.awt.Point[x=6,y=1], java.awt.Point[x=5,y=1], java.awt.Point[x=5,y=2], java.awt.Point[x=5,y=3], java.awt.Point[x=6,y=2]]
pop -> java.awt.Point[x=6,y=2]
stack: [java.awt.Point[x=6,y=1], java.awt.Point[x=5,y=1], java.awt.Point[x=5,y=2], java.awt.Point[x=5,y=3]]
pop -> java.awt.Point[x=5,y=3]
stack: [java.awt.Point[x=6,y=1], java.awt.Point[x=5,y=1], java.awt.Point[x=5,y=2]]
pop -> java.awt.Point[x=5,y=2]
stack: [java.awt.Point[x=6,y=1], java.awt.Point[x=5,y=1]]
pop -> java.awt.Point[x=5,y=1]
stack: [java.awt.Point[x=6,y=1]]
pop -> java.awt.Point[x=6,y=1]
stack: []
pop -> empty stack
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36535851
Yes, now the parameters ARE getting set to the Point class since your broken constructor for the subclass is no longer being used. I assumed you were going to do other things with your subclass or you shouldn't have made one in the first place
0
 
LVL 10

Expert Comment

by:gordon_vt02
ID: 36542837
On a side note, it's always better to explicitly import only the classes you need instead of doing a .* import.  That way you avoid unintentional naming conflicts.
0
 

Author Comment

by:Smanyx
ID: 36567589
I've requested that this question be closed as follows:

Accepted answer: 0 points for Smanyx's comment http:/Q_27307186.html#36535832

for the following reason:

Thanks for your help anyway. I do appreciate it.<br />Cheers.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36567590
I gave the reason for the problem before the OP decided to close it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36592326
:)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split string containing \r\n in Java 46 44
web services creation SOAP vs REST 5 41
javap not working 8 45
Java: anonymous class 4 20
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question