{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture_06

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

This preview shows pages 1–6. Sign up to view the full content.

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

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

View Full Document
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 b e doubled? Year Balance 0 \$10,000 1 \$10,500 2 \$11,025 3 \$11,576.25 4 \$12,155.06 5 \$12,762.82
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

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

View Full Document
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. 47: @return the number of years since the start of the investment 48: */ 49: public int getYears() 50: { 51: return years; 52: } 53: 54: private double balance; 55: private double rate; 56: private int years; 57: }
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design File InvestmentTester.java 01: /** 02: This program computes how long it takes for an investment 03: to double.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}