Results 1 to 5 of 5
  1. #1
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Simple rectangle code gives an error

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.lang.Math.*;
    
    public class rectangles
    {
    	public static void main(String[] args)
    	{
    		rectangle r1 = new rectangle(3,4);
    		System.out.println("Rectangle r1 height is " + r1.height + ". Rectangle r1 width is " + r1.width + " Rectangle r1 perimeter is " + r1.perimeter() + " Rectangle r1 diagonal is " + r1.diagonal() );
    	}
    }
    class rectangle
    {
    	public int height;
    	public int width;
    	public double sqrt1;
    	public void rectangle (int heightt, int widthh)
    	{
    		height=heightt;
    		width=widthh;
    	}
    	public void rectangle()
    	{
    		height=2;
    		width=2;
    	}
    	public double perimeter()
    	{
    		return (2*height)+(2*width);
    	}
    	public double area()
    	{
    		return height*width;
    	}
    	public double diagonal()
    	{
    		sqrt1=Math.sqrt(height*height + width*width);
    		return sqrt1;
    	}

    It gives me an error saying that there should be no arguments for rectangle r1 = new rectangle(3,4); . I am learning Java in college and they told me to do this basing it on the circles example. I think I did everything right but looks like I didn't. Here is the error:

    rectangles.java:9: error: constructor rectangle in class rectangle cannot be applied to given types;
    rectangle r1 = new rectangle(3,4);
    ^
    required: no arguments
    found: int,int
    reason: actual and formal argument lists differ in length

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Simple rectangle code gives an error

    Constructors don't have a return type.

    Follow standard naming conventions. Class names and their constructors should begin with a capital letter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Simple rectangle code gives an error

    Quote Originally Posted by KevinWorkman View Post
    Constructors don't have a return type.

    Follow standard naming conventions. Class names and their constructors should begin with a capital letter.
    What is a constructor?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Simple rectangle code gives an error

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Simple rectangle code gives an error

    Quote Originally Posted by KevinWorkman View Post
    Now after I read that I took out the void out of my constructor and it worked!

Similar Threads

  1. Simple rectangle display applet doesn't work in browser
    By stainluss in forum Java Applets
    Replies: 7
    Last Post: 01-31-2012, 11:38 PM
  2. 30 line simple gui, simple error, need help
    By cc11rocks in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 08:33 PM
  3. Replies: 6
    Last Post: 11-30-2010, 11:29 PM
  4. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  5. Error with my simple GUI code
    By Menre in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 10:49 PM

Posting Permissions

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