Program Definition 25 ::
|
// save this code in edu/gtu/geometry folder
package edu.gtu.geometry;
//program 25
public class CartesianPoint
{
public int x, y;
public CartesianPoint(int a)
{
x = y = a;
}
public CartesianPoint(int a , int b)
{
x = a; y = b;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void move(int a)
{
x = y = a;
}
public void move(int a, int b)
{
x = a; y = b;
}
public void display()
{
System.out.println("("+ x +", " + y + ")");
}
}
// save this code in edu/gtu/geometry/shapes folder
package edu.gtu.geometry.shapes;
import edu.gtu.geometry.*;
public class Triangle
{
CartesianPoint cp[];
public Triangle(CartesianPoint c[])
{
cp = new CartesianPoint[ c.length ];
for(int i=0;i<c.length;i++)
{
cp[i]=c[i];
}
}
public double area()
{
return ((Math.abs(cp[0].getX()*(cp[1].getY()-cp[2].getY()) + cp[1].getX()*(cp[2].getY()-cp[0].getY()) + cp[2].getX()*(cp[0].getY()-cp[1].getY()))/2));
}
public void display()
{
for ( int i = 0 ; i < 3 ; i++ )
{
cp[i].display();
}
}
}
// save this code in edu/gtu/test folder
package edu.gtu.test;
import edu.gtu.geometry.*;
import edu.gtu.geometry.shapes.*;
class TestTriangle
{
public static void main(String args[])
{
CartesianPoint c[] = new CartesianPoint[ 3 ];
c[0] = new CartesianPoint(5,5);
c[1] = new CartesianPoint(10,5);
c[2] = new CartesianPoint(10,10);
Triangle T1 = new Triangle(c);
T1.display();
System.out.println("Area :: " + T1.area() );
}
}
/*
D:\JAVA >javac edu/gtu/test/*.java
D:\JAVA >java edu.gtu.test.TestTriangle
(5, 5)
(10, 5)
(10, 10)
Area :: 12.0
*/
0 comments :