Friday July 7, 2000


Quiz 17


1. When a compiler stops evaluating a logical expression since it can determine the truth or falsehood of the expression by looking at the first condition in the expression what is this called?

2. What type of variable can be used in a switch statement?

3. True/False In the expression A || B, both A and B have to be true in order for the expression to be true.

4. True/False Any statement that can be written as a switch can be rewritten as a nested if with an else.




























Recall our two homework problems from yesterday.

24.A student choosing among payment plans for a college loan wants to keep the monthly payments to less than $200. If the amount of the loan is $5000 then write a program to calculate which plans are acceptable given different loan lengths and simple interest rates.

30. A bicycle shop in Hyannis rents bicycles by the day at different rates throughout the year according to the season. The proprietor also gives a 25% discount if the rental period is greater than 7 days. Renters must also pay a $50 returnable deposit for each bicycle rented. Write a program to input the season and the number of days of rental and then calculate and display a total charge that includes the deposit.

Season Charge
Spring $5.00
Summer $7.50
Autumn $3.75
Winter $2.50


We need to say something about using real numbers in logical tests. Since all numbers must be stored in binary in order to be stored in memory, there is a problem storing real numbers because they do not always have exact binary equivalents. So be careful when using real numbers in logical tests.

We have discussed conditional execution or selection. Let us now review the fundamentals of repeated execution or looping.

Recall that we have three basic ways of implementing loops.
  1. do-while

  2. while

  3. for
do-while

The syntax of the do-while loop is

do
   statements
while (condition)
Since the condition is checked after the body of the loop this is called a posttest loop.

An example of a do-while loop would be the following.

do
   quotient = (int)(a/b);
   remainder = b % a;
   b = a;
   a = remainder;
while (a > 0);
Recall that this is the loop we used to computer the greatest common divisor of a and b. Notice that the difference between this loop and the one shown in one solution is that we don't have to initialize the value of remainder to make sure the loop is entered. Regardless of what the value of remainder is the loop will be entered at least one time.

while

The syntax for the while loop is

while (condition)
   statements
An example of a while loop would be the following.

int remainder = 1;

while (remainder > 0) {
   quotient = (int)(a/b);
   remainder = a % b;
   b = a;
   a = remainder;
}
Note here that we have to initialize the value of remainder to something larger than 0 to make sure the loop is entered at least once.

for

The syntax of the for loop is

for (statement1;condition;statement2) {
   some statements
}
An example of a for loop would be the following.

for (int counter=2;counter <= n;counter++)
   if (is_prime(counter))
      count++;
Note that the for loop is equivalent to the following while loop.

statement1;
while (condition) {
   some statements
   statement2
}
Normally statement1 is used as an initialization since it is only executed once. statement2 is normally used as an increment or a decrement.

Recall that there is a shortcut to incrementing a variable by 1.

If we have the statement counter++; then this is the same as saying counter = counter+1.

We can also use this in a for loop.

for (int counter=1;counter<=10;counter++)
   System.out.println(counter);
This is called a postincrement because the ++ comes after the variable name. This means that the result of the expression counter++ is the value of counter before it is incremented.

We can do a preincrement by placing the ++ before the variable name. This means that the result of the expression ++counter is the value of counter after it is incremented.

Consider the following code.

public class Test {

   public static void main(String[] args) {
      int counter = 1;

      while (counter++ < 10)
         System.out.println(counter);

      System.out.println("\n");

      counter = 1;

      while (++counter < 10)
         System.out.println(counter);
   }

}
This code produces the following.
2
3
4
5
6
7
8
9
10


2
3
4
5
6
7
8
9
Consider the following code.
public class Test1 {

   public static void main(String[] args) {
      int a = 1;

      int b = a++;

      a = 1;

      int c = ++a;

      System.out.println("b is " + b + ".");
      System.out.println("c is " + c + ".");
   }

}
It produces the following.
b is 1.
c is 2.
Recall that a common thing to do in loops is to accumulate sums or products.

We can accumulate a sum by initialing a variable to 0 and then adding something to that sum each time through the loop.

We can accumulate a product by initializing a variable to 1 and then multiplying that sum by something each time through the loop.

Problem

In Calculus the area under a curve can be measured in different ways. One way of doing it is to break the interval under consideration up into smaller intervals and obtain an estimate of the area in each small interval and then add them up.

We can obtain an estimate by approximating the area of each interval with a rectangle.

We can obtain a left hand estimate by evaluating the area at the left side of each interval and adding up the results. We can obtain a right hand estimate by evaluating the area at the right hand side of each interval and adding up the results.

Consider the function f(x) = 1/x on the interval [1,2]. Write a program that will accept as input a number of subintervals and then use the left and right hand estimate for the area under f(x) on the interval [1,2].