Hello
I have been reading a java a book. and the writer puts a lot of code in his constructors. Can someone tell me if this is good practice. An example is below.
I thought that constructors were just for initializing variables, and should not include a lots of code. Maybe l am wrong?
Can someone tell me if the code below is ok to put into a constructor.
Many thanks in advanceCode:public class Beaufort { //instance variables float speedOfWind = 0.0f; int force = 0; String beaufortDiscription; //constructor public Beaufort(float windSpeed) { speedOfWind = windSpeed; if (speedOfWind == 0) { beaufortDiscription = "Calm"; force = 0; } else if (speedOfWind <= 4) { beaufortDiscription = "Light air"; force = 1; } else if (speedOfWind <= 7) { beaufortDiscription = "Light breeze"; force = 2; } else if (speedOfWind <= 11) { beaufortDiscription = "Gentle breeze"; force = 3; } else if (speedOfWind <= 18) { beaufortDiscription = "Moderate breeze"; force = 4; } else if (speedOfWind <= 24) { beaufortDiscription = "Fresh breeze"; force = 5; } else if (speedOfWind <= 31) { beaufortDiscription = "Strong gale"; force = 6; } else if (speedOfWind <= 38) { beaufortDiscription = "Near gale"; force = 7; } else if (speedOfWind <= 46) { beaufortDiscription = "Gale"; force = 8; } else if (speedOfWind <= 54) { beaufortDiscription = "Strong Gale"; force = 9; } else if (speedOfWind <= 63) { beaufortDiscription = "Storm"; force = 10; } else if (speedOfWind <= 73) { beaufortDiscription = "Violent storm"; force = 11; } else if (speedOfWind >= 74) { beaufortDiscription = "Hurricane"; force = 12; } } public int windForce() { return force; } public String description() { return beaufortDiscription; } }
Steve




Reply With Quote