while (CONDITION) { //Objects want to be in loops too! }
public void loopConstructingObjects() { //Construct a rectangle during each loop //And use it for a calculation prn("\nloopConstructingObjects()"); int side = 1, diagonalsSum = 0; while (side <= 10) { //Make a square by setting width and height equal Rectangle rec = new Rectangle(side, side); diagonalsSum += rec.computeDiagonal(); side++; } prn("Sum Of Diagonals: " + diagonalsSum); }
public void loopChangingObject() { //Given a Rectangle -- Increase it's width //and height during each loop prn("\nloopChangingObject"); Rectangle rec = new Rectangle(0, 0); int count = 1; prn("(Before) Rec: " + rec); while (count <= 10) { rec.setWidth(rec.getWidth() + 2); rec.setHeight(rec.getHeight() + 1); count++; } prn("(After) Rec: " + rec); }