Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Storing point coordinates in a Stack

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
Smanyx
Asked:
Smanyx
2 Solutions
 
Gurvinder Pal SinghCommented:
can you share the Stack class as well?
0
 
SmanyxAuthor Commented:
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
 
CEHJCommented:
You're not setting the instance variables to the value of the parameters
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
SmanyxAuthor Commented:
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
 
CEHJCommented:
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
 
gordon_vt02Commented:
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
 
SmanyxAuthor Commented:
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
 
CEHJCommented:
I gave the reason for the problem before the OP decided to close it
0
 
CEHJCommented:
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now