Calculating unknown parts of a right triangle I am in the process of creating a program that allows a user to input known parts of a right triangle and the calculates all of the unknown parts if it is possible.

I am using a class that I wrote in order to do the calculations. I wanted to know if there is a more efficient solution to this problem or if my code is good as is.

public class RightTriangle 
{
    private double sideA; //Leg
    private double sideB; //Leg
    private double sideC; //Hypotenuse 

    private double alpha; 
    private double beta;

    private int numOfSides;
    private int numOfAngles;

    public RightTriangle(double a, double b, double c, double A, double B)
    {
        numOfSides=0;
        numOfAngles=0;

        sideA=a; if(sideA&gt;0){numOfSides++;}
        sideB=b; if(sideB&gt;0){numOfSides++;}
        sideC=c; if(sideC&gt;0){numOfSides++;}

        alpha=A; if(alpha&gt;0){numOfAngles++;}
        beta=B; if(beta&gt;0){numOfAngles++;}

        if(numOfAngles&lt;2)
        {
            findMissingAngles();
        }

        if(numOfSides&lt;3)
        {
            findMissingSides();
        }

        if(!isPossible())
        {
            sideA=sideB=sideC=alpha=beta=0;
        }
    }

    private void findMissingAngles()
    {
        if(alpha==0)
        {
            calcAlpha();
        }

        if(beta==0)
        {
            calcBeta();
        }
    }

    private void findMissingSides()
    {
        if(sideA==0)
        {
            calcSideA();
        }

        if(sideB==0)
        {
            calcSideB();
        }

        if(sideC==0)
        {
            calcSideC();
        }
    }

    private void calcAlpha()
    {
        if(numOfAngles==1)
        {
            alpha=90-beta;
        }

        else
        {
            if(sideA&gt;0 &amp;&amp; sideB&gt;0)
            {
                alpha=Math.toDegrees(Math.atan(sideA/sideB));
            }

            else if(sideB&gt;0 &amp;&amp; sideC&gt;0)
            {
                alpha=Math.toDegrees(Math.acos(sideB/sideC));
            }

            else if(sideA&gt;0 &amp;&amp; sideC&gt;0)
            {
                alpha=Math.toDegrees(Math.asin(sideA/sideC));
            }
        }
    }

    private void calcBeta()
    {
        beta=90-alpha;
    }

    private void calcSideA()
    {
        if(sideC&gt;0)
        {
            sideA=Math.sin(Math.toRadians(alpha)) * sideC;
        }

        else if(sideB&gt;0)
        {
            sideA=Math.tan(Math.toRadians(alpha)) * sideB;
        }
    }

    private void calcSideB()
    {
        sideB=sideA/Math.tan(Math.toRadians(alpha));
    }

    private void calcSideC()
    {
        sideC=sideA/Math.sin(Math.toRadians(alpha));
    }

    private boolean isPossible()
    {
        return ((alpha+beta+90)==180 &amp;&amp; Math.abs(Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2)) - sideC) &lt; 1);
    }

    public void printInfo()
    {
        System.out.println("Triangle Info\n");
        System.out.println("Side A: " + String.format("%.02f", sideA));
        System.out.println("Side B: " + String.format("%.02f", sideB));
        System.out.println("Side C: " + String.format("%.02f", sideC) + "\n");
        System.out.println("Alpha: " + String.format("%.01f", alpha));
        System.out.println("Beta: " + String.format("%.01f", beta));
    }
}


Input




Enter Triangle Information (Enter 0 for missing parts)
Enter side A: 60
Enter side B: 60
Enter side C: 0

Enter angle Alpha: 0 
Enter angle Beta: 0



Output


Triangle Info

Side A: 60.00
Side B: 60.00
Side C: 84.85

Alpha: 45.0
Beta: 45.0
Keep it simple


        numOfSides=0;
        numOfAngles=0;

        sideA=a; if(sideA&gt;0){numOfSides++;}
        sideB=b; if(sideB&gt;0){numOfSides++;}
        sideC=c; if(sideC&gt;0){numOfSides++;}

        alpha=A; if(alpha&gt;0){numOfAngles++;}
        beta=B; if(beta&gt;0){numOfAngles++;}

        if(numOfAngles&lt;2)
        {
            findMissingAngles();
        }

        if(numOfSides&lt;3)
        {
            findMissingSides();
        }



This seems more complicated than necessary.  Why have numOfAngles and numOfSides at all?  You could just say 

        sideA = a;
        sideB = b;
        sideC = c;

        alpha = A;
        beta = B;

        findMissingAngles();

        findMissingSides();


And then modify 


        if(numOfAngles==1)



in calcAlpha to say 

        if (beta &gt; 0)


That saves two object fields that exist just for construction purposes.  

Be careful of false modularity


    private void calcBeta()
    {
        beta=90-alpha;
    }



This only works if you calculate alpha first.  That's the programming logic here, but nothing enforces it.  So why have a separate method to do an assignment based on one subtraction?  You could change 


        if(beta==0)
        {
            calcBeta();
        }



to say 

        if (beta==0)
        {
            beta = 90 - alpha;
        }


instead.  Then you could get rid of calcBeta.  

This also avoids the problem of calling calcBeta without testing that beta doesn't already have a value.  

Alternately, you could change calcBeta to be more like calcAlpha where it calculates alpha from beta if beta exists or from two sides otherwise.  

Similarly, all the calcSide methods assume that alpha exists.  And calcSideB and calcSideC assume that sideA exists.  If any of that is not true, then you'll get weird results.  

Exceptional circumstances


    private boolean isPossible()
    {
        return ((alpha+beta+90)==180 &amp;&amp; Math.abs(Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2)) - sideC) &lt; 1);
    }



Why not change (alpha+beta+90)==180 to just (alpha + beta) == 90?  

Why is 1 the acceptable error?  Why not .1 or .03 or something else?  

Are negative values acceptable?  Because you accept them if they have the right magnitude.  For example, -95 and 185 are a valid pair of angles.  And -3, -4, 5 are valid side values.