# Method returns value of 0

• 09-26-2012, 04:02 PM
gerchan
Method returns value of 0
Hey everyone! Java beginner here. So I was writing this method which gets the area of a right-angled triangle. The sides (a b c) will be called by the constructor Triangle. My problem is that the program compiles well enough however the value returned for the area is 0. I tried initializing the variables but to no avail. Can anyone please guide me as to where the problem lies. I'm not sure its a problem with the syntax since the program compiles but probably the error lies somewhere with the logic. Thanks Below is the code:

Code:

```import java.util.*; public class Triangle {                 double a=0, b=0, c=0;         double area =0;                 public Triangle (double side1, double side2, double side3)         {         this.a = side1;         this.b = side2;         this.c = side3;         } public double getArea()         {                 if (a>b && b>c)                 {                 this.area = ((b*c)/2);                 }                 else if (b>a && a>c)                 {                 this.area = ((a*c)/2);                 }                 else if (c>a && a>b)                 {                 this.area = ((a*b)/2);                 }         return this.area;         } }```
• 09-26-2012, 04:20 PM
Tolls
Re: Method returns value of 0
Stick some debug code (println() statements) into the getArea method, first printing out the values of a, b and c, and then to see which path it is going through in that if/else statement.
• 09-26-2012, 06:16 PM
Norm
Re: Method returns value of 0
Always add an ending else statement at the end of a chain of if/else if that prints out why none of the above if conditions were true.
• 09-27-2012, 01:56 AM
thinkinjava24
Re: Method returns value of 0
your condition in the method getArea() does not cover all the possibilities.
they should be like this:
public double getArea()
{
if (a>b && a>c)
{
this.area = ((b*c)/2);
}
else if (b>a && b>c)
{
this.area = ((a*c)/2);
}
else if (c>a && c>b)
{
this.area = ((a*b)/2);
}
return this.area;
}
hope it helps
by the way, if you write the code like this, you must ensure that the triangle you input to the system is RT. otherwise, your result will be wrong.
• 09-27-2012, 03:16 AM
Norm
Re: Method returns value of 0