Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why am I getting error code on my Triangle Class?  RSS feed

 
Debbie Thornton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why am I getting error code on  Triangle t = new Triangle(1.0,1.0,1.0);{  in my program?  The error code is: constructor Triangle in class Triangle cannot be applied to given types;  I have been working on this program, trying to make it run without failing, for 3 days now.  I have had several people look at it and either tell me it was okay and it didn't pass the run or tell me things to add and it still did not run correctly.  Can someone PLEASE help me?



  


THE ERROR CODE:

constructor Triangle in class Triangle cannot be applied to given types;
  required: no arguments
  found: double,double,double
  reason: actual and formal argument lists differ in length
----
(Alt-Enter shows hints)

THANK YOU!!
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating a Triangle, not a Triangletest object. See line 18.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 38 through 48 are getters, not setters.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
constructor Triangle in class Triangle cannot be applied to given types;
  required: no arguments
  found: double,double,double
  reason: actual and formal argument lists differ in length 

The compiler found a call to a constructor with 3 double arguments but only found a constructor that takes no arguments.
Either change the call not to have any arguments
or add a constructor to the class that has 3 double arguments.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getArea() formula looks wrong. I believe it should be (base * height)/2.

See https://www.mathsisfun.com/geometry/herons-formula.html
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch! 

  • As Carey already mentioned, you are creating an object of Triangle rather than Triangletest and about getter methods which named like setter methods.
  • You don't need to assign on line no 32 and 33 in Triangletest(double side1, double side2, double side3) constructor.
  • which are being assigned by parameters of constructor so better remove those two assignment and make it look like below.

  • I think, you haven't noticed assigning value to side1 twice on line no 27, 28 in non-parameterized Triangletest constrcutor, here.

  • If you want side1, side2 and side3 instance variables to initialize with value 1.0 on object creation, then either initialize them where they are declared in class like this on line 23,23, 24
  • Or
    In non-parameterized constructor like this on line no 26, 27 and 28.
  • You can think of ranaming Triangletest to TriangleTest may look nice, worth reading Java Programming Style Guide
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!