4 Replies - 621 Views - Last Post: 14 February 2009 - 09:29 AM Rate Topic: -----

#1 Nazkav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-February 09

Inheritence and constructors

Post icon  Posted 14 February 2009 - 07:03 AM

Please could someone help me with this i am new to java and im a bit stuck. here is the code

public class Point
{
	public int x;
	public int y;
	
	public Point(int x, int y)
	{
		this.x=x;
		this.y=y;
		
		System.out.println("Constructing a Point.");
	}
}

public class GeometricShape
{
	public Point centre;
	
	public GeometricShape(int x, int y)
	{
		System.out.println("Constructing a GeometricShape.");
		Point centre = new Point(x,y);
	}
	public void displayCentre()
	{	
		System.out.println("The x and y coordinates are: " + centre);
	}
}

public class Rectangle extends GeometricShape
{
	public int width;
	public int height;
	
	public Rectangle(int x,int y,int w, int h)
	{
		System.out.println("Constructing a Rectangle.");
		width=w;
		height=h;
		
	}
}
public class Square extends Rectangle
{
	public String colour;
	
	public Square(int x,int y,int w,int h,String col)
	{
		colour=col;
		System.out.println("Constructing a Square.");
	}
	public void showColour()
	{
		System.out.println("The Colour of the Square is: " + colour);
	}
}

public class InheritProgram
{
	public static void main(String [] args)
	{
		Square s = new Square(10,20,15,15,"Blue");
		
		s.displayCentre();
		s.showColour();
	}
}


The problem i have is that the result of the x and y coordinates returns null. In the assignment i was asked to pass two of the integers from the Rectangle class' constructor to the parent constructor and i didn't really understand that. here is the output if it helps.

Constructing a GeometricShape.
Constructing a Point.
Constructing a Rectangle.
Constructing a Square.
The x and y coordinates are: null
The Colour of the Square is: Blue

any help would be much appreciated. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Inheritence and constructors

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Inheritence and constructors

Posted 14 February 2009 - 07:08 AM

public Rectangle(int x,int y,int w, int h)
	{
		super(x, y);//<<<i think that's what u were looking for
		System.out.println("Constructing a Rectangle.");
		width=w;
		height=h;
	   
	}


Was This Post Helpful? 0
  • +
  • -

#3 Nazkav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-February 09

Re: Inheritence and constructors

Posted 14 February 2009 - 07:20 AM

Thanks for the help but unfortunatly i've already tried that one.

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Inheritence and constructors

Posted 14 February 2009 - 07:48 AM

try this out
public class Point
{
	public int x;
	public int y;
   
	public Point(int x, int y)
	{
		this.x=x;
		this.y=y;
	   
		System.out.println("Constructing a Point.");
	}
	public void displayCentre()
	{	
		System.out.println("The x and y coordinates are: " + x + " " + y);
	}
}


public class GeometricShape extends Point
{
   
	public GeometricShape(int x, int y)
	{
		super(x,y);
	}
	
}


public class Rectangle extends GeometricShape
{
	public int width;
	public int height;
   
	public Rectangle(int x,int y,int w, int h)
	{
	super(x, y);
		System.out.println("Constructing a Rectangle.");
		width=w;
		height=h;
	   
	}
}


public class Square extends Rectangle
{
	public String colour;
   
	public Square(int x,int y,int w,int h,String col)
	{
	super(x, y, w, h);
		colour=col;
		System.out.println("Constructing a Square.");
	}
	public void showColour()
	{
		System.out.println("The Colour of the Square is: " + colour);
	}
}


public class InheritProgram
{
	public static void main(String [] args)
	{
		Square s = new Square(10,20,15,15,"Blue");
	   
		s.displayCentre();
		s.showColour();
	}
}


hope this helps
Was This Post Helpful? 1
  • +
  • -

#5 Nazkav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-February 09

Re: Inheritence and constructors

Posted 14 February 2009 - 09:29 AM

Thanks a lot that works great. only thing is its a little bit outside the parameters of what was layed down in the my assignment, but i,ll send it in to my tutor and see what he says. Thanks again that was very helpful

nazkav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1