Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Storing point coordinates in a Stack

Posted on 2011-09-14
10
511 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 IE Certificate Issue 10 51
MySQL  on Tomcat 8 68
how to see all occupied ports on windows 10 laptop 15 76
Tomcat: Unable to run tomcat service. 2 23
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

861 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