Solved

Storing point coordinates in a Stack

Posted on 2011-09-14
10
492 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now