Improvfed constructors using this keyword. Filter input through setters.

This commit is contained in:
HeshamTB 2020-09-25 21:33:10 +03:00
parent 7390d3d9f8
commit 6019f0fc41
Signed by: Hesham
GPG Key ID: 74876157D199B09E
3 changed files with 8 additions and 11 deletions

View File

@ -7,8 +7,7 @@ public class Circle extends _Point {
* @param radius * @param radius
*/ */
public Circle(double radius){ public Circle(double radius){
super(); this(radius, 0, 0);
setRadius(radius);
} }
/** /**

View File

@ -10,7 +10,7 @@ public class Cylinder extends Circle {
public Cylinder(double r, double h){ public Cylinder(double r, double h){
super(r); super(r);
// Circle with location 0, 0 and radius r. // Circle with location 0, 0 and radius r.
this.height = h; setHeight(h);
//now a cylinder with radius r and height h at 0,0 //now a cylinder with radius r and height h at 0,0
} }
@ -23,7 +23,7 @@ public class Cylinder extends Circle {
*/ */
public Cylinder(int x, int y,double r, double h){ public Cylinder(int x, int y,double r, double h){
super(r, x, y); //a circle with radius r at x,y super(r, x, y); //a circle with radius r at x,y
this.height = h; setHeight(h);
//cylinder with radius r and height h at x,y //cylinder with radius r and height h at x,y
} }

View File

@ -9,9 +9,7 @@ public class _Rectangle extends _Point {
* @param height height of rectangle * @param height height of rectangle
*/ */
public _Rectangle(double width, double height){ public _Rectangle(double width, double height){
super();// This might be useless. this(0,0, width, height);
this.height = height;
this.width = width;
} }
/** /**
@ -23,21 +21,21 @@ public class _Rectangle extends _Point {
*/ */
public _Rectangle(int x, int y, double w, double h){ public _Rectangle(int x, int y, double w, double h){
super(x, y); super(x, y);
this.width = w; setHeight(h);
this.height = h; setWidth(w);
} }
/** /**
* set height of rectangle * set height of rectangle
* @param h height * @param h height
*/ */
public void setHeight(double h){ this.height = h; } public void setHeight(double h){ if (h >= 0) this.height = h; else this.height = 0;}
/** /**
* set width of rectangle * set width of rectangle
* @param w width * @param w width
*/ */
public void setWidth(double w){ this.width = w; } public void setWidth(double w){ if (w >= 0) this.width = w; else this.width = 0; }
/** /**
* returns height * returns height