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){
          this.a=a;
          this.b=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(){
           //code...
}
}
public class Triangle{
     protected Line l1,l2,l3;
     public  Triangle(Line l1,Line l2,Line L3){
              this.l1=l1;
              this.l2=l2;
              this.l3=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
}
//remaining
}

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){
        a=p1.x;
        b=p1.y;
        c=p2.x;
        d=p2.y;
   }
       //other code like functions
   }
   class Triangle{
       int d1,d2,d3;   
       Triangle (Line l1,Line l2,Line l3){
       d1=l1.length();
       d2=l2.length();
       d3=l3.length();
    }
 }
 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){
                  //construct

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

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

Recommended Answers

All 2 Replies

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..

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.