I have a Triangle class
and here is the RightTriangle class which Extends Triangle
how can I prevent the inherited methods to not making the right triangle invalid?
in main I have these code:
and when I run the program I get the default side lengths which are [3 , 4, 5] that sets in the default constructor in Triangle class!
could you please help me to solve this problem?!
public class Triangle {
private int side1, side2, side3;
public Triangle(){
side1 = 3;
side2 = 4;
side3 = 5;
}
public Triangle(int a, int b, int c){
this();
setSide1(a );
setSide2(b );
setSide3(c );
}
public void setSide1 (int a){
if ( a > 0)
side1 = a;
}
public void setSide2 (int b ){
if ( b > 0)
side2 = b;
}
public void setSide3 (int c){
if ( c > 0)
side3 = c;
}
public int getSide1() {
return side1;
}
public int getSide2(){
return side2;
}
public int getSide3(){
return side3;
}
public String toString( ) {
return "[ " + side1 + ", " + side2 + ", " + side3 + " ]" ;
}
public int findPerimeter ( ) {
return side1 + side2 + side3;
}
}
and here is the RightTriangle class which Extends Triangle
public class RightTriangle extends Triangle {
private int side1, side2, side3;
public RightTriangle() {
super();
}
public RightTriangle ( int a, int b , int c){
this();
if ( ((a* a) == (b * b ) + (c* c)) || ((b * b ) == (a * a) + (c * c)) || ((c * c) == (b * b ) + (a * a)) ){
setSides( a, b , c );
}
}
public void setSides(int a, int b, int c){
if( a > 0 && b > 0 && c > 0 ){
side1 = a;
side2 = b;
side3 = c;
}
}
public int getSide1() {
return side1;
}
public int getSide2(){
return side2;
}
public int getSide3(){
return side3;
}
}
how can I prevent the inherited methods to not making the right triangle invalid?
in main I have these code:
public class RightTriangleApplication {
public static void main( String args[] ){
Triangle triangles[] = new Triangle [ 5 ];
triangles [ 0 ] = new Triangle();
triangles [ 1 ] = new RightTriangle();
triangles [ 2 ] = new RightTriangle( 8, 15, 17 );
triangles [ 3 ] = new Triangle( 5, 9, 12 );
triangles [ 4 ] = new RightTriangle( 5, 9, 12 );
for( Triangle t : triangles )
System.out.println( t );
}
}
and when I run the program I get the default side lengths which are [3 , 4, 5] that sets in the default constructor in Triangle class!
could you please help me to solve this problem?!