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

Sunday, 15 July 2012

Make the class Cartesian Point, belong to a package called edu. gtu. geometry, the classes Polygon, Triangle and Rectangle belong to the package edu. gtu. geometry. shapes and the classes Test Cartesian Point, Test Triangle, Test Rectangle and Test Polygon belong to the package edu. gtu. test. Use appropriate access specifiers for the classes and the members of the classes defined in the earlier exercises. Now onwards all the classes must be defined in a package.


[ 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();
}
}

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