Java Program to implement methods abtraction


Define a class called Triangle, which has constructor with three parameters, which are of type
CartesianPoint, defined in the exercise 7. Provide methods to find the area and the perimeter of
the Triangle, a method display() to display the three CartesianPoints separated by ':' character, a
method move() to move the first CartesianPoint to the specified x, y location, the move should
take care of relatively moving the other points as well, a method called rotate, which takes two
arguments, one is the CartesianPoint and other is the angle in clockwise direction. Overload the
move method to work with CartesianPoint as a parameter. Now define a class called
TestTriangle to test the various methods defined in the Triangle class. Similarly also define a
class called Rectangle which has four CartesianPoint.

Program:

 TestTriangle.java

public class TestTriangle
{
  public static void main(String args[])
  {
    //Triangle tr = new Triangle();
   
    //CartesianPoint[] cp = new CartesianPoint[3];
    CartesianPoint c1 = new CartesianPoint(10, 40);
    CartesianPoint c2 = new CartesianPoint(10, 10);
    CartesianPoint c3 = new CartesianPoint(30, 10);

    Triangle tr = new Triangle(c1, c2, c3);
    Triangle tr2 = new Triangle(c1, c2, c3);

    System.out.println("tr2 toString: "+tr2.toString());
    System.out.println("HashCode of tr2: "+tr2.hashCode());

    System.out.println(tr2.equals(tr));                         //true
   
    System.out.println("\nArea of Triangle: "+tr.area());
    System.out.println("\nPerimeter of Triangle: "+tr.perimeter());
    tr.display();

    System.out.println("\nAfter move method ");
    tr.move(12, 44);
    tr.display();

    tr.rotate(c1, 60);
    tr.display();

    tr.move(c1);
    tr.display();
   
  }
}



class Triangle
{

    CartesianPoint cp1, cp2, cp3;
   
   
    Triangle(CartesianPoint cp1, CartesianPoint cp2, CartesianPoint cp3)
    {
        this.cp1=cp1;
        this.cp2=cp2;
        this.cp3=cp3;
    }   
   
    double area()
    {
        double ar = (0.5) * ((cp3.getX() - cp2.getX()) * (cp1.getY() - cp2.getY()));
        return ar;
    }
   
    double perimeter()
    {
        return (cp2.getX() + cp1.getX() + cp3.getX());
    }
   
    void display()
    {
        System.out.println("(x1:y1) = ("+cp1.getX()+":"+cp1.getY()+")");
        //System.out.println("(x1:y1) = ("+String.format("%.2g",cp1.getX())+":"+cp1.getY()+")");
        System.out.println("(x2:y2) = ("+cp2.getX()+":"+cp2.getY()+")");
        System.out.println("(x3:y3) = ("+cp3.getX()+":"+cp3.getY()+")");
    }

    void move(double m, double n)
    {
        double l = cp1.getX();
        double z = cp1.getY();
        cp1.move(m, n);
        cp2.x = cp2.x + (m-l);
        cp2.y = cp2.y + (n-z);
        cp3.x = cp3.x + (m-l);
        cp3.y = cp3.y + (n-z);
       
    }

    void rotate(CartesianPoint c1, float degree)
    {
        //System.out.println(degree / 360);//           12
        this.cp1 = c1;
        cp1.x = cp1.getX() * (degree / 360);
        cp1.y = cp1.getY() * (degree / 360);
       
    }

    void move(CartesianPoint c1)
    {   
        this.cp1 = c1;
        double a = cp1.getX();
        double b = cp1.getY();
        move(a, b);
    }

    public boolean equals(Object obj)
    {
        if(!(obj instanceof Triangle))
        {
            return false;
        }
   
        if(this.cp1.getX() != ((Triangle)obj).cp1.getX())
        {
            return false;
        }

        return true;
    }   

    public int hashCode()                        //cannot be double return type as hashCode in object class has signature of int
    {
       
        return this.cp1.getX();
    }
   
}



TestRectangle.java


Define a class called Triangle, which has constructor with three parameters, which are of type
CartesianPoint, defined in the exercise 7. Provide methods to find the area and the perimeter of
the Triangle, a method display() to display the three CartesianPoints separated by ':' character, a
method move() to move the first CartesianPoint to the specified x, y location, the move should
take care of relatively moving the other points as well, a method called rotate, which takes two
arguments, one is the CartesianPoint and other is the angle in clockwise direction. Overload the
move method to work with CartesianPoint as a parameter. Now define a class called
TestTriangle to test the various methods defined in the Triangle class. Similarly also define a
class called Rectangle which has four CartesianPoint.


class TestRectangle extends TestPolygon
{
   public static void main(String args[])
   {
    CartesianPoint cp1 = new CartesianPoint(10,10);
    CartesianPoint cp2 = new CartesianPoint(10, 20);
    CartesianPoint cp3 = new CartesianPoint(40, 20);
    CartesianPoint cp4 = new CartesianPoint(40, 10);
   
    Rectangle r1 = new Rectangle(cp1, cp2, cp3, cp4);

    System.out.println("Area of Rectangle: "+r1.area());
    System.out.println("Perimeter of Rectangle: "+r1.perimeter());

    r1.display();
    System.out.println("Calling move method");
    r1.move(15, 17);
   }
}


class Rectangle
{

    CartesianPoint c1, c2, c3, c4;

    Rectangle(CartesianPoint c1, CartesianPoint c2, CartesianPoint c3, CartesianPoint c4)
    {
        this.c1 = c1;
        this.c2 = c2;
        this.c3 = c3;
        this.c4 = c4;
    }

    double area()
    {
        return (c4.getX() - c1.getX()) * (c2.getY() - c1.getY());
    }

    double perimeter()
    {
        return ((c4.getX() - c1.getX()) + (c2.getY() - c1.getY()) +  (c3.getX() - c2.getX()) + (c3.getY() - c4.getY())) ;
    }

    void display()
    {
        System.out.println("(x1:y1) = ("+c1.getX()+":"+c1.getY()+")");
        //System.out.println("(x1:y1) = ("+String.format("%.2g",cp1.getX())+":"+cp1.getY()+")");
        System.out.println("(x2:y2) = ("+c2.getX()+":"+c2.getY()+")");
        System.out.println("(x3:y3) = ("+c3.getX()+":"+c3.getY()+")");
        System.out.println("(x4:y4) = ("+c4.getX()+":"+c4.getY()+")");
    }

    void move(double m, double n)
    {
        //System.out.println(n);
        if(m<0)
        {
          
        }
        double l = c1.getX();
      
        double z = c1.getY();
        //System.out.println(z);
        c1.move(m, n);
        c2.x = c2.x + (m-l);
        //System.out.println(c2.x);
        c2.y = c2.y + (n-z);
        //System.out.println(c2.y);
        c3.x = c3.x + (m-l);
        c3.y = c3.y + (n-z);
        c4.x = c4.x + (m-l);
        c4.y = c4.y + (n-z);
        display();
      
    }

}

Comments

Popular Posts