Lecture_06 - ENGR 2200U UOIT/FEAS Fall 2007 ENGR 2710U OO...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design 1 Iteration Professor Saman Kumarawadu ENGR 2710U Fall 2010 – Saman Kumarawadu ENGR 2710U OO Programming and Design Chapter Goals To be able to program loops with the while , for , and do - while statements To avoid infinite loops and off - by - one errors To understand nested loops To learn how to process input • To implement simulations
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design while Loops Repeated executes body of the loop which can be a single or compound statement A while loop will execute as long as its condition is true An infinite loop will occur if the condition never becomes false Most commonly, the statement is a block statement (set of statements delimited by { } ) while ( condition ) statement ; ENGR 2710U OO Programming and Design Calculating the Growth of an Investment Invest $10,000, 5% interest, compounded annually, When will it be doubled? Year Balance 0 $10,000 1 $10,500 2 $11,025 3 $11,576.25 4 $12,155.06 5 $12,762.82
Background image of page 2
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Calculating the Growth of an Investment When has the bank account reached a particular balance? while (balance < targetBalance) { year++; double interest = balance * rate / 100; balance = balance + interest; } ENGR 2710U OO Programming and Design File Investment.java 01: /** 02: A class to monitor the growth of an investment that 03: accumulates interest at a fixed annual rate. 04: */ 05: public class Investment 06: { 07: /** 08: Constructs an Investment object from a starting balance 09: and interest rate. 10: @param aBalance the starting balance 11: @param aRate the interest rate in percent 12: */ 13: public Investment( double aBalance, double aRate) 14: { 15: balance = aBalance; 16: rate = aRate; 17: years = 0 ; 18: } 19: Continued…
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design File Investment.java 20: /** 21: Keeps accumulating interest until a target balance has 22: been reached. 23: @param targetBalance the desired balance 24: */ 25: public void waitForBalance( double targetBalance) 26: { 27: while (balance < targetBalance) 28: { 29: years++; 30: double interest = balance * rate / 100 ; 31: balance = balance + interest; 32: } 33: } 34: 35: /** 36: Gets the current investment balance. 37: @return the current balance 38: */ Continued… ENGR 2710U OO Programming and Design File Investment.java 39: public double getBalance() 40: { 41: return balance; 42: } 43: 44: /** 45: Gets the number of years this investment has 46: accumulated interest.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

Lecture_06 - ENGR 2200U UOIT/FEAS Fall 2007 ENGR 2710U OO...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online