Program Definition 6 ::
Define a class called Triangle, which has constructor with three parameters, which are of type Cartesian Point, defined in the exercise 6. Provide methods to find the area and the perimeter of the Triangle, a method display() to display the three Cartesian Points separated by ':' character, a method move() to move the first Cartesian Point to the specified x, y location, the move should take care of relatively moving the other points as well, a method called rotate, which takes two arguments, one is the Cartesian Point and other is the angle in clockwise direction. Overload the move method to work with Cartesian Point as a parameter. Now define a class called TestTriangle to test the various methods defined in the Triangle class.
class CartesianPoint
{
int x, y;
CartesianPoint(int a)
{
x = y = a;
}
CartesianPoint(int a , int b)
{
x = a; y = b;
}
protected int getX()
{
return x;
}
protected int getY()
{
return y;
}
protected void move(int a)
{
x = y = a;
}
protected void move(int a, int b)
{
x = a; y = b;
}
public void display()
{
System.out.println(" point(x, y) = (" + x +", " + y + ")");
}
}
class Triangle
{
private CartesianPoint A, B, C;
Triangle ( CartesianPoint p1, CartesianPoint p2, CartesianPoint p3 )
{
A = p1 ; B = p2 ; C = p3;
}
public void display()
{
System.out.println("A(x,y):B(x,y):C(x,y) ==>> " + "(" +A.getX() + ", " + A.getY() + "):" +"(" +B.getX() + ", " + B.getY() + "):" + "(" +C.getX() + ", " + C.getY() + ")");
}
public void move( int a, int b )
{
int d1 = a - A.getX();
int d2 = b - A.getY();
A = new CartesianPoint(a,b);
B = new CartesianPoint( B.getX() + d1 , B.getY() + d2);
C = new CartesianPoint( C.getX() + d1 , C.getY() + d2);
}
public double getArea()
{
return ((Math.abs(A.getX()*(B.getY()-C.getY()) + B.getX()*(C.getY()-A.getY()) + C.getX()*(A.getY()-B.getY()))/2));
}
public double perimeter()
{
double d1,d2,d3;
d1 = Math.sqrt((B.getX()-A.getX())*(B.getX()-A.getX())+(B.getY()-A.getY())*(B.getY()-A.getY()));
d2 = Math.sqrt((C.getX()-B.getX())*(C.getX()-B.getX())+(C.getY()-B.getY())*(C.getY()-B.getY()));
d3 = Math.sqrt((A.getX()-C.getX())*(A.getX()-C.getX())+(A.getY()-C.getY())*(A.getY()-C.getY()));
return (d1+d2+d3);
}
public void rotate(double angle, CartesianPoint D)
{
int x, y;
x = (int)(D.getX()*Math.cos(angle) - D.getY()*Math.sin(angle));
y = (int)(D.getX()*Math.sin(angle) + D.getY()*Math.cos(angle));
A = new CartesianPoint( x , y );
}
}
class TestTriangle
{
public static void main(String args[])
{
CartesianPoint A = new CartesianPoint(15,15);
CartesianPoint B = new CartesianPoint(23,30);
CartesianPoint C = new CartesianPoint(50,25);
Triangle T1 = new Triangle(A,B,C);
T1.display();
T1.move(16,16); // moves only one point
System.out.println("\n After move(16,16) ::>>\n");
T1.display();
System.out.println("Area of Triangle is :: " + T1.getArea());
System.out.println("Perimeter of Triangle is :: " + T1.perimeter());
T1.rotate(90, A);
System.out.println("\n\nAfter 30 degree Rotation at (16,16), New Cordinates are ::\n");
T1.display();
}
}
/*
D:\JAVA >JAVAC Prog6.java
D:\JAVA >JAVa TestTriangle
A(x,y):B(x,y):C(x,y) ==>> (15, 15):(23, 30):(50, 25)
After move(16,16) ::>>
A(x,y):B(x,y):C(x,y) ==>> (16, 16):(24, 31):(51, 26)
Area of Triangle is :: 222.0
Perimeter of Triangle is :: 80.85960988189456
After 30 degree Rotation at (16,16), New Cordinates are ::
A(x,y):B(x,y):C(x,y) ==>> (-20, 6):(24, 31):(51, 26)
*/
0 comments :