Wednesday June 21, 2000


Quiz 12


1. What are the five primary components in a class definition?

2. What are the four primary components in a method signature?

3. What environment variable is used by javac and java to locate class files?

4. If we write a program which is part of a package, then where does that program have to be stored?




























There is a technique that you might find helpful in your program.

Suppose you need to swap the values of two variables a and b. You couldn't simply assign a into b because you would lose the value of b. You couldn't simply assign b into a because you would lose the value of a. So you make an intermediate step. Store the value of a into another variable called temp. Then assign b into a. Then assign temp into b.

Recall the problem we discussed at the end of class yesterday.

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

We solved a related problem a couple of weeks ago.

Given a positive number, n, determine whether or not n is prime.

The algorithm for doing this was
1. Get the number, n.

2. Initialize an integer called prime to 1.

3. for (initialize an integer called counter to 2;
        as long as 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 is equal to 1)
      report that n is prime
   else
      report than n is not prime
This is a good strategy when we want to determine whether or not a given number is prime. Notice however that this is not a good approach to take in the problem we are trying to solve.

If we tried to use this code directly, then we would have to write these lines for every integer we are examining. This would be very poor strategy because it would require to write hundreds of lines of redundant code, and we would not even be able to determine how many times we would actually execute this code since we don't know what n is.

There must be a better to execute code that is repeated. The way we do this is with methods.

Recall that the five primary components in a class definition are instance variables, instance methods, class variables, class methods, and constructors.

Since a constructor is a method, notice that 3 of the 5 primary components in a class definition are methods. So we need to have a firm understand of what methods are and how they work.

A method is a collection of code that produces some result. Recall that the signature of a method is the header of a method, or the declaration of the method.

The signature consists of 4 primary components.

  1. access control keyword
  2. return type
  3. name
  4. formal parameter list
When we have class methods we use the keyword static and that would also be considered part of the signature. However all methods can have the 4 primary components in their signature.

For example, here are some method headers.
public int half(int a)

public float logarithm(double a)

public void print_out()

public boolean is_it_prime(int a)
Note a couple of things.
  1. The formal parameter list can be empty.

  2. If we do not wish to have the method return a value, then we make the return type void.
When we wish to use a method, we must know what its return type is. If a method has the return type int, then wherever we can use an int variable, we can use that method.

For example, I can use an int in a addition.

int a = b + c;
Here we assume that a,b, and c are integers.

If we have a method whose signature is public int half(int a), then we can use the following call.

int a = b + half(c);
Now let us look inside the method itself.

Within the method we are free to use any Java constructs that we want. This means we can use if statements, while loops, call other methods, arithmetic operations, logical operations, etc.

Now examine the formal parameter list in the following method signature.

public int add(int a,int b,int c)
The positions of these parameters is important.

When we make a call to this method, we could use the following code.

int sum = add(1,2,3);
Within the body of the method, the first actual parameter, 1, would be placed into the variable a. The second actual parameter, 2, would be placed into the variable b. The third actual parameter, 3, would be placed into the variable c.

Within the body of the method if we make a reference to the variable a, then the compiler will use the variable declared in the formal parameter list even if there is variable called a already declared somewhere else in the program.

public class Test {

   public static int add(int a,int b,int c) {
      System.out.println("The value of a is " + a);
      return(a+b+c);
   }

   public static void main(String[] args) {
      int a = 1;
      int sum = Test.add(2,3,4);
   }

}
Test.java

What will the program output?

Notice here that in the signature of the method I used the keyword static. This means that this method is a class method. This allows me to invoke the method using the name of the class (that is why it is in caps) followed by the name of the method. Recall the dot operator is used to access a method or variable in a class.

This was done because the main method wouldn't really be considered an instance method or class method. It is a special method that is used to execute a Java program. This is what distinguishes an application from an applet.

Note another important part of the body of the method. If we do not use void as the return type in the signature, then we must include a return statement. The syntax of the return statement is

return(value);
This statment must be included in the body of the method if the return type is not void.

Recall also that we need to have braces around the body of the method.

One other thing to note as well is that if we declare a variable inside of the body of the method which has the same name as a variable in another part of the program, then when we refer to that variable in the method, the new variable we declared will be changed.

So the rule the compiler uses when changing the value of a variable is it looks first within itself, either at its formal parameter list or the variables which are declared inside of it. If it does not find the variable declared there, then it searches the program at the place the method was called to find that variable.

For example in the file Test.java if I declared an integer called d within the method add, and also in the main method, then if change d within the method, then the variable that gets changed is the one I declared inside the method.

public class Test2 {

   public static int add(int a,int b,int c) {
      int d = 2;
      d = d + 1;
      System.out.println("The value of a is " + a);
      System.out.println("The value of d is " + d);
      return(a+b+c);
   }

   public static void main(String[] args) {
      int a = 1;
      int d = 1;
      int sum = Test2.add(2,3,4);
   }

}
Test2.java

What will be the output of this program?

Now returning to our problem, it seems like a method will make the task easier. Note the algorithm we would use in solving the problem.
1. Get n.

2. for (initialize an integer called counter to 2;
        as long as counter is less than or equal to n;
        increment counter by 1)
      if (counter is prime)
         report that counter is prime
So the algorithm is very simple. Note however that there is a piece which refers to code that is not given to us in Java. That is, there is not a built in way in Java to determine whether or not a number is prime, so we have to write one. This is where we use a method.

Note that the most appropriate return type for a method which will report whether or not a number has a certain property is a boolean.

So our method signature would look something like

public boolean is_it_prime(int a)
Since the return type is boolean, the method can be used anywhere we use a boolean variable.

Notice that in the algorithm for determinging whether or not a number is prime, we used an integer called prime and set it equal to 1 to indicate that the number is prime. If prime is 0, then the number is not prime. Note that this is the essential activity of a boolean variable. So instead of using an int we can use a boolean.

public static boolean is_it_prime(int a) {
   boolean prime = true;

   for (int counter = 2;counter < n;counter++)
      if (a % counter == 0)
         prime = false;

   return(prime);
}
So now we can use this to implement the algorithm.
import java.io.*;

public class Prime {
   
   public static boolean is_it_prime(int a) {
      boolean prime = true;

      for (int counter = 2;counter < a;counter++)
         if (a%counter == 0)
            prime = false;

      return(prime);
   }

   public static void main(String[] args) throws IOException {
      InputStreamReader stream = new InputStreamReader(System.in);
      BufferedReader keyboard = new BufferedReader(stream);

      System.out.println("Please enter n");

      int n = new Integer(keyboard.readLine()).intValue();

      for (int counter = 2;counter <= n;counter++)
         if (Prime.is_it_prime(counter))
            System.out.println(counter + " is prime");
   }

}
We haven't spent much time talking about logical operators, so let's examine them a little more closely.

We looked at the following logical operators.
<
<=
>
>=
!=
==
&&
||
Consider the following problems and decide how we would use these logical operators.

1. Let a = 3 only if the value of count is 1.

2. Let b = 4 only if a is equal to 3.

3. Let c = 4 if either b is 1 or a is 5.

4. Let d = 5 if either of the following conditions is true
a is 1 and b is 5 or a is 2 and b is 5.

5. Let d = 3 if a is not equal to 4.

6. Let b = 5 if either c is not equal to 5 or b is not equal to 4.

7. Print "Hello" if a is 5.

8. Print "Hello" if either a is 6 or b is not equal to 1.

9. Print "Goodbye" if either of the following three conditions occur
a is 4 and b is not 5 or
c is 5 and e is not 6 or
d is 5 and b is 5

10. Print "Goodbye" if a is less than 6.

11. Let a = 5 is b is less than or equal to 5.

12. Let b = 6 if a is greater than or equal to 6.