Unresolved and not visible with inheritance and objects

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1519 Views - Last Post: 27 March 2013 - 03:49 AM Rate Topic: -----

#16 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7650
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:13 PM

 Cube iceCube = new Cube(iceCube.getX(), iceCube.getY(), iceCube.getSL(), depth);



iceCube doesn't exist at this point - you're just declaring it now - so how can you be calling methods on it?
Was This Post Helpful? 1
  • +
  • -

#17 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:17 PM

Square mySquare = new Square(mySquare.getX(), mySquare.getY(), sideLength);

Tell me.. How do we know what mySquare.x is at this point?

In this scenario, the constructor asks you "How big is the new square going to be?" and you reply "make it as big as the new square is."

This simply cannot work. You need to be giving the new square values that already exist.
Was This Post Helpful? 2
  • +
  • -

#18 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:19 PM

View Postjon.kiparsky, on 26 March 2013 - 02:13 PM, said:

 Cube iceCube = new Cube(iceCube.getX(), iceCube.getY(), iceCube.getSL(), depth);



iceCube doesn't exist at this point - you're just declaring it now - so how can you be calling methods on it?


Ok, I get that. If I can't use methods on it to pass it the right variables for the constructor, how can I use it?

Build it first with dummy local variables and then pass them in?? That seems sloppy, and I probably don't know how to do that anyway.
Was This Post Helpful? 0
  • +
  • -

#19 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7650
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:24 PM

You're getting values from the user... you could use those. Or, if you've constructed those values into some object, like a Point, you could use the object that already exists. (point.getX(), point getY(), etc., are all fine, as long as you've already created point. otherwise, they're kind of, um, pointless... )
Was This Post Helpful? 1
  • +
  • -

#20 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:24 PM

like this?

	public static Square createSquare(Point myPoint) {

		double sideLength = 0.0;
		int dummyX;
		int dummyY;

		while (true) {
			try {
				sideLength = Integer.parseInt(JOptionPane.showInputDialog(null,
						"Please input the length ",
						"The length of one side of the square."));
			} catch (NumberFormatException z) {
				JOptionPane.showMessageDialog(null, "Sorry, invalid input.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}
			if (sideLength < 0) {
				JOptionPane.showMessageDialog(null,
						"The length of the side cannot be less than 0.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}

			break;

		}
		dummyX = myPoint.getX();
		dummyY = myPoint.getY();
		Square mySquare = new Square(dummyX, dummyY, sideLength);
		return mySquare;
	}



Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7650
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:28 PM

or just
Square mySquare = new Square(myPoint.getX(), myPoint.getY(), sideLength);


Was This Post Helpful? 1
  • +
  • -

#22 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:34 PM

View Postjon.kiparsky, on 26 March 2013 - 02:28 PM, said:

or just
Square mySquare = new Square(myPoint.getX(), myPoint.getY(), sideLength);




Ok. I had that before, but it doesn't work for the Cube class. Says myPoint cannot be resolved. Do I have to pass it both the Point and Square to get it to work like that?

Well I guess I should be able to use mySquare.getX() for that one.
Was This Post Helpful? 0
  • +
  • -

#23 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:36 PM

I believe you're trying to pull this off:
Cube(Square s, int depth) {
 //draw3DRect or something
}
Square(Point p, int width, int height){
 Cube(this, 10 /*depth*/)
}
Point(p) {//mouse click event or something generates a point p
 Square(p,10 /*width*/, 10 /*height*/);
}



is that correct? Ignoring any mistakes I've made (tired) that should be a logically sound inheritance tree to generate a 10x10x10 cube from a single point.. Maybe I'm just rambling.
Was This Post Helpful? 2
  • +
  • -

#24 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:39 PM

Seems to work like this. So I guess when I pass the mySquare into the Cube constructor it carries the x and y with it as well.

public static Cube createCube(Square mySquare) {
		
		double depth = 0.0;
		
		depth = mySquare.getSL();
		
		Cube iceCube = new Cube(mySquare.getX(), mySquare.getY(), mySquare.getSL(), depth);

		return iceCube;
	}



View PostFlukeshot, on 26 March 2013 - 02:36 PM, said:

I believe you're trying to pull this off:
Cube(Square s, int depth) {
 //draw3DRect or something
}
Square(Point p, int width, int height){
 Cube(this, 10 /*depth*/)
}
Point(p) {//mouse click event or something generates a point p
 Square(p,10 /*width*/, 10 /*height*/);
}



is that correct? Ignoring any mistakes I've made (tired) that should be a logically sound inheritance tree to generate a 10x10x10 cube from a single point.. Maybe I'm just rambling.



No nothing like that yet. I just need to right an output to an optionpane now. Thank you though, I might be able to use that later.

write... I'm spent lol.

Thanks again fellas +'s all around. I'll probably be back :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#25 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Unresolved and not visible with inheritance and objects

Posted 27 March 2013 - 03:49 AM

It is the hen and the egg :)
Square mySquare = new Square(mySquare.getX(), mySquare.getY(), sideLength);


you cannot invoke getX() on mySquare to create mySquare. mySquare does not exist yet
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2