Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Storing point coordinates in a Stack

Posted on 2011-09-14
10
Medium Priority
?
537 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
[X]
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
10 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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 1200 total points
ID: 36535537
You're not setting the instance variables to the value of the parameters
0
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!

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.
Suggested Courses

664 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