0

I am working on a project that computes the perimeter and the area of a triangle given its three sides a,b,c and that side b is the base of the triangle, I have made a class for it as shown below but I am having issues setting up the math and I guess the tester file to see if it all works, if someone could help me I would be very grateful

edit: I made some corrections(un-capitalizing class seemed to help some) as for the constructor I wanted to have one that handled all three sides and wasn't quiet sure how to handle it and while compiling this I ran into some errors which I can't figure out (its very late over here) and not quiet sure how to post my errors

// Class declaration
public class Triangle
{
//  3 private variables for the 3 triangle sides.
private double sideA;
private double sideB;
private double sideC;

// constant value for bad input.
public static final int INVALID_DIMENSION = -1;

// one arg constructor. Divides input by 3 and calls 
// setter methods.
public Triangle(double length)
{
    int sideLength = length/3;

    setSideA(sideLength);
    setSideB(sideLength);
    setSideC(sideLength);
}



// setter for sideA
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideA(double a)
{
    if (a > 0)
        this.sideA = a; 
    else 
        this.sideA = INVALID_DIMENSION; 
}

// setter for sideB
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideB(double b)
{
    if (b > 0)
        this.sideB = b; 
    else 
        this.sideB = INVALID_DIMENSION; 
}

//setter for sideC
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideC(double c)
{
    if (c > 0)
        this.sideC = c; 
    else 
        this.sideC = INVALID_DIMENSION; 
}

// private method that cheks to see if any values have been assigned INVALID_DIMENSION. If any have been assigned
// that value, the method returns false. If all values are valid the method returns true.
private boolean isValid()
{
    if((this.sideA == INVALID_DIMENSION)||(this.sideB == INVALID_DIMENSION)||(this.sideC == INVALID_DIMENSION))
        return false;
    else
        return true;
}

// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned. 
public double getArea()
{
    if (isValid()) 
    {
        // Logic here

        return // return new value here.
    }
   return INVALID_DIMENSION;
}


// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned. 
public double getPermiter()
{
    if (isValid()) 
    {
        // Logic here

      return // return new value here.
    }
   return INVALID_DIMENSION;
}
}

edit hi everyone! sorry for the delay I took your guys advice and took it bit by bit and I sorted out the errors I did add my math now, but I ran into an error that said "unreachable statement" and I can't figure out why here is the new new code with the comment on where the error pops up

// Class declaration
 public class Triangle 
{
//  3 private variables for the 3 triangle sides.
private double sideA;
private double sideB;
private double sideC;
private double s;
private double area;
// constant value for bad input.
public static final int INVALID_DIMENSION = -1;

// one arg construcotr. Divides input by 3 and calls 
// setter methods.
public Triangle(double length)
{
    double sideLength = length/3;

    setSideA(sideLength);
    setSideB(sideLength);
    setSideC(sideLength);
}


// setter for sideA
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideA(double a)
{
    if (a > 0)
        this.sideA = a; 
    else 
        this.sideA = INVALID_DIMENSION; 
}

// setter for sideB
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideB(double b)
{
    if (b > 0)
        this.sideB = b; 
    else 
        this.sideB = INVALID_DIMENSION; 
}

//setter for sideC
// if input is less than 0 value is set to INVALID_DIMENSION
public void setSideC(double c)
{
    if (c > 0)
        this.sideC = c; 
    else 
        this.sideC = INVALID_DIMENSION; 
}

// private method that checks to see if any values have been assigned INVALID_DIMENSION. If any have been assigned
// that value, the method returns false. If all values are valid the method returns true.
private boolean isValid()
{
    if((this.sideA == INVALID_DIMENSION)||(this.sideB == INVALID_DIMENSION)||(this.sideC == INVALID_DIMENSION))
        return false;
    else
        return true;
}

// checks to see if all values are valid. If all values pass then Area is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned. 
 public double getArea()
{
    if (isValid())
    {
        // Logic here
        s = 1/2 * (sideA + sideB + sideC);
    area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
       return getArea(); // return new value here.
    }
    else
    {

    return INVALID_DIMENSION;
    }
}    // checks to see if all values are valid. If all values pass then permiter is calculated and returned
// if any value does not pass than the value of INVALID_DIMENSION is returned. 
public double getPermiter()
{
    if (isValid()) 
    {
        // Logic here
           double s = (sideA+sideB+sideC)/2;
           return Math.sqrt(s * (s-sideA) * (s-sideB) * (s-sideC));
      return getPermiter(); // return new value here. this is where it is telling me where the error is 

    }

   return INVALID_DIMENSION;
}

}
Web Cobra
  • 71
  • 8
  • These are more suggestions than comments. 1) You should just throw an exception if the input side is negative. 2) This constructor will only handle equilateral triangles. That's somewhat limiting. I'd suggest, in my own code, having a constructor that accepts lengths for all three sides, and if I want special logic for equilateral, inherit from my triangle class. – Ron Thompson Mar 24 '15 at 04:01
  • 1
    To know how to get the area given the three sides, you need something called Heron's formula. Use your favourite search engine to find it. – Dawood ibn Kareem Mar 24 '15 at 04:01
  • `int sideLength = length/3;` won't compile. If `length` is a `double`, meaning it could be a number that isn't an integer, then why would you think `length/3` would be an integer? – ajb Mar 24 '15 at 04:03
  • @RonThompson No, the code won't handle only equilateral triangles, since there are setter methods for handling the other sides. But yes, your way of doing it would be better. – ajb Mar 24 '15 at 04:05
  • @ajb Yeah you caught me before I edited it, replacing "code" with "constructor". Either way, just suggestions :) – Ron Thompson Mar 24 '15 at 04:08
  • @WebCobra I would recommend attacking this a little at a time. Get your constructor, setter methods and `isValid` working first, and write some tests to make sure they work the way you expect them to. Then when you're happy with all of that, write `getPerimeter` and test it. Leave `getArea` till last, because it will probably be the most difficult. Also, you might consider including a check of the triangle inequality in `isValid` - that is, make sure that each side is shorter than the sum of the other two. – Dawood ibn Kareem Mar 24 '15 at 04:20

1 Answers1

0

It seems your mixing two peices of functionality in the same method.

If you notice you have two return statements. get rid of the second. Its unreachable becuase the method exits before the return statement.

  return Math.sqrt(s * (s-sideA) * (s-sideB) * (s-sideC));
  return getPermiter();

if you refactor this section to look like this:

public double getPermiter(){
    if(!isValid()){
         throw new InvalidTriangleException("your trangle isn't a trangle dood");
    }

    double s = (sideA+sideB+sideC)/2;

    return Math.sqrt(s * (s-sideA) * (s-sideB) * (s-sideC));
}

you won't have to rely on the caller knowing how the function works to handle the INVALID_DEMENSION case, and any errors surface quickly.

---- defining your custom exceptions How to define custom exception class in Java, the easiest way?

but basically,

public InvalidTriangleException extends Exception{
    public InvalidTriangleException(String message){
      super(message)
    }
}
Community
  • 1
  • 1
AnthonyJClink
  • 1,008
  • 2
  • 11
  • 32
  • okay after looking into it I see how it all works with throwing the exception if the perimeter isn't valid if it is I go on with the rest of the math involved my only issue now after doing some tinkering I run into this error on the `throw new InvalidTriangleExeception` cannot find symbol would I need to declare that first? – Web Cobra Mar 25 '15 at 21:19
  • Yes the InvalidTraingleException class must exist. this must be written by you. – AnthonyJClink Jun 22 '17 at 16:17