java - Validating an objects arguments -
i'm not sure how validate info of object's arguments(parameters? if can explain difference, great!). basically, default values of variables need 1.0 when run below 1.0, doesn't take consideration if statements have put up. example, negative values stay negative.how can make if below 1.0, must set 1.0? thank you!
private double length; private double width; private double height; public box(double l, double w, double h){ length=l; if(l<1.0) l=1.0; width=w; if(w<1.0) w=1.0; height=h; if(h<1.0) h=1.0; } public void setlength(double l){ if(l<1.0) l=1.0; } public void setwidth(double w){ if(w<1.0) w=1.0; } public void setheight(double h){ if(h<1.0) h=1.0; }
here main
box box3= new box(7,8,9); box box4= new box(-1.0,-2.0,-3.0);
you setting local variables instead of members, have different names :
private double length; private double width; private double height;
for example, here's fix constructor :
public box(double l, double w, double h){ length=l; if(l<1.0) length=1.0; width=w; if(w<1.0) width=1.0; height=h; if(h<1.0) height=1.0; }
your setters worse, nothing. fix them assigning input value instance member :
public void setheight(double h){ height = h; if(h<1.0) height=1.0; }
Comments
Post a Comment