Unresolved and not visible with inheritance and objects

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 HGPolumbus  Icon User is offline

  • D.I.C Head

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

Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 08:37 AM

Hello again. I'm working on a program where I need to collect 2 point coordinates, the length of a side of a square, and the depth of a cube. The program should then print it all out, each class inheriting from the last. My issue I'm having is building my driver class so that I can call a method to get the input for each one before printing from the objects. Here's where I'm at:

import javax.swing.JOptionPane;

public class InheritanceTest {

	public static void main(String args[]) {

		Point myPoint = createPoint();
		Square mySquare = createSquare(myPoint.x, myPoint.y); //this is wrong and where my problem is.
		Cube iceCube = createCube();
	}

	public static Point createPoint() {

		int localX = 0;
		int localY = 0;

		while (true) {
			try {
				localX = Integer.parseInt(JOptionPane.showInputDialog(null, "Please type a coordinate: ", "The coordinate for X"));
			} catch (NumberFormatException z) {
				JOptionPane.showMessageDialog(null, "Sorry, invalid input.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}
			if (localX < 0) {
				JOptionPane.showMessageDialog(null,
						"Coordinate X cannot be less than 0.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}

			break;
		}

		while (true) {
			try {
				localY = Integer.parseInt(JOptionPane.showInputDialog(null, "Please type a coordinate: ", "The coordinate for Y"));
			} catch (NumberFormatException z) {
				JOptionPane.showMessageDialog(null, "Sorry, invalid input.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}
			if (localY < 0) {
				JOptionPane.showMessageDialog(null,
						"Coordinate Y cannot be less than 0.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}

			break;

		}
		Point myPoint = new Point(localX, localY);
		return myPoint;

	}
	
	public static Square createSquare(Point myPoint) {//problem could be here
		
		double sideLength = 0.0; 

		while (true) {
			try {
				sideLength = Integer.parseInt(JOptionPane.showInputDialog(null, "Please type the length of one side: ", "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;

		}
		Square mySquare = new Square(x, y, sideLength); //problem could also be here
		return mySquare;
	}
}

class Point {

	private int x;
	private int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}
}

class Square extends Point {

	private double sideLength;

	public Square(int x, int y, double sideLength) {
		super(x, y);
		this.sideLength = sideLength;
	}

	public double getArea() {
		return sideLength * sideLength;
	}

	public double getPerimeter() {
		return 4 * sideLength;
	}
}

class Cube extends Square {
	private double depth;

	public Cube(int x, int y, double sideLength, double depth) {
		super(x, y, sideLength);
		this.depth = depth;
	}

	public double area() {
		return 6 * super.getArea();
	}

	public double volume() {
		return super.getArea() * depth;
	}
}


I got most of it done but I can't figure out the part where you put it together. How do I pull the variables out of the superclass to use them when I build the subclass object? Shouldn't they just be in there from inheriting?

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Unresolved and not visible with inheritance and objects

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

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

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 08:44 AM

Class Square doesn't have a constructor "Square(Point p1, Point p2)". Which is what you are trying to call.

Sorry ignore that, not read the code properly.

This post has been edited by Flukeshot: 26 March 2013 - 08:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 08:45 AM

Square extends Point? Hm. Paging Dr. Abbott...

But assuming that this is the design that you want, or have to use, I believe your main issue is the use of private access on x and y. Try protected.

This is a little counterintuitive, maybe: since Square is a Point, it has an x and a y, and they are its fields, so it should have access to them. I totally agree that this would make more sense. However, that's not actually how it works: inherited fields are actually not visible unless they're protected or public, and you don't want them to be public.
Was This Post Helpful? 1
  • +
  • -

#4 HGPolumbus  Icon User is offline

  • D.I.C Head

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

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:18 AM

View Postjon.kiparsky, on 26 March 2013 - 08:45 AM, said:

Square extends Point? Hm. Paging Dr. Abbott...

But assuming that this is the design that you want, or have to use, I believe your main issue is the use of private access on x and y. Try protected.

This is a little counterintuitive, maybe: since Square is a Point, it has an x and a y, and they are its fields, so it should have access to them. I totally agree that this would make more sense. However, that's not actually how it works: inherited fields are actually not visible unless they're protected or public, and you don't want them to be public.



Is there another option to keep them private? Forget about my create....() methods? Would I have to sloppily put all the optionpane stuff in the main then?

Thanks for the replies.

Even if it were to work with protected, I have to call the next one

Square mySquare = createSquare(myPoint.x, myPoint.y, sideLength);

and sideLength cannot be resolved...

because it doesn't exist until after the method is done and the Square object created
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:21 AM

You do this:
Square mySquare = createSquare(myPoint.x, myPoint.y);



but createSquare actually takes a Point, not two ints:

 public static Square createSquare(Point myPoint)



So @flukeshot was on the right track.
Was This Post Helpful? 1
  • +
  • -

#6 HGPolumbus  Icon User is offline

  • D.I.C Head

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

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:29 AM

I tried to do that before, but then the square object doesn't have the sideLength in it... and if I change the constructor to include the object as a parameter, I get an error on the super(x,y).
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:36 AM

Well, you have to have your parameters and your arguments line up. Can't give me two ints if I want a point.

What exactly is the problem you're experiencing? Are you getting compiler error messages? If so, what are they? That would help a lot.
Was This Post Helpful? 0
  • +
  • -

#8 HGPolumbus  Icon User is offline

  • D.I.C Head

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

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:37 AM

Hmmmm

I did

Square mySquare = createSquare(myPoint);

and

Square mySquare = new Square(myPoint.x, myPoint.y, sideLength);

and no errors.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:39 AM

Yes, those are two different methods. One is your local method which gets some information from the user and calls the Square constructor, the other is the Square constructor. One takes two ints and a double, the other takes a Point.

There's nothing surprising about the fact that both of those calls work.
Was This Post Helpful? 0
  • +
  • -

#10 HGPolumbus  Icon User is offline

  • D.I.C Head

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

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:43 AM

It is relatively surprising to me. :sweatdrop:

I just had my parameters messed up, thank you for your help. I should be able to finish this on my own now.


Thanks again.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:49 AM

Glad to help.
Was This Post Helpful? 0
  • +
  • -

#12 HGPolumbus  Icon User is offline

  • D.I.C Head

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

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:04 PM

Well, I was wrong. I'm really lost now. Teacher said the variables have to stay private, and I have to use accessor methods...

I know what an accessor method is

public void getX(){
return x;
}

but what I don't know is where to put them in my program or how to sue them with inheritance.

This is where I got with my code.

import javax.swing.JOptionPane;

public class InheritanceTest {

	public static void main(String args[]) {

		Point myPoint = createPoint();
		Square mySquare = createSquare(myPoint);
		Cube iceCube = createCube(mySquare);
	}

	public static Point createPoint() {

		int localX = 0;
		int localY = 0;

		while (true) {
			try {
				localX = Integer.parseInt(JOptionPane.showInputDialog(null,
						"Please type a coordinate: ", "The coordinate for X"));
			} catch (NumberFormatException z) {
				JOptionPane.showMessageDialog(null, "Sorry, invalid input.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}
			if (localX < 0) {
				JOptionPane.showMessageDialog(null,
						"Coordinate X cannot be less than 0.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}

			break;
		}

		while (true) {
			try {
				localY = Integer.parseInt(JOptionPane.showInputDialog(null,
						"Please type a coordinate: ", "The coordinate for Y"));
			} catch (NumberFormatException z) {
				JOptionPane.showMessageDialog(null, "Sorry, invalid input.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}
			if (localY < 0) {
				JOptionPane.showMessageDialog(null,
						"Coordinate Y cannot be less than 0.",
						"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
				continue;
			}

			break;

		}
		Point myPoint = new Point(localX, localY);
		return myPoint;

	}

	public static Square createSquare(Point myPoint) {

		double sideLength = 0.0;

		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;

		}
	//said I need accessor methods for x and y
		Square mySquare = new Square(mySquare.getX(), mySquare.getY(), sideLength);
		return mySquare;
	}

	public static Cube createCube(Square mySquare) {//this is probably wrong.
		//said I need accessor methods for x, y, and sideLength, I don't think I did it right.
		Cube iceCube = new Cube(iceCube.getX(), iceCube.getY(), iceCube.getSL(), depth);

		return iceCube;
	}
	
}

class Point {

	private int x;
	private int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public int getX(){
		return x;
	}
	
	public int getY() {
		return y;
	}
}

class Square extends Point {

	private double sideLength;

	public Square(int x, int y, double sideLength) {
		super(x, y);
		this.sideLength = sideLength;
	}

	public double getArea() {
		return sideLength * sideLength;
	}

	public double getPerimeter() {
		return 4 * sideLength;
	}
	
	public double getSL(){
		return sideLength;
	}
}

class Cube extends Square {
	private double depth;

	public Cube(int x, int y, double sideLength, double depth) {
		super(x, y, sideLength);
		this.depth = depth;
	}

	public double area() {
		return 6 * super.getArea();
	}

	public double volume() {
		return super.getArea() * depth;
	}
}


I know it's not void in the get method, just a typo

so where do I put the get methods? how do I call them with inheritance?
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:07 PM

If you have a private field foo in an object, and you want to let the world look at it, give that object a getFoo method. If you have a private field foo in an object and you want to let the world poke at it, give the object a setFoo method.

And at that point, you've made the object public, so there's no point in calling it private at all...
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:07 PM

Your Point class has a getX() and getY() that can be called on any element of your hierarchy (Point, Square, Cube)
you just need a public getSideLength() for the Square and a public getDepth() for the Cube

This post has been edited by pbl: 26 March 2013 - 02:08 PM

Was This Post Helpful? 2
  • +
  • -

#15 HGPolumbus  Icon User is offline

  • D.I.C Head

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

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:11 PM

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

If you have a private field foo in an object, and you want to let the world look at it, give that object a getFoo method. If you have a private field foo in an object and you want to let the world poke at it, give the object a setFoo method.

And at that point, you've made the object public, so there's no point in calling it private at all...



I did that, I think... but the error under my objects like mySquare.getX() says that it is not initialized. I thought creating it was initializing it....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2