Hi all,
A problem asks me to create a point class,line class composing two points, a triangle class composing three lines.Also a right angle triangle and an equilateral triangle class both inherit triangle class.I did this.

public class Point{
    private int x;
    private int y;
    public Point(int x,int y){
    //error check
    //then this.x=x;this.y=y;


int getX(){
    return x;
int getY(){
    return y;
public class Line{
    private Point a,b;
    public Line(Point a,Point b){
public Line(int x1,int y1,int x2,int y2){
         this.a=new Point(x1,y1);
         this.b=new Point(x2,y2);
public int getLength(){
public class Triangle{
     protected Line l1,l2,l3;
     public  Triangle(Line l1,Line l2,Line L3){

    public  Triangle(Point a,Point b,Point c){
              l1=new Line(a,b);
              l2=new Line(b,c);
              l3=new Line(c,d);
   public Triangle(int x,int y,int z){
          //doubted constructor

I wrote the above code.But somebody suggested using Line extends Point which seems unnatural to me.And also my strategy is to tell the constructor create an object and get object without having to error check in driver functions (like main).Another strategy is to take three points in the main method and to check whether they form triangle and then to pass the points to the doubted constructor represented in Triangle class above, which also doesn't seem to be a good design strategy.I'm now having trouble at extending the right angle and the equilateral triangles from triangle class.Shall I to take points and make a possible right angle/equilateral triangle or I to error check before passing them to constructor..My strategy is to strictly follow right kind of object oriented principles.So, I struck here.Please help me doing this...(Any help would be greatly greatly appreciated.)
The other person wrote the code like this.

    class Point{
        int x,y;

   class Line extends Point{
        int a,b,c,d;
        Line(Point p1,Point p2){
       //other code like functions
   class Triangle{
       int d1,d2,d3;   
       Triangle (Line l1,Line l2,Line l3){
 public static void main(String args[]){
       Point p1=new Point(a,b);
       Point p2=new Point(c,d);
       Point p3=new Point(e,f);
       Line l1=new Line(p1,p2);
      //similar code
      if(check whether the data forms triangle){

            // construct triangle
         if(check whether the data forms right angle triangle){

         else if(check whether the data forms equilateral....){

But the above code seems bad to me.....
Please help me because I'm new to object oriented design......
Thanks in advance....

4 Years
Discussion Span
Last Post by jalpesh_007

The "other person" is not helping you. Line extends Point makes no sense. A Line is not a "kind of" Point. The Line class makes no use of the variables or methods it inherits. It's just wrong.
Your Line class, with two Point instance variables to define its ends makes perfect sense.
You define a Tringle by 3 Lines. That's OK, but dangerous. How do you know that the three lines all meet end-to-end properly? It would be much safer to define a Triangle by three Points, then you can trivially derive the Lines if you want them. Three points always define a Triangle, although it may be zero height or have one or more zero length sides. It's up to you whether you treat those as just "interesting" triangles or flag them as errors.
I don't understand how int x,y,z could define a triangle.


one more problem is, you have write

Point p1=new Point(a,b);
Point p2=new Point(c,d);
Point p3=new Point(e,f);

in main method, i think it seems to be wrong.

Because you have not created constructor with argument after line 2.and you are passing argument a,b as asn argument in constructor as above.This seems to be wrong.

And as james says: there is no need of lines,but you have to make 3 points and join them to amke a triangle.

Do proper work on paper,then try to make a code..


This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.