I'm writing a program to ask the user for 6 points for a triangle and then calculate the length of the 3 sides, the perimeter, and the area. Then the tester will have to display the properties of the triangle, length of sides, the area, the perimeter, and if the triangle is valid. Nothing too serious, just that if SideAB + SideBC > SideAC.. it's invalid.

Here's my class.

public class Triangle
{
private double x1;
private double y1;
private double x2;
private double y2;
private double x3;
private double y3;

private double sideAB;
private double sideBC;
private double sideAC;

private double perimeter;
private double area;

private double s;


public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
}
public boolean isValid() {
if (sideAB()+sideBC() > sideAC())
return true;
else
return false;
}
public double sideAB()
{
double x = Math.pow(((x2)-(x1)),2);
double y = Math.pow(((y2)-(y1)),2);
sideAB = Math.sqrt((x+y));
return sideAB;
}

public double sideBC()
{
double x = Math.pow(((x3)-(x2)),2);
double y = Math.pow(((y3)-(y2)),2);
sideBC = Math.sqrt((x+y));
return sideBC;
}

public double sideAC()
{
double x = Math.pow(((x1)-(x3)),2);
double y = Math.pow(((y1)-(y3)),2);
sideAC = Math.sqrt((x+y));
return sideAC;
}


public double peterimeter(double perimeter)
{
perimeter = ((sideAB) + (sideBC) + (sideAC));
return perimeter;
}


public double area(double area)
{
s = perimeter/2;
area = Math.sqrt(s * (s - sideAB) * (s - sideBC) * (s - sideAC));
return area;
}


public String toString()
{
Double.toString(x1);
Double.toString(y1);
Double.toString(x2);
Double.toString(y2);
Double.toString(x3);
Double.toString(y3);
Double.toString(sideAB);
Double.toString(sideBC);
Double.toString(sideAC);
Double.toString(area);
Double.toString(perimeter);
String myTriangle = "A traingle with points " + "(" +x1+ "," +y1+ ")" + " " + "(" +x2+ "," +y2+ ")" + " " + "(" +x3+ "," +y3+ ")" + " a perimeter of: " +perimeter+ " and an area: " + area;

return myTriangle;
}


public double getX1()
{
return x1;
}


public double getY1()
{
return y1;
}



public double getX2()
{
return x2;
}



public double getY2()
{
return y2;
}


public double getX3()
{
return x3;
}


public double getY3()
{
return y3;
}


public String getPerimeter()
{
String p = Double.toString(perimeter);
return p;
}

public String getArea()
{
String a = Double.toString(area);
return a;
}

public double getSideAB()
{
return sideAB;
}

public double getSideBC()
{
return sideBC;
}

public double getSideAC()
{
return sideAC;
}
}

And Here's my tester

import java.util.*;

public class TriangleTester
{
public static void main(String[] args)
{

{

Scanner scanObject = new Scanner(System.in);

System.out.println("Enter x coordinate for first point: ");
double x1 = scanObject.nextInt();
System.out.println("Enter y coordinate for first point: ");
double y1 = scanObject.nextInt();
System.out.println("Enter x coordinate for second point: ");
double x2 = scanObject.nextInt();
System.out.println("Enter y coordinate for second point: ");
double y2 = scanObject.nextInt();
System.out.println("Enter x coordinate for third point: ");
double x3 = scanObject.nextInt();
System.out.println("Enter y coordinate for third point: ");
double y3 = scanObject.nextInt();

Triangle myTriangle = new Triangle(x1, y1, x2, y2, x3, y3);
if (myTriangle.isValid())
System.out.println(myTriangle);
else
System.out.println("This triangle is invalid");
}


}
}

I know that I never executed the methods, which is why the area and perimeter is always 0 no matter what values I put.

I tried putting the calculations inside like this :

public double getSideAB()
{
double x = Math.pow(((x2)-(x1)),2);
double y = Math.pow(((y2)-(y1)),2);
sideAB = Math.sqrt((x+y));
return sideAB;
}

But the output will always say the triangle is invalid. So now it just left me confused on where I should put the calculations.

Thanks

if SideAB + SideBC > SideAC.. it's invalid.

Where did you get that from? That's wrong. It should be that all of the below is true, it is a triangle.

AB+AC > BC
AB+BC > AC
BC+AC > AB

It's what my professor wrote for me. Like the pythagoriam therom. If a + b > c then its false. Sorry if it doesn't make sense I'm celebrating. My 21st birthday right now.

Taywin is right here. You don't want to check is a+b > c, that is valid for any triangle. You should check for a+b < c. Make sure that your isValid method includes: a+b < c, a+c < b and b+c < a. If any of them are true, you have an invalud triangle.

You need to go back to your professor and tell him/her that this is wrong. Ask him/her to give you an example of a right triangle which has "sideA + sideB <= sideC" (Pythagorean triangle). If he/she is still stubborn and not accept it, go to the Dean or someone who is the head of the department and explain this situation to that person.

Anyway, how about a real life sample to show you that any addition of 2 sides of a triangle must be greater than the other side.

Let say your house, a department store, and school are points.
1)If these points do form an arbitrary triangle, and you want to go from your house to school via the department store, the display to go to school directly will ALWAYS be shorter than going via the department store. Why? Because you are going "around" and not a "direct" route. This will apply to ANY of your starting and end point in a triangle.
2)If these points form any equal or less distance, they are on the same line (look at my attempted picture below).

You don't want to check is a+b > c, that is valid for any triangle. You should check for a+b < c. Make sure that your isValid method includes: ab+bc < ac, ac+bc < ab and ab+bc < ac. If any of them are true, you have an invalud triangle.

I'm sorry to tell you that this is wrong again. You MUST check for ab+bc>ac and all other sides as well. If you are going to check the opposite (using less than), you must use "<=" or you will get it wrong. In other words, regardless the way you check, you MUST check all 3 sides.

If side ab+bc = ac, your points are on the same line!

/*
  .-----------.-----.
  a           b     c
  ab + bc = ac


  The triangle below can be any arbitrary triangle.
          a
         /\
        /  \
       /    \
      '------'
      b      c
    ab + bc > ac
    ab + ac > bc
    bc + ac > ab
*/

If AB + BC == AC, the points are on a like like you say. Would you call that a triangle? I wouldn't.

I'm sorry to tell you that this is wrong again. You MUST check for ab+bc>ac and all other sides as well. If you are going to check the opposite (using less than), you must use "<=" or you will get it wrong. In other words, regardless the way you check, you MUST check all 3 sides.

That is exactly what I am saying. If any of the following is true, the triangle is INvalid: a+b<c, a+c<b and b+c<a.

public boolean isValid() 
{
	if(sideAB+sideAC > sideBC)
		return true;
	else if(sideAB + sideBC > sideAC)
		return true;
	else if(sideBC + sideAC > sideAB)
		return true;
	else
		return false;
}

is this correct?

No. You must check exactly what I said: if ANY of the following are true, the triangle is INVALID: a+b<c, a+c<b and b+c<a (where a, b and c are the sides);

In code:

public boolean isValid () {
return !(a+b<c || b+c<a || a+c<b);
// return false if any of the statements are true, else return false.
}

Or:

public boolean isValid () {
return (a+b>=c && b+c>=a && a+c<=b);
// return true if all the statements are true, else return false.
}

In the future think about what you want the function to do, what statements you need, what those statements mean, and what you should return using the calculations you made in the function.

Sorry, I was going off of what Taywin posted. Can you give me a set of valid points to check if it works?

Any three points in a 2D system should be valid. (0,0) (2,3) (4,4) is a valid triangle.
Think of some invalid points: they don't exsist, or I haven't found any :)...

@hiddepolen
OK, you are correct about that if and only if it is true when it is invalid. Didn't read it that way the first time. However, you demonstrated the code wrong including my algorithm... Also, my explanation is much straight forward in implementation.

@cecsFTL
I didn't say that you need if-else for checking. I said check them all.

public boolean isValid () {
  return !(a+b<=c || b+c<=a || a+c<=b);
  // return true if all the statements are true, else return false.
}
// or
public boolean isInvalid () {  // notice it is isInvalid
  return (a+b<=c || b+c<=a || a+c<=b);
  // return true if all the statements are true, else return false.
}
// or

// or my algorithm
public boolean isValid () {
  return (a+b>c && b+c>a && a+c>b);
  // return true if all the statements are true, else return false.
}

Tell me if a is at one location and b,c at the same location. What is returned from your original isValid() version?

/*
  .-----------.
  A          B,C

  a = sizeAB  (let say distance is x)
  b = sizeBC  (distance is 0)
  c = sizeAC  (let say distance is x)

// with this original condition
return !(a+b<c || b+c<a || a+c<b);
*/

@Taywin
I think we need to discuss here what we difine as a triangle. I have looked it up on Wikipedia (yes, I do find that reliable information):

A triangle is one of the basic shapes of geometry: a polygon with three corners or vertices and three sides or edges which are line segments. A triangle with vertices A, B, and C is denoted ABC.
In Euclidean geometry any three non-collinear points determine a unique triangle and a unique plane (i.e. a two-dimensional Euclidean space).

A triangle made with the defenition above CAN have an area of 0. All it needs is a 2D space (we have got it: only two coordinates), and three points (check!).
So I think the triangle (0,0) (1,0) (2,0) should be valid.

My method will return that as valid, yours will not. My conclusion is that my two functions are correct.

(Doesn't really matter: I don't think the user will ever enter three points in one line, what is more important: what does the assignment say?

Thanks for all your help guys. I think i have one more error.

public class Triangle
{
  private double x1;
  private double y1;
  private double x2;
  private double y2;
  private double x3;
  private double y3;
  
  private double sideAB;
  private double sideBC;
  private double sideAC;
  
  private double perimeter;
  private double area;
  
  private double s;
  

  public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
  {
	  this.x1 = x1;
	  this.x2 = x2;
	  this.x3 = x3;
	  this.y1 = y1;
	  this.y2 = y2;
	  this.y3 = y3;  
  }
  
public boolean isValid() 
{
return (sideAB + sideBC >= sideAC && sideBC + sideAC >= sideAB && sideAB + sideAC <= sideBC);
}

  public double getX1()
  {
	  return x1;
  }
  
  
  public double getY1()
  {
	  return y1;
  }
  

  
  public double getX2()
  {
	  return x2;
  }
  

  
  public double getY2()
  {
	  return y2;
  }
  

  public double getX3()
  {
	  return x3;
  }
  

  public double getY3()
  {
	  return y3;
  }
  
  public double getSideAB()
  {
	  double x = Math.pow(((x2)-(x1)),2);
	  double y = Math.pow(((y2)-(y1)),2); 
	  sideAB = Math.sqrt((x+y));
	  return sideAB;
  }

  public double getSideBC()
  {
	  double x = Math.pow(((x3)-(x2)),2);
	  double y = Math.pow(((y3)-(y2)),2); 
	  sideBC = Math.sqrt((x+y));
	  return sideBC;
  }
  
  public double getSideAC()
  {
	  double x = Math.pow(((x1)-(x3)),2);
	  double y = Math.pow(((y1)-(y3)),2); 
	  sideAC = Math.sqrt((x+y));
	  return sideAC;
  }
  
  public double getPerimeter()
  { 
	  perimeter = ((sideAB) + (sideBC) + (sideAC));
	  return perimeter;
  }
  
  public double getArea()
  {
	  s = (perimeter) / 2.0;
	  area = Math.sqrt(s * (s - sideAB) * (s - sideBC) * (s - sideAC)); 
	  return area;
  }
  

}

Is there something wrong with my area calculation? Because the output for area is always -0.0. My professor is not really a good teacher but she's pretty nice. We have to teach ourselves by reading the book.

(Doesn't really matter: I don't think the user will ever enter three points in one line, what is more important: what does the assignment say?

Just answer my question above. Tell me what is returned from your algorithm?

Also, assuming that there will never be a 3 points in one line is ridiculous. When you implement a program and have requirements, you MUST always implement it correctly. The chance that the user will enter invalid inputs may be slim to none, but that does not eliminate the possibility that it will never happen! You should know what they call when you "assume"...

Do not always believe the wikipedia. I found that wikipedia could also be wrong at least once. When I was implementing Hough-transform 3 years ago, I used wikipedia. You know what? The formula on there is up-side-down! I was looking for why it didn't work until I found another university website which explains the equation in the opposite way!

PS: I would admit if I am wrong, but I would not let things that are obviously wrong go by without giving any comment or trying to correct them.

***On topic***
Did you calculate for all sides before you call the getArea() method?

OK, that is a good example. Personally I have never found Wikipedia wrong, but I only use it for easy defenitions like a triangle...

So, I hope you agree with my method then:

public boolean isValid () {
return !(a+b<c || b+c<a || a+c<b);
// return false if any of the statements are true, else return false.
}

I'm sure it did.. because it calculated the perimeter still and the perimeter calculation used the side length.

Well, it uses the perimeter variable, but it does not call the getPerimeter() method. So, Taywin is right to wonder. Place a System.out.println(); call in the getPerimeter method, and check if it is called.

getPerimeter() does NOT compute those sides...

@hiddepolen,
I'm sorry, I can't agree yet... Let me show you...

/*
  .-----------.
  A          B,C

  a = sizeAB  (let say distance is x)
  b = sizeBC  (distance is 0)
  c = sizeAC  (let say distance is x)

  x is a positive value (obviously)
// apply this original condition
!(a+b<c || b+c<a || a+c<b);
!(x+0<x || 0+x<x || x+x<0)
!(x<x || x<x || 2x<0)
!(false || false || false)
==> true (incorrect)
*/

True, getArea() should call getPerimeter(), getPerimeter() should call all the getSides().

What is your definition then of a triangle? Because I think we must both agree that we need three points, and two points (B and C) in the same spot are not two different points. I wouldn't consider that a triangle at all...

I know that it is not a triangle. What is "isValid()" method for if it is not for checking whether 3 points are forming a triangle? And if B and C are at the same location, it is still valid for the requirement for given 3 points.

Ah, yes, I was thinking of something entirely different.
My suggestion then, should solve all the problems:

public boolean isValid () {
return !(a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0);
// return false if any of the statements are true, else return false.
}

Okay I think I got it; it's able to calculate the area, length of the sides, and perimeter.

public class Triangle
{
	private double x1;
	private double y1;
	private double x2;
	private double y2;
	private double x3;
	private double y3;

	
  public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
  {
	  
	  this.x1 = x1;
	  this.x2 = x2;
	  this.x3 = x3;
	  this.y1 = y1;
	  this.y2 = y2;
	  this.y3 = y3;  
	  
  }

  
  public boolean isValid() 
  { 
	  if ( x1 == x2 && x2 == x3) 
		  return false; 
	  else if ( y1 == y2 && y2 == y3) 
		  return false;
	  else if (x1!= x2 && x2 != x3 && x1 != x3 && y1 != y2 && y2 != y3 && y1 != y3) 
		  return false;
	  else return true;
		
  }
  
  
	public double getSideAB()
	{
		return Math.sqrt(Math.pow(((x2)-(x1)),2) + Math.pow(((y2)-(y1)),2));
	}

	public double getSideBC()
	{
		return Math.sqrt(Math.pow(((x3)-(x2)),2) + Math.pow(((y3)-(y2)),2));
	}

	public double getSideAC()
	{
		return Math.sqrt(Math.pow(((x1)-(x3)),2) + Math.pow(((y1)-(y3)),2));
	}

	public double getPerimeter()
	{ 
		double A = getSideAB(), B = getSideBC(), C = getSideAC();
		return (A + B + C);
	}

	public double getArea()
	{
		double perimeter = getPerimeter();
		double s = (perimeter * 0.5); 
		double A = getSideAB(), B = getSideBC(), C = getSideAC();
		return Math.sqrt(s * (s - A) * (s - B) * (s - C));
	}

}

Tester Class

import java.util.*;

public class TriangleTester 
{
    public static void main(String[] args)
    {
		{

		Scanner scanObject = new Scanner(System.in);

		System.out.println("Enter x coordinate for first point: ");
		double x1 = scanObject.nextDouble();
		System.out.println("Enter y coordinate for first point: ");
		double y1 = scanObject.nextDouble();
		System.out.println("Enter x coordinate for second point: ");
		double x2 = scanObject.nextDouble();
		System.out.println("Enter y coordinate for second point: ");
		double y2 = scanObject.nextDouble();
		System.out.println("Enter x coordinate for third point: ");
		double x3 = scanObject.nextDouble();
		System.out.println("Enter y coordinate for third point: ");
		double y3 = scanObject.nextDouble();
		
		
		Triangle myTriangle = new Triangle(x1, y1, x2, y2, x3, y3);
		
		if (myTriangle.isValid())
		{
			System.out.println("Side 1 is: " + myTriangle.getSideAB());
			System.out.println("Side 2 is: " + myTriangle.getSideBC());
			System.out.println("Side 3 is: " + myTriangle.getSideAC());
			System.out.println("The perimeter is: " + myTriangle.getPerimeter());
			System.out.println("The area is: " + myTriangle.getArea()); 
		}
		else
			System.out.println("This triangle is invalid");
		}
    }
}

If it works, please mark the thread as solved!

So does everything check out fine? I know that I didn't use exactly what you guys posted but it helped me out!

public boolean isValid()
{
if ( x1 == x2 && x2 == x3)
return false;
else if ( y1 == y2 && y2 == y3)
return false;
else if (x1!= x2 && x2 != x3 && x1 != x3 && y1 != y2 && y2 != y3 && y1 != y3)
return false;
else return true;
 
}

is that fine?

I don't actually think so, there are so many checks that should not be needed.

- Get all the three sides, A, B and C.
- if (a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0), return false
- else, return true.

Like the example above:

public boolean isValid () {
return !(a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0);
// return false if any of the statements are true, else return false.
}

I tried that, but I couldn't think of a valid set of points that return the output. It kept saying it invalid haha. It's just that the one I have now, it calculates everything.

Our method returns invalid on what set of points?

For your methods, i'm suppose to write it out like

if()
return false;
else
return true;?

Or since you have ! it means it cannot equal to that.
What I used:

public boolean isValid() 
	{
		return !(getSideAB()+getSideBC()<getSideAC() || getSideBC()+getSideAC()<getSideAB() || getSideAB()+getSideAC()<getSideBC() || getSideAB() == 0 || getSideBC() == 0 || getSideAC() == 0);

	}

I tried using the points you gave me. 0,0 1,0 2,0.. it return as invalid.
Edit: Nevermind, these points does not say it's invalid. It just brings me back to my old problem.. it does not calculate the area.

What are a, b and c then in the method I gave you?
If you use it like this, it should work...

public boolean isValid () {
double a = getSideAB();
double b = getSideBC();
double c = getSideCA();
return !(a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0);
// return false if any of the statements are true, else return false.
}

It works, now i'm just back to my old problem.. it's not calculating the area.

public class Triangle
{
	private double x1;
	private double y1;
	private double x2;
	private double y2;
	private double x3;
	private double y3;

	
  public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
  {
	  
	  this.x1 = x1;
	  this.x2 = x2;
	  this.x3 = x3;
	  this.y1 = y1;
	  this.y2 = y2;
	  this.y3 = y3;  
	  
  }
  public boolean isValid () {
	  double a = getSideAB();
	  double b = getSideBC();
	  double c = getSideAC();
	  return !(a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0);
	  // return false if any of the statements are true, else return false.
	  }
  
	public double getSideAB()
	{
		return Math.sqrt(Math.pow(((x2)-(x1)),2) + Math.pow(((y2)-(y1)),2));
	}

	public double getSideBC()
	{
		return Math.sqrt(Math.pow(((x3)-(x2)),2) + Math.pow(((y3)-(y2)),2));
	}

	public double getSideAC()
	{
		return Math.sqrt(Math.pow(((x1)-(x3)),2) + Math.pow(((y1)-(y3)),2));
	}

	public double getPerimeter()
	{ 
		double A = getSideAB(), B = getSideBC(), C = getSideAC();
		return (A + B + C);
	}

	public double getArea()
	{
		double perimeter = getPerimeter();
		double s = (perimeter * 0.5); 
		double A = getSideAB(), B = getSideBC(), C = getSideAC();
		return Math.sqrt(s * (s - A) * (s - B) * (s - C));
	}
	
}

Tester

import java.util.*;

public class TriangleTester 
{
    public static void main(String[] args)
    {
		{

		Scanner scanObject = new Scanner(System.in);

		System.out.println("Enter x coordinate for first point: ");
		double x1 = scanObject.nextDouble();
		System.out.println("Enter y coordinate for first point: ");
		double y1 = scanObject.nextDouble();
		System.out.println("Enter x coordinate for second point: ");
		double x2 = scanObject.nextDouble();
		System.out.println("Enter y coordinate for second point: ");
		double y2 = scanObject.nextDouble();
		System.out.println("Enter x coordinate for third point: ");
		double x3 = scanObject.nextDouble();
		System.out.println("Enter y coordinate for third point: ");
		double y3 = scanObject.nextDouble();
		
		
		Triangle myTriangle = new Triangle(x1, y1, x2, y2, x3, y3);
		
		if (myTriangle.isValid())
		{
			System.out.println("Side 1 is: " + myTriangle.getSideAB());
			System.out.println("Side 2 is: " + myTriangle.getSideBC());
			System.out.println("Side 3 is: " + myTriangle.getSideAC());
			System.out.println("The perimeter is: " + myTriangle.getPerimeter());
			System.out.println("The area is: " + myTriangle.getArea()); 
		}
		else
			System.out.println("This triangle is invalid");
		}
    }
}

I used points (0,0)(1,0)(2,0)
Output:
Side 1 is: 1.0
Side 2 is: 1.0
Side 3 is: 2.0
The perimeter is: 4.0
The area is: 0.0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.