Results 1 to 7 of 7
  1. #1
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Post No-arg constructor??

    I am little confused about no-arg consturctor and need help in understanding exactly what it is. I have googled this topic and is more confused. I have created a class Rectangle. One of my methods is called public double Rectangle(){} which I thought was a no arg constructor.
    But when I compile I am getting an saying missing return statement line 9 (verbatim). Am I missing something?


    Java Code:
    public class Rectangle
    {
    
    	private double height = 1.0;
    	private double width= 1.0;
    	private static String color;
    
    	public double Rectangle()
    	{}
    
    
    	public Rectangle (double high, double wid)
    	{
    		this.width=wid;
    		this.height= high;
    	}
    
    	public Rectangle (String recColor)
    	{
    		color = recColor;
    	}
    
    	public void setHeight(double high)
    	{
    		this.height = high;
    	}
    
    	public void setWidth(double wid)
    	{
    		this.width = wid;
    	}
    
    	public void setColor(String col)
    	{
    		color = col;
    	}
    
    	public double getHeight()
    	{
    		return height;
    	}
    
    	public double getWidth()
    	{
    		return width;
    	}
    
    	public double getArea()
    	{
    		return height*width;
    	}
    
    	public double getPerimeter()
    	{
    		return 2*(height + width);
    	}
    
    		public String getRecColor()
    	{
    		return this.color;
    	}
    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Constructors don't need a return type.

    No arg constructors are the same as constructors that take arguments. They are used to be the default way to initialize some object.
    Last edited by sunde887; 03-09-2011 at 02:26 AM.

  3. #3
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Are you trying to have a constructor which creates a rectangle, but takes no arguments? You are on the right track, remember a constructor can not return a value...

  4. #4
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    When I try to compile this code I am receiving an error "missing return statement - line 9 (verbatim). ". Line 9 is: Rectangle(){} . So I am not sure why it is telling me to put a return statement on a no arg constructor. Can someone help understand why I am receiving this error on a no arg constructor?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do the other constructors work? A constructor should not have a return type. Just a random attempt at a solution is adding the line this(); in the code block.

  6. #6
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by collwill View Post
    When I try to compile this code I am receiving an error "missing return statement - line 9 (verbatim). ". Line 9 is: Rectangle(){} . So I am not sure why it is telling me to put a return statement on a no arg constructor. Can someone help understand why I am receiving this error on a no arg constructor?
    This compiles for me:

    Java Code:
    public class Rectangle
    {
    
    	private double height = 1.0;
    	private double width= 1.0;
    	private static String color;
    
    	public Rectangle()
    	{}
    
    
    	public Rectangle (double high, double wid)
    	{
    		this.width=wid;
    		this.height= high;
    	}
    
    	public Rectangle (String recColor)
    	{
    		color = recColor;
    	}
    
    	public void setHeight(double high)
    	{
    		this.height = high;
    	}
    
    	public void setWidth(double wid)
    	{
    		this.width = wid;
    	}
    
    	public void setColor(String col)
    	{
    		color = col;
    	}
    
    	public double getHeight()
    	{
    		return height;
    	}
    
    	public double getWidth()
    	{
    		return width;
    	}
    
    	public double getArea()
    	{
    		return height*width;
    	}
    
    	public double getPerimeter()
    	{
    		return 2*(height + width);
    	}
    
    		public String getRecColor()
    	{
    		return this.color;
    	}
    
    }

  7. #7
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    Ok, looking at it change the no arg constructor and it works better and no compile errors:

    Java Code:
    public class Rectangle
    {
    
    	//Declare variables
    	private double height;
    	private double width;
    	private static String color;
    
    	//no arg constructor
    	public Rectangle()
    	{
    		height = 1.0;
    		width= 1.0;
    		color="Red";
    	}

Similar Threads

  1. Constructor
    By theoneroo in forum New To Java
    Replies: 5
    Last Post: 01-28-2011, 09:37 AM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 08:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •