Monday June 12, 2000


Quiz 5


1. What are the three types of execution we have talked about?

2. What is the syntax of an if statement without an else?

3. What is guaranteed to happen in a do-while loop that is not guaranteed to happen in a while loop?

4. What is the syntax of a for statement?
































Flowcharts

When trying to understand the flow of control in a program it is helpful sometimes to use a flowchart. There are certain symbols that are used to represent the different activities in a program.

Ellipses are used to indicate the start and end of an algorithm.

For complex blocks of code we use a rectangle with side bars.
For receiving input from the user we use a parallelogram.
When a decision needs to be based on the value of a variable, then we use a diamond.

For an if statement without a corresponding else we can use the following.
If we have an else statement, then we can use the following.
For a switch we use the following diagram. Note that after each case, execution goes to the same place.
For the first type of while loop, a do-while, we use the following diagram.
For the second type of while loop, we use the following.
For a for loop, we use the following.
Let us reconsider some of the problems we looked at last time.

1. Given a positive number determine whether or not it is even.

2. Given a positive number find all of the positive divisors of the number.

3. Determine whether or not a given positive number is prime.

4. Given a positive number, n, find all of the prime numbers less than or equal to n.

Algorithms 1.
1. Get the number, n.

2. Divide n by 2 to get the quotient and the remainder r.

3. If (r equals 0)
      report that n is even
   else
      report that n is not even
2.
1. Get the number, n.

2. Report that 1 is a divisor of n.

3. Report that n is a divisor of n.

4. for (initialize counter to 2;counter is less than n;increment counter by 1) {
      Divide n by counter to get the quotient and remainder r
      if (r equals 0)
         report that counter is a divisor of n
   }
3.
1. Get the number, n

2. Initialize prime to 1.

3. for (initialize counter to 2;counter is less than n;increment counter by 1) {
      Divide n by counter to get the quotient and remainder r
      if (r equals 0)
         set prime to 0
   }

4. if (prime equals 1)
      report that n is a prime number
   else
      report that n is not a prime number
Now find an algorithm to solve problem 4.

In order for you to be able to write code in Java to implement these algorithms, we need to look at some specifics of the language.

Let's start with the simple example we looked at in lab.
public class Example1 {
   public static void main(String[] args) {
      Sytem.out.println("Hello World");
   }
}
In Object-Oriented Programming, we deal with two seperate entities, a class and an object. A class is an abstract thing, and an object is a concrete example of that thing. Here we are creating a new class called Example1.

The syntax of the header statement is as follows.

<access control keyword> class classname <extends A> <implements B, C, etc.>

The parts surrounded with < and > are optional.

The extends clause and the implements clause are things we will deal with later. For the moment we will also just use the access control keyword public. There are three other access control keywords, private, protected, and none, but for now we will just use public.

The body of the class definition must be surrounded by the { and }. This is not optional. Notice that with an if or a while statement, we can eliminate the braces if the block only contains one line of code. But with a class definition we must surround it in braces.

The block within the class body is called a method. Recall that in Object-Oriented Programming we have things called objects that have associated with them properties and methods. The method we have here is a special type of method.

This method is used anytime we want to create a Java application which we can execute. We must use the syntax in the header each time, but the body will be different. When we are writing methods, we also need to surround the body of the method with braces.

Let's look at writing some simple code and making it execute.