0% found this document useful (0 votes)
2 views3 pages

Triangle

The document presents a Java class named 'Triangle' that defines a triangle's vertices and sides using Point and Line objects. It includes constructors for creating triangles, methods for editing vertices, printing coordinates, calculating perimeter, and checking triangle properties such as isosceles, equilateral, and right-angled. Additionally, it provides methods to find the maximum and minimum side lengths of the triangle.

Uploaded by

panav.golyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Triangle

The document presents a Java class named 'Triangle' that defines a triangle's vertices and sides using Point and Line objects. It includes constructors for creating triangles, methods for editing vertices, printing coordinates, calculating perimeter, and checking triangle properties such as isosceles, equilateral, and right-angled. Additionally, it provides methods to find the maximum and minimum side lengths of the triangle.

Uploaded by

panav.golyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

File: Untitled Document 1 Page 1 of 3

import java.util.*;
/**
* Write a description of class Polygon here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Triangle
{
Point v1 , v2 , v3;//These instance variables store the point value for each vertice
of a triangle
Line s1 , s2 , s3;//These variables store each side of the triangle

//Default constructor
public Triangle()
{
v1 = new Point();
v1.x = 0;
v1.y = -4;
v2 = new Point();
v2.x = 0;
v2.y = 4;
v3 = new Point();
v3.x = 4;
v3.y = 0;
}

//Parameterised Constructor
public Triangle(Point v1 , Point v2 , Point v3)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;

s1 = new Line();
s1.p1 = v1;
s1.p2 = v2;

s2 = new Line();
s2.p1 = v2;
s2.p2 = v3;

s3 = new Line();
s3.p1 = v3;
s3.p2 = v1;
}

//Parameterised Constructor
public Triangle(Line s1 , Line s2 , Line s3)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;

}
//This Function allows user to edit the coordinates
void editVertices()
{
Scanner sc = new Scanner(System.in);
int option ;
double x , y;

v1.printCoordinates();
System.out.println();
System.out.println("Enter 1. to edit vertice 1 or Enter 0.");
option = sc.nextInt();
File: Untitled Document 1 Page 2 of 3

if(option == 1)
{
v1.readCoordinates();
System.out.println();
}
v2.printCoordinates();
System.out.println();
System.out.println("Enter 1. to edit vertice 2 or Enter 0.");
option = sc.nextInt();
if(option == 1)
{
v2.readCoordinates();
System.out.println();
}
v3.printCoordinates();
System.out.println();
System.out.println("Enter 1. to edit vertice 3 or Enter 0.");
option = sc.nextInt();
if(option == 1)
{
v3.readCoordinates();
System.out.println();
}
/**
* Output:
*
* The coordinates of the points are :0.0,-4.0
Enter 1. to edit vertice 1 or Enter 0.
0
The coordinates of the points are :0.0,4.0
Enter 1. to edit vertice 2 or Enter 0.
0
The coordinates of the points are :4.0,0.0
Enter 1. to edit vertice 3 or Enter 0.
0
*/
}

//This function prints the coordinates of the vertices


void printVertices()
{
System.out.print("The first Vertice is" + "(" + v1.x + "," + v1.y + ")");
System.out.print("The Second Vertice is" + "(" + v2.x + "," + v2.y + ")");
System.out.print("The Third Vertice is" + "(" + v3.x + "," + v3.y + ")");
/**
* Output:
*
* The first Vertice is(0.0,-4.0)The Second Vertice is(0.0,4.0)The Third Vertice
is(4.0,0.0)
*/
}

//This function prints the length of the sides


void print_vertice_side()
{
printVertices();
System.out.println();
double d1 = s1.getLength();
double d2 = s2.getLength();
double d3 = s3.getLength();
System.out.println("The length of First side is:" + d1);
System.out.println("The length of Second side is:" + d2);
System.out.println("The length of Third side is:" + d3);
}

//This function checks if three lines form a triangle


File: Untitled Document 1 Page 3 of 3

boolean isTriangle()
{
double d1 = s1.getLength();
double d2 = s2.getLength();
double d3 = s3.getLength();
return d1+d2 > d3 ;
}

//This function finds the length of the perimeter


double perimeter()
{
double p = s1.getLength() + s2.getLength() + s3.getLength();
return p;
}
// ? Ternary Operator => Takes three arguments
//This function returns if the trinagle is isosceles
boolean isIsosceles()
{
return (s1.getLength() == s2.getLength()) ? true : (s2.getLength() ==
s3.getLength()) ?
true :(s1.getLength() == s3.getLength()) ? true : false;
}

//This function check if the triangle is equilateral or not


boolean isEquilateral()
{
return s1.getLength() == s2.getLength() && s2.getLength() == s3.getLength();
}

//This checks if the trianlge is rigth angled or not


boolean isRight_angle ()
{
double largest = Math.max (s3.getLength() ,(Math.max(s1.getLength() ,
s2.getLength())));
double smallest = Math.min (s3.getLength() ,(Math.min(s1.getLength() ,
s2.getLength())));
double second_largest = Math.max(Math.min(s1.getLength() , s2.getLength()) ,
Math.min(s1.getLength() , s3.getLength()));

return Math.pow(smallest,2) + Math.pow(second_largest , 2) == Math.pow(largest ,


2);
}

//This function finds the max side


double maxSide()
{
double a = s1.getLength();
double b = s2.getLength();
double c = s3.getLength();

return (a>=b?(a>=c? a : (b>= c? b : c)) : b);


}

//This function finds the min side


double minSide()
{
double a = s1.getLength();
double b = s2.getLength();
double c = s3.getLength();

return (a<=b? (a<=c? a : c): (b <= c? b : c));


}
}

You might also like