for (int i = 1; i <= n; i++)
{
double interest = balance * rate / 100;
balance = balance + interest;
}
/**
A class to monitor the growth of an investment that
accumulates interest at a fixed annual rate
*/
public class Investment
{
private double balance;
private double rate;
private int years;
/**
Constructs an Investment object from a starting balance and
interest rate.
@param aBalance the starting balance
@param aRate the interest rate in percent
*/
public Investment(double aBalance, double aRate)
{
balance = aBalance;
rate = aRate;
years = 0;
}
/**
Keeps accumulating interest until a target balance has
been reached.
@param targetBalance the desired balance
*/
public void waitForBalance(double targetBalance)
{
while (balance < targetBalance)
{
years++;
double interest = balance * rate / 100;
balance = balance + interest;
}
}
/**
Keeps accumulating interest for a given number of years.
@param numberOfYears the number of years to wait
*/
public void waitYears(int numberOfYears)
{
for (int i = 1; i <= numberOfYears; i++)
{
double interest = balance * rate / 100;
balance = balance + interest;
}
years = years + n;
}
/**
Gets the current investment balance.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Gets the number of years this investment has accumulated
interest.
@return the number of years since the start of the investment
*/
public int getYears()
{
return years;
}
}
/**
This program computes how much an investment grows in
a given number of years.
*/
public class InvestmentRunner
{
public static void main(String[] args)
{
final double INITIAL_BALANCE = 10000;
final double RATE = 5;
final int YEARS = 20;
Investment invest = new Investment(INITIAL_BALANCE, RATE);
invest.waitYears(YEARS);
double balance = invest.getBalance();
System.out.printf("The balance after %d years is %.2f\n",
YEARS, balance);
}
}
Program Run:
The balance after 20 years is 26532.98
Which of the following is true?
for loop can be rewritten as a
while loop, and every while loop can be
rewritten as a for loopfor loops can be rewritten as while loops,
and all while loops can be rewritten as for
loopsfor loops can be rewritten as while loops,
and some while loops can be rewritten as for
loopsfor loops that can't be rewritten as
while loops, and there are while loops that
can't be rewritten as for loops
int sum = 0; for (i = 0; i <= 10; i++) sum = sum + i;
for (i = 1; i <= n; i++)
1 ≤ i ≤ n
Executed n times
for (i = 10; i <= 20; i++)
10 ≤ i ≤ 20
Executed 11 times: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
A fence with ten segments has eleven posts
for (i = a; i <= b; i++) is executed b - a +
1 timesfor (i = 10; i < 20; i++)
Executed 10 times
for (i = a; i < b; i++) is executed b - a timesa is zero
for (i = 0; i < n; i++) is executed n timesfor (i = 0; i < str.length(); i++)
{
String letter = str.substring(i, i + 1);
...
}
for (i = 0; i < str.length(); i++)
for (i = str.length() - 1; i >= 0; i--)
String letter = str.substring(i, i + 1);
char:
char ch = str.charAt(i);
char is only useful for (years = 1;
(balance = balance + balance * rate / 100) < targetBalance;
years++)
System.out.println(years);
sum = 0; for (i = 1; i <= 10; i++); sum = sum + i; System.out.println(sum);
How many times is the condition of this for loop evaluated?
for (int i = 1; i <= 6; i = i + 2)
What does this loop do?
int result = 0;
for (int i = 0; i <= str.length(); i++)
{
char ch = str.charAt(i);
if (Character.isDigit(ch))
result = result + ch;
}
"He110!" ->
"110""He110!" -> 3"He110!" ->
110
CodeCheck (experimental; follow the “Code Completion” link)