Thursday June 22, 2000


Quiz 13


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

2. How do we indicate that a method will not return a value?

3. What statement is used to cause a method to return a value?

4. How do we distinguish between a class method and an instance method?
























Recall that yesterday you were asked to look at these problems and try to translate them into Java code.

Let us look at each one and how you would implement it.
1. Let a = 3 only if the value of count is 1.

if (count == 1)
   a = 3;

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

if (a == 3)
   b = 4;

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

if (b == 1 ||  a == 5)
   c = 4;

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.

if ((a == 1 && b == 5) || (a == 2 && b == 5))
   d = 5;
 
5. Let d = 3 if a is not equal to 4.

if (a != 4)
   d = 3;

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

if (c != 5 || b != 4)
   b = 5;

7. Print "Hello" if a is 5.

if (a == 5)
   System.out.println("Hello");

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

if (a == 6 || b != 1)
   System.out.println("Hello");

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

if ((a == 4 && b != 5) || (c == 5 && e != 6) || (d == 5 && b == 5))
   System.out.println("Goodbye");

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

if (a < 6)
   System.out.println("Goodbye");

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

if (b <= 5)
   a = 5;

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

if (a >= 6)
   b = 6;
There is another topic related to methods that we need to discuss. This topic is known as method overloading.

In Java we are allowed to declare two methods with the same name if they have different parameter lists.

For example, it would be valid to have the following two instance methods in a class definition.

public int add(int a,int b) {
   return(a+b);
}

public int add(int a,int b,int c) {
   return(a+b+c);
}
We could call either method and based on the number of parameters sent the compiler would know which method to use.

Another topic we have mentioned but not spent much time discussing is constants.

Recall that are two types of identifiers, variables and constants. We distinguish between them with the keyword final.

By default if we don't use the keyword final, then the compiler assumes the identifier to be a variable. Variables can change throughtout a program. However if an identifier is declared to be a constant, then it is value is initialized when it is declared, and it can't be changed. So why would we use constants? Consider the following situation.

An astromer has been studying the orbits of certain asteroids and calculating their orbits using mathematical formulas. An important part of these formulas is the mathematical constant pi. He has spent several years writing programs to determine where the asteroids will go so that spacecraft can be placed in orbit without being hit by the asteroids. He has written two millions lines of code, and in each calculation using pi, he uses the double, 3.14.

But then one day because the value of pi is not accurate enough the orbit he projected for a certain asteroid was incorrect and that inaccuracy caused a $110 billion spacecraft to be destroyed. NASA is very upset and orders him to change his program until he finds a value of pi that will give more correct results.

The astronomer is then hit with the realization that he must edit two million lines of code and in every line in which he used 3.14 he must experiment and use more accurate approximations for pi.

This would certainly be a daunting task. There must be a way to make a problem like this easier to solve. One possible solution is to use a constant.

Suppose when the astronomer wrote the program, instead of using 3.14 in his calculations, he used a constant PI which he initialized to 3.14. Then in order to use a more accurate approximation of pi he only needs to make one change. This is one advantage of using constants.

The convention that we will follow in writing programs is that we will use all caps when we declare a constant.

Now that we have talked about methods, variables, and constants, we have talked about most of the essential ingredients in a class definition. Now let us go back and look in more detail at the different parts of a class definition.

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

A class is an abstract thing. That is it describe what properties and activities should be associated with something.

For example, consider a class called Computer.

A computer has certain things associated with it: etc.

Now when an order is placed for a new set of computers, a company will generally build identical machines. So the properties above that are associated with a computer would be associated with each computer that is built. So a particular computer in that group would have the same properties above that all of the other computers have.

So these properties or variables would be considered class variables, because they do not change when we build a new computer.

Now let's consider what properties do change for a new computer. etc.

These properties would be different for each new computer. So we would classify these properties or variables as instance variables.

So notice the distinction between these two types of variables.

A class variable is a variable associated with a class definition that will not change when we create a new instance of that class. That is, we have a class called computer with these properties. However to actually have a machine which we can use, an instance of this class must be made. We say that we create an instance of the class or instantiate an object. The actual instance of the class is called an object.

This object has associated with it these properties. The class variables are the same for every object that is instantiated. However, the instance variables are different for each object that is created. This is the reason that they are instance variables.

So we could have the following class definition.
public class Computer {
   public static int ammountOfHardDriveSpace;
   public static int ammountOfMemory;
   public static String typeOfNetworkCard;
   public static String typeOfVideoCard;
   public static String typeOfSoundCard;
   public static int processorSpeed;
   public int serialNumber;
   public int hardwardAddress;
   public int ipAddress;
   public String contentsOfHardDrive;

   ...

}
As the adjective implies, class variables are static. They do not change.

We have a similar distinction between instance methods and class methods.

Class methods are the same for every instance created of the class. Instance methods are different for each instance created of the class.

We have seen examples of class methods and instance methods already. Recall that when I wanted to take the logarithm of a number, I used the method Math.log(). There is a class method in the class Math that will compute the logarithm of a number.

An example of an instance method is in the call System.out.println. Recall that we begin a class name with a capital letter and start variables with a lower case letter. So here we have a call to a variable called out in the System class. It is a variable and not another class because it starts with a lower case letter. Notice that is it followed by a dot, so it can't be a primitive data type because we can't treat primitive data types like objects. So out is an object. Within the class that defines that object there is a method called println. Because it called from a particula instance of a class and not using the class name, it is an instance method.

The other primary component in a class definition is very important. What is the job of the constructor?

The job of a constructor would be analogous to a building contractor. If you were going to build a house you would call a contractor. Even though he understands what the concept of a house it, he must know certain things before he can build your house.

Similarly a constructor will build a new instance of your class. However because each object that is instantiated has associated with it instance variables, each of those variables must be defined before a new instance of the class can be created. Just like a building contractor can't build a house for you unless he knows what particular properties your house will have, number of stories, dimensions, type, number of rooms, etc., a constructor cannot build a new object for you unless it knows what to assign to the instance variables in the class definition.

The signature of a constructor consists of
  1. access control keyword
  2. name of class
  3. formal parameter list
Note that since a constructor is a method, it can be overloaded. Therefore we can define several ways that an object can be created.

The way we distinguish between different constructors is with the formal parameter list. If we have an empty formal parameter list, then we call the constructor a default constructor.

Now a natural question is how do I make a call to the constructor of a class to create a new instance of the class?

The answer is with the Java keyword new.

A call to create a new object would look something like this.

Computer myComputer = new Computer( ... );
Let's consider the following problems and see how would could create a class definition to solve them.

1. Associated with each number is certain properties: whole number/fraction, prime/not prime, perfect square/not perfect square, even/odd, etc. Write a class called Number which will have these properties as variables. Write methods called is_integer, is_prime, is_square, and is_even which return boolean variables based on whether or not a particular instance of this class possesses these properties. Use a constructor which will accept four boolean values.

There is another Java keyword that we will find useful. It is the keyword this. When the keyword this is used it refers to the current instance of a class.

So if we define a class which has the instance variable a, and we define a method in which we declare a variable called a, then to distinguish between the two we would use the keyword this. this.a would refer to the instance variable a.