-1

I have a problem in floats. My 1st problem is my public float area(), the problem is the result value is returning zero. 2nd is the public float computeHeight(), no value will return. I'm having headache with this. please help me thank you. Just delete if duplicate or repost. thank you

private int sideA, sideB, sideC;
private float computePerimeter;
private float area;
private float computeHeight;

public Triangle(){
}

// I want to set all sides to 10

public Triangle(int a, int b, int c){
    sideA = a;
    sideB = b;
    sideC = c;
}

//setters & getters

//perimeter is the sum of all the sides of the triangle.
public float computePerimeter(){
    computePerimeter = sideA + sideB + sideC;
    return computePerimeter;
}


//A=1/2bh.
//A = Area of the triangle
//b = Length of the base of the triangle //SideB
//h = Height of the base of the triangle //SideA

public float area(){
     area =   1/2 * (sideB  * sideA);
     return area;
}

public float computeHeight(){
     sideC = 2 * (area/sideB) ;
     return computeHeight;
}


public void display(){
        System.out.println("Side A: "+getSideA() +" Side B: "+getSideB()+" Side C: "+getSideC() );
        System.out.println("\nThe sum of all the sides of the triangle is: " +computePerimeter() );
        System.out.println("The area of the triangle is: " + area() );
}

public static void main(String []args){
    Triangle result = new Triangle();
    result.setSideA(10);
    result.setSideB(10);
    result.setSideC(10);
    result.display();

}
azro
  • 53,056
  • 7
  • 34
  • 70
  • *2nd is the public float computeHeight(), no value will return.* That's literally impossible. What are you trying to say? – shmosel Jul 21 '17 at 08:58

2 Answers2

0

Maybe:

public float area(){
  area =  (float) (0.5f * (sideB * sideA));
  return area;
}

public float computeHeight(){
  sideC = 2f * (area/sideB) ;
  return computeHeight;
}

Adding the "f" and the casting makes the numbers to be treated as a float.

Related to: I don't know how to cast to float in Java.

If you don't do this, then the number is processed as if it was an integer (1/2 in integer world = 0). Thus you lose a lot of precision.

spi
  • 1,673
  • 13
  • 19
0

1/2 return 0 because they are int, you need to use (1.0 / 2)

Also I would advise to replace all your float and int by double (for the 6 attributes) , it would allow you to not have warning possible loss convertion' to avoid casting(float)and usef` after the number

private double sideA, sideB, sideC, computePerimeter, area, computeHeight;

And a trick I'd learn to you, this will will assign the result to area AND return it :

public double area() {
     return (area = 1.0 / 2 * (sideB * sideA));
}

Last thing, because you have a public Triangle(double a, double b, double c) constructor you may replace

Triangle result = new Triangle();
result.setSideA(10);
result.setSideB(10);
result.setSideC(10);

By Triangle result = new Triangle(10,10,10); in one-line

azro
  • 53,056
  • 7
  • 34
  • 70
  • @KennethIsrael you post that comment after I tell to replace by double, please read before comment ;) – azro Jul 21 '17 at 09:02
  • thanks sir, but the result of computeHeight will override the the set value of SideC. – Kenneth Israel Jul 21 '17 at 09:09
  • overwrite*, nope because you never call the method computeHeight // and even if it was called used `Triangle(10,10,10)` or `Triangle() and then set()` is 100% same – azro Jul 21 '17 at 09:12