Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Sunday 15 July 2012

Define an abstract class called Polygon. Provide a constructor which takes an array of Cartesian Point as parameter. Also provide method called perimeter, which calculates and returns the perimeter of the Polygon. Declare abstract method area for this class. Also define a method called move, which takes two parameters x and y to specify the destination for the first point of the Polygon, and overload to make it work for Cartesian Point as a parameter. Now update the classes Triangle and Rectangle in the exercise 8 above, to be a subclass of the Polygon class. Write appropriate class with main method to test the polymorphism in the area method.


[ test.java ]
import graphics.*;
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();
void move(double x,double y)
{
cp[0].move(x,y);
}
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);
}
}
}
void display()
{
for(int i=0;i<cp.length;i++)
{
System.out.println("cp" + (i+1) +"("+cp[i].getx() + "," + cp[i].gety() + ")");
}
}
}
class triangle extends polygon
{
triangle(CartesianPoint p[])
{
super(p);
}
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 void display()
{
for(int i=0;i<cp.length;i++)
{
System.out.println("POINT 1 : ("+cp[i].getx() + ":" + cp[i].gety() + ")");
}
}
}
class rectangle extends polygon
{
rectangle(CartesianPoint p[])
{
super(p);
}
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);
}
void display()
{
for(int i=0;i<cp.length;i++)
{
System.out.println("POINT 1 : ("+cp[i].getx() + ":" + cp[i].gety() + ")");
}
}
}

class test
{
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();
}
}


[ graphics.java ] (save it in graphics folder)

package graphics;
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);
}
}



1 comment:

  1. GoTo Command Prompt(Win + R)

    HOW TO COMPILE
    c:\yourdir\graphics\graphics.java
    c:\yourdir\javac test.java

    HOW TO EXECUTE
    c:\yourdir\java test

    ReplyDelete