[ polygon.java ]
(save it in edu/gtu/geometry/shapes folder)
package edu.gtu.geometry.shapes;import edu.gtu.geometry.*;
import java.io.*;
abstract class polygon
{
CartesianPoint cp[];
polygon()
{
}
polygon(CartesianPoint p[])
{
cp=new CartesianPoint[p.length];
for(int i=0;i<p.length;i++)
{
cp[i]=p[i];
}
System.out.println(cp.length);
}
double perimeter()
{
double peri=0,a[];
a=new double[cp.length];
for(int i=0;i<cp.length;i++)
{
if( (i+1) < cp.length)
a[i]=Math.sqrt(Math.pow((cp[i].getx()-cp[i+1].getx()),2)+Math.pow((cp[i].gety()-cp[i+1].gety()),2));
else
a[i]=Math.sqrt(Math.pow((cp[i].getx()-cp[0].getx()),2)+Math.pow((cp[i].gety()-cp[0].gety()),2));
peri=peri+a[i];
}
return peri;
}
abstract void area();
public void move(double x,double y)
{
cp[0].move(x,y);
}
public void move(CartesianPoint p) throws IOException
{
for(int i=0;i<cp.length;i++)
{
if(cp[i].getx()==p.getx() && cp[i].gety()==p.gety())
{
double tempx,tempy;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Value of x : ");
tempx=Double.parseDouble(br.readLine());
System.out.println("Enter Value of y : ");
tempy=Double.parseDouble(br.readLine());
cp[i].move(tempx,tempy);
p.move(tempx,tempy);
}
}
}
}
[ rectangle.java ]
(save it in edu/gtu/geometry/shapes folder)
package edu.gtu.geometry.shapes;
import edu.gtu.geometry.shapes.*;
import edu.gtu.geometry.*;
import java.io.*;
public class rectangle extends polygon
{
public rectangle(CartesianPoint p[])
{
super(p);
}
public void area()
{
double s=0,area,temp=0,a[];
a=new double[cp.length];
for(int i=0;i<cp.length;i++)
{
if( (i+1) < cp.length)
a[i]=Math.sqrt(Math.pow((cp[i].getx()-cp[i+1].getx()),2)+Math.pow((cp[i].gety()-cp[i+1].gety()),2));
else
a[i]=Math.sqrt(Math.pow((cp[i].getx()-cp[0].getx()),2)+Math.pow((cp[i].gety()-cp[0].gety()),2));
}
area=a[0]*a[1];
System.out.println("AREA OF TRIANGLE IS : "+area);
}
public void display()
{
for(int i=0;i<cp.length;i++)
{
System.out.println("POINT 1 : ("+cp[i].getx() + ":" + cp[i].gety() + ")");
}
}
}
[ triangle.java ]
(save it in edu/gtu/geometry/shapes folder)
package edu.gtu.geometry.shapes;
import edu.gtu.geometry.shapes.*;
import edu.gtu.geometry.*;
import java.io.*;
public class triangle extends polygon
{
public triangle(CartesianPoint p[])
{
super(p);
}
public void area()
{
double s=0,area,temp=0,a[];
a=new double[cp.length];
for(int i=0;i<cp.length;i++)
{
if( (i+1) < cp.length)
a[i]=Math.sqrt(Math.pow((cp[i].getx()-cp[i+1].getx()),2)+Math.pow((cp[i].gety()-cp[i+1].gety()),2));
else
a[i]=Math.sqrt(Math.pow((cp[i].getx()-cp[0].getx()),2)+Math.pow((cp[i].gety()-cp[0].gety()),2));
}
s=super.perimeter()/2;
temp=s;
for(int i=0;i<cp.length;i++)
{
temp=temp*(s-a[i]);
}
area=Math.sqrt(temp);
System.out.println("AREA OF TRIANGLE IS : "+area);
}
public double perimeter()
{
return super.perimeter();
}
public void display()
{
for(int i=0;i<cp.length;i++)
{
System.out.println("POINT 1 : ("+cp[i].getx() + ":" + cp[i].gety() + ")");
}
}
}
[ CartesianPoint.java ]
(save it in edu/gtu/geometry folder)
package edu.gtu.geometry;
public class CartesianPoint
{
double x,y;
public CartesianPoint()
{
}
public CartesianPoint(double x)
{
System.out.println("CONSTRUCTOR WTIH ONE PARAMETER");
this.x=x;
this.y=x;
}
public CartesianPoint(double x,double y)
{
System.out.println("CONSTRUCTOR WTIH TWO PARAMETER");
this.x=x;
this.y=y;
}
public double getx()
{
return x;
}
public double gety()
{
return y;
}
public void move(double x,double y)
{
System.out.println("MOVE METHOD WTIH ONE PARAMETER");
this.x=x;
this.y=y;
}
public void move(double x)
{
System.out.println("MOVE METHOD WTIH TWO PARAMETER");
this.x=x;
this.y=x;
}
public void display()
{
System.out.println("Value of x : " + x);
System.out.println("Value of y : " + y);
}
}
[ testrectangle.java ]
(save it in edu/gtu/test folder)
package edu.gtu.test;
import edu.gtu.geometry.*;
import edu.gtu.geometry.shapes.*;
import java.io.*;
class testrectangle
{
public static void main(String args[]) throws IOException
{
CartesianPoint p[];
p=new CartesianPoint[3];
p[0]=new CartesianPoint(5,5);
p[1]=new CartesianPoint(10,5);
p[2]=new CartesianPoint(10,10);
rectangle obj;
obj=new rectangle(p);
System.out.println("PERIMETER OF RECTANGLE IS:" + obj.perimeter());
obj.area();
obj.display();
obj.move(50,50);
obj.area();
obj.area();
obj.display();
obj.move(p[1]);
obj.area();
obj.area();
obj.display();
}
}
[ testtriangle.java ]
(save it in edu/gtu/test folder)
package edu.gtu.test;
import edu.gtu.geometry.*;
import edu.gtu.geometry.shapes.*;
import java.io.*;
class testtriangle
{
public static void main(String args[]) throws IOException
{
CartesianPoint p[];
p=new CartesianPoint[3];
p[0]=new CartesianPoint(5,5);
p[1]=new CartesianPoint(10,5);
p[2]=new CartesianPoint(10,10);
triangle obj;
obj=new triangle(p);
System.out.println("PERIMETER OF TRIANGLE IS:" + obj.perimeter());
obj.area();
obj.display();
obj.move(50,50);
obj.area();
obj.area();
obj.display();
obj.move(p[2]);
obj.area();
obj.area();
obj.display();
}
}
No comments:
Post a Comment