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