By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,154 Members | 949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,154 IT Pros & Developers. It's quick & easy.

Error messages! area(double, double) cannot be applied to ( )

P: 74
I keep getting these error messages:

area(double,double) in Rectangle cannot be applied to ()
return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ^

perimeter(double,double) in Rectangle cannot be applied to ()
return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter();
^
setSides(double,double) in Rectangle cannot be applied to (double)
R.setSides(input.nextDouble());
^
3 errors

Tool completed with exit code 1

Can anybody tell me why it is doing this? Thanks in advance!

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Rectangle
  4. {
  5.     public double length;
  6.     public double width;
  7.  
  8.     public Rectangle()
  9.     {
  10.         length = 0;
  11.         width = 0;
  12.     }
  13.  
  14.     public double getSides()
  15.     {
  16.         return length;
  17.         return width;
  18.     }
  19.  
  20.     public void setSides(double length, double width)
  21.     {
  22.         this.length = length;
  23.         this.width = width;
  24.     }
  25.  
  26.     public double area(double length, double width)
  27.     {
  28.         double area = length * width;
  29.  
  30.         return area;
  31.     }
  32.  
  33.     public double perimeter(double length, double width)
  34.     {
  35.         double perimeter = (length * 2) + (width * 2);
  36.  
  37.         return perimeter;
  38.     }
  39.  
  40.     public String toString()
  41.     {
  42.         return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter();
  43.     }
  44.  
  45.     public static void main(String [] args)
  46.     {
  47.         Rectangle R = new Rectangle();
  48.  
  49.         Scanner input = new Scanner(System.in);
  50.  
  51.         System.out.println("Enter radius: ");
  52.  
  53.         R.setSides(input.nextDouble());
  54.  
  55.         System.out.println(R.toString());
  56.     }
  57. }
  58.  
Oct 27 '06 #1
Share this Question
Share on Google+
1 Reply


Ganon11
Expert 2.5K+
P: 3,652
I keep getting these error messages:

area(double,double) in Rectangle cannot be applied to ()
return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ^

perimeter(double,double) in Rectangle cannot be applied to ()
return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter();
^
setSides(double,double) in Rectangle cannot be applied to (double)
R.setSides(input.nextDouble());
^
3 errors

Tool completed with exit code 1

Can anybody tell me why it is doing this? Thanks in advance!

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Rectangle
  4. {
  5.     public double length;
  6.     public double width;
  7.  
  8.     public Rectangle()
  9.     {
  10.         length = 0;
  11.         width = 0;
  12.     }
  13.  
  14.     public double getSides()
  15.     {
  16.         return length;
  17.         return width;
  18.     }
  19.  
  20.     public void setSides(double length, double width)
  21.     {
  22.         this.length = length;
  23.         this.width = width;
  24.     }
  25.  
  26.     public double area(double length, double width)
  27.     {
  28.         double area = length * width;
  29.  
  30.         return area;
  31.     }
  32.  
  33.     public double perimeter(double length, double width)
  34.     {
  35.         double perimeter = (length * 2) + (width * 2);
  36.  
  37.         return perimeter;
  38.     }
  39.  
  40.     public String toString()
  41.     {
  42.         return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter();
  43.     }
  44.  
  45.     public static void main(String [] args)
  46.     {
  47.         Rectangle R = new Rectangle();
  48.  
  49.         Scanner input = new Scanner(System.in);
  50.  
  51.         System.out.println("Enter radius: ");
  52.  
  53.         R.setSides(input.nextDouble());
  54.  
  55.         System.out.println(R.toString());
  56.     }
  57. }
  58.  
Simple. The way you have defined area and perimeter, you want the function call to pass two variables. Instead, why not just use the length and width inside the class? In other words, rename your functions

Expand|Select|Wrap|Line Numbers
  1. public double area() {...}
  2. public double perimeter() {...}
The code inside each of these functions does not need to be altered.

For set sides, your function header is correct - you want to have two sizes to define how large the rectangle is. Your call, however (R.setSides(input.nextDouble())) only sends 1 double variable. Try R.setSides(input.nextDouble(), input.nextDouble()), and this should work.

Finally, I see one more error in your code - namely, getSides(). You cannot retrun two values in a function. This will not give you an error explicitly, but the function will only return length. Any function automatically exits after the very first return statement. Thus, your code to return width is never touched - obviously, an error in logic.

Try splitting getSides into two functions: double getLength() and double getWidth(). You will have to call both functions instead of only one, but this is the only way to get two seperate variables in Java (that I know of).
Oct 28 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.