[ testtriangle .java ]
import graphics.*; //TO IMPORT CARTESIANPOINT CLASS
import java.io.*;
import java.lang.*;
class ClassNotMatchException extends Throwable
{
void display()
{
System.out.println("Class Not Math Exception Found");
}
}
class triangle
{
CartesianPoint p1,p2,p3;
triangle(CartesianPoint p1,CartesianPoint p2,CartesianPoint p3)
{
this.p1=p1;
this.p2=p2;
this.p3=p3;
}
public void display()
{
System.out.println("\nPOINT 1 : ( " + this.p1.getx() + " : " + this.p1.gety() + " )");
System.out.println("\nPOINT 2 : ( " + this.p2.getx() + " : " + this.p2.gety() + " )");
System.out.println("\nPOINT 3 : ( " + this.p3.getx() + " : " + this.p3.gety() + " )");
}
public void move() throws IOException
{
double newx,newy,diffx,diffy;
System.out.println("Enter New Cordinates of Point 1");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter x-Cordinates :");
newx=Integer.parseInt(br.readLine());
System.out.println("Enter y-Cordinates :");
newy=Integer.parseInt(br.readLine());
diffx=newx-p1.getx();
diffy=newy-p1.gety();
p1.move(newx,newy);
p2.move(p2.getx() + diffx,p2.gety() + diffy);
p3.move(p3.getx() + diffx,p3.gety() + diffy);
}
public void move(Object p1) throws IOException
{
try
{
if( !(p1 instanceof CartesianPoint))
{
ClassNotMatchException obj;
obj=new ClassNotMatchException();
throw obj;
}
else
{
double newx,newy,a,b,c;
System.out.println("Enter New Cordinates of Point 1");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter x-Cordinates :");
newx=Integer.parseInt(br.readLine());
System.out.println("Enter y-Cordinates :");
newy=Integer.parseInt(br.readLine());
// p1.move(newx,newy);
this.p1.move(newx,newy);
}
}
catch(ClassNotMatchException obj)
{
obj.display();
}
}
public void area()
{
double a,b,c,s,perimeter,area;
a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p3.getx()),2)+Math.pow((p1.gety()-p3.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
s=(a+b+c)/2;
area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of Triangle is : " + area);
}
public void perimeter()
{
double a,b,c,perimeter;
a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p3.getx()),2)+Math.pow((p1.gety()-p3.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
perimeter=a+b+c;
System.out.println("Perimeter of Triangle is : " + perimeter);
}
public void rotate(CartesianPoint p1,double x)
{
double newx,newy;
newx=(p1.getx() * Math.cos(x)) - (p1.gety() * Math.sin(x));
newy=(p1.getx() * Math.sin(x)) + (p1.gety() * Math.cos(x));
p1.move(newx,newy);
}
public String toString()
{
return "p1(x):"+p1.getx()+" ; p1(y):"+p1.gety()+" ; p2(x):"+p2.getx()+" ; p2(y):"+p2.gety()+" ; p3(x):"+p3.getx()+" ; p3(y):"+p3.gety();
}
public boolean equals(Object tempobj)
{
if(!(tempobj instanceof triangle))
return false;
if(this.p1.getx() != ((triangle)tempobj).p1.getx() && this.p1.gety() != ((triangle)tempobj).p1.gety())
return false;
if(this.p2.getx() != ((triangle)tempobj).p2.getx() && this.p2.gety() != ((triangle)tempobj).p2.gety())
return false;
if(this.p3.getx() != ((triangle)tempobj).p3.getx() && this.p3.gety() != ((triangle)tempobj).p3.gety())
return false;
return true;
}
}
class rectangle
{
CartesianPoint p1,p2,p3,p4;
rectangle(CartesianPoint p1,CartesianPoint p2,CartesianPoint p3,CartesianPoint p4)
{
double a,b,c,d;
this.p1=p1;
this.p2=p2;
this.p3=p3;
this.p4=p4;
a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p4.getx()),2)+Math.pow((p1.gety()-p4.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
d=Math.sqrt(Math.pow((p3.getx()-p4.getx()),2)+Math.pow((p3.gety()-p4.gety()),2));
if ( !((a==c && b==d) || (a==b && c==d) || (a==d && b==c) ))
{
System.out.println("IT IS NOT A RECTANGLE....");
}
}
public void display()
{
System.out.println("\nPOINT 1 : ( " + this.p1.getx() + " : " + this.p1.gety() + " )");
System.out.println("\nPOINT 2 : ( " + this.p2.getx() + " : " + this.p2.gety() + " )");
System.out.println("\nPOINT 3 : ( " + this.p3.getx() + " : " + this.p3.gety() + " )");
System.out.println("\nPOINT 4 : ( " + this.p4.getx() + " : " + this.p4.gety() + " )");
}
public void move() throws IOException
{
double newx,newy,diffx,diffy;
System.out.println("Enter New Cordinates of Point 1");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter x-Cordinates :");
newx=Integer.parseInt(br.readLine());
System.out.println("Enter y-Cordinates :");
newy=Integer.parseInt(br.readLine());
diffx=newx-p1.getx();
diffy=newy-p1.gety();
p1.move(newx,newy);
p2.move(p2.getx() + diffx,p2.gety() + diffy);
p3.move(p3.getx() + diffx,p3.gety() + diffy);
p4.move(p4.getx() + diffx,p4.gety() + diffy);
}
public void move(CartesianPoint p1) throws IOException
{
try
{
if( !(p1 instanceof CartesianPoint))
{
ClassNotMatchException obj;
obj=new ClassNotMatchException();
throw obj;
}
else
{
double newx,newy;
System.out.println("Enter New Cordinates of Point 1");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter x-Cordinates :");
newx=Integer.parseInt(br.readLine());
System.out.println("Enter y-Cordinates :");
newy=Integer.parseInt(br.readLine());
// p1.move(newx,newy);
this.p1.move(newx,newy);
}
}
catch(ClassNotMatchException obj)
{
obj.display();
}
}
public void area()
{
double a,b,c,d,s,perimeter,area=0;
a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p4.getx()),2)+Math.pow((p1.gety()-p4.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
d=Math.sqrt(Math.pow((p3.getx()-p4.getx()),2)+Math.pow((p3.gety()-p4.gety()),2));
if (a==c && b==d)
{
area=a*b;
}
else if( a==b && c==d)
{
area=a*c;
}
else if( a==d && b==c)
{
area=a*b;
}
System.out.println("Area of rectangle is : " + area);
}
public void perimeter()
{
double a,b,c,d,perimeter=0;
a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p4.getx()),2)+Math.pow((p1.gety()-p4.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
d=Math.sqrt(Math.pow((p3.getx()-p4.getx()),2)+Math.pow((p3.gety()-p4.gety()),2));
if (a==c && b==d)
{
perimeter=2*(a+b);
}
else if( a==b && c==d)
{
perimeter=2*(a+c);
}
else if( a==d && b==c)
{
perimeter=2*(a+b);
}
System.out.println("Perimeter of rectangle is : " + perimeter);
}
public void rotate(CartesianPoint p1,double x)
{
double newx,newy;
newx=(p1.getx() * Math.cos(x)) - (p1.gety() * Math.sin(x));
newy=(p1.getx() * Math.sin(x)) + (p1.gety() * Math.cos(x));
p1.move(newx,newy);
}
public String toString()
{
return "p1(x):"+p1.getx()+" ; p1(y):"+p1.gety()+" ; p2(x):"+p2.getx()+" ; p2(y):"+p2.gety()+" ; p3(x):"+p3.getx()+" ; p3(y):"+p3.gety()+" ; p4(x):"+p4.getx()+" ; p4(y):"+p4.gety();
}
public boolean equals(Object tempobj)
{
if(!(tempobj instanceof rectangle))
return false;
if(this.p1.getx() != ((rectangle)tempobj).p1.getx() && this.p1.gety() != ((rectangle)tempobj).p1.gety())
return false;
if(this.p2.getx() != ((rectangle)tempobj).p2.getx() && this.p2.gety() != ((rectangle)tempobj).p2.gety())
return false;
if(this.p3.getx() != ((rectangle)tempobj).p3.getx() && this.p3.gety() != ((rectangle)tempobj).p3.gety())
return false;
if(this.p4.getx() != ((rectangle)tempobj).p4.getx() && this.p4.gety() != ((rectangle)tempobj).p4.gety())
return false;
return true;
}
}
class testtriangle
{
public static void main(String args[]) throws IOException
{
CartesianPoint p1,p2,p3,p4;
triangle triangle_obj;
rectangle rectangle_obj;
p1=new CartesianPoint(5,5);
p2=new CartesianPoint(10,5);
p3=new CartesianPoint(10,10);
p4=new CartesianPoint(5,10);
rectangle_obj=new rectangle(p1,p2,p3,p4);
triangle_obj=new triangle(p1,p2,p3);
triangle_obj.display();
triangle_obj.perimeter();
triangle_obj.area();
triangle_obj.move(rectangle_obj);
System.out.println("After Moving of Triangle");
triangle_obj.display();
triangle_obj.perimeter();
triangle_obj.area();
/*
triangle_obj.rotate(p1,90);
triangle_obj.rotate(p2,90);
triangle_obj.rotate(p3,90);
triangle_obj.perimeter();
triangle_obj.area();
// rectangle
*/
rectangle_obj.display();
rectangle_obj.perimeter();
rectangle_obj.area();
/*triangle_obj.move();
System.out.println("After Moving of rectangle");
rectangle_obj.display();
rectangle_obj.perimeter();
rectangle_obj.area();*/
rectangle_obj.rotate(p1,90);
rectangle_obj.rotate(p2,90);
rectangle_obj.rotate(p3,90);
rectangle_obj.perimeter();
rectangle_obj.area();
System.out.println("hasCode of triangle :"+ triangle_obj.hashCode());
System.out.println("hasCode of rectangle :"+ rectangle_obj.hashCode());
if(triangle_obj.equals(rectangle_obj))
System.out.println("BOTH OBJECTS ARE EQUAL");
else
System.out.println("BOTH OBJECTS ARE NOT EQUAL");
}
}
[ graphics.java ]
(save it in graphics folder)
(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);
}
}
No comments:
Post a Comment