package thisss;
public class Triangle{
double x,y;
Triangle(){
this(0,0);
}
Triangle(double x){
this(x,x);
}
Triangle(double x, double y){
this.x=x;
this.y=y;
}
void area(){
System.out.println("The area of the triangle is: " + (x*y)/2.0);
}
}
package thisss;
public class Main{
public static void main(String args[]){
Triangle t = new Triangle();
Triangle tri = new Triangle(3);
Triangle tri2 = new Triangle(3,2);
t.area();
tri.area();
tri2.area();
}
}
Using "this" keyword in Constructors
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment