Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java inheritance program

Options
  • 14-03-2012 7:22pm
    #1
    Closed Accounts Posts: 3


    Hi Im having trouble with a point and triangle inheritance question in java if anyone could help.
    Write a class triangle that has three vertices represented by instances of the point class.
    here is the code i have so far any advice would be greatly appreciated

    class Point
    {
    double x,y;

    public Point(double x, double y)
    {
    this.x = x;
    this.y = y;
    }
    // constructor of the Point class

    public double getX()
    {
    return x;
    }
    //returns the value of x

    public double getY()
    {
    return y;
    }
    //returns the value of y

    public void setX(double x)
    {
    this.x = x;
    }
    // this procedure sets the value of x

    public void setY(double y)
    {
    this.y = y;
    }
    // sets the value of y

    public String toString()
    {
    return "("+x+","+y+")";
    }
    // converts the values of x and y to a string format

    public double distance(Point p)
    {


    double x1 = x-p.getX();
    double y1 = y-p.getY();
    double d = Math.sqrt(x1*x1+y1*y1);
    return d;
    //gets the distance between the points
    }
    }

    class Triangle extends Point
    {
    private Point p1;
    private Point p2;
    private Point p3;
    //Attributes for triangle using instance of point class

    public Triangle(double x, double y, Point p1, Point p2, Point p3)
    {
    super(x,y);
    p1 = new Point(-3,6);
    p2 = new Point(4,-4);
    p3 = new Point(8,8);
    //constructor for the triangle class
    }

    public double getSidep1()
    {
    double length = p2.distance(p3);
    return length;
    }
    //gets the length of the 1st side of triangle

    public double getSidep2()
    {
    double length = p1.distance(p3);
    return length;
    }
    //gets the length of the second side

    public double getSidep3()
    {
    double length = p1.distance(p2);
    return length;
    }
    //gets the length of the 3rd side of triangle


    public boolean isEquilateral()
    {
    // get the length of each side
    double v1 = getSidep1();
    double v2 = getSidep2();
    double v3 = getSidep3();

    // check if the lengths of each side are equal if they are it is equilateral triangle

    if (p1 != p2)
    {
    return false;
    // not equilateral if two sides have different lengths
    }

    else if (p1 != p3)
    {
    return false;
    // not equilateral if they have got different lengths
    }

    else if (p2 != p3)
    {
    return false;
    // not equilateral if they have got different lengths
    }
    else
    {
    return true;
    // if all sides are equal in length it is an equilateral triangle
    }

    // in the question the definition for isoceles triangle is wrong it is definition of equlateral

    }
    public double area()
    {
    double a = p1.distance(p2);
    double b = p2.distance(p3);
    double c = p3.distance(p1);
    double s = (a+b+c)/2.0;

    return Math.sqrt(s*(s-a)*(s-b)*(s-c));
    //Method to return the area of the triangle

    }

    }


Comments

  • Registered Users Posts: 131 ✭✭CuAnnan


    Why is triangle extending point?


  • Closed Accounts Posts: 3 charliestriker


    I was taking it that it was an inheritance question and that you would need the keyword extends and superclass. I didnt know another way to take the points from the point class in order to get the sides of the triangle


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    Is a triangle a point though? That's what inheritance is. Composition is where a triangle has points. In OOP, you should favour composition.

    You can declare your Point class, then have a Triangle class that has points as you described.


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    Triangles are made up of points, but they aren't a type point, which is singular. So your Triangle class would just have either an array of 3 points or a list of 3 points, or just 3 fixed points.
    class Triangle 
    {
        Point p1 = new Point();
        Point p2 = new Point();
        Point p3 = new Point();
    }
    

    If, for some reason you need to show inheritance (for an assignment or something). You could conceivably come up with an abstraction which you extend from. My syntax is probably crap here

    ie:
    public abstract class Shape
    {
        public ArrayList<Point> Points // etc etc
    }
    

    And then extend
    public class Triangle extends Shape
    {
        public Triangle(Point p1, Point p2, Point p3)
        {
            Points.add(p1);
            Points.add(p2);
            Points.add(p3);
        }
    }
    

    And continue like this
    public class Circle extends Shape
    {
            int radius;
            public Circle(Point p1, int radius)
            {
                  Points.add(p1);
            }
    }
    


  • Closed Accounts Posts: 3 charliestriker


    Thanks everyone

    I got it sorted out by taking out the extends and super keywords and called the points in a method in the triangle class. thanks for your input and your help got me looking at in a different way


  • Advertisement
Advertisement