Wednesday June 14, 2000


Quiz 7


1. How many bytes are used to store an int variable?

2. What are the two types of real numbers that we use in Java? What is the difference between them?

3. What are the valid characters that can be used in an identifier?

4. What is the Java keyword used to declare an identifier as a constant?
































Recall from yesterday that we discussed the arithmetic operators available to us in Java. Let us discuss these again to make sure that everyone understands the operators and their order of precedence.

  1. Unary Operators. + or -. They act on one operand. These are operators used to indicate positive or negative numbers.

  2. Binary Multiplicative Operators. They act on two operands.

    1. * - Multiplication
    2. / - Division
    3. % - Remainder or Modulus - Acts on two integers. a % b returns the remainder upon division of a by b.


  3. Binary Additive Operators. They act on two operands.

    1. + - Addition
    2. - - Subtraction
Some of these operators may be used in conjuction with the assignment operator.

For example, suppose we have the following line in a Java program.
counter += 2;
This is equivalent to the line
counter = counter + 2;
If the increment or decrement is 1, then we may use double plus or double minus signs.

counter = counter + 1;
is equivalent to
counter += 1;
is equivalent to
counter++;
We also looked at the casting operator. That is, if we want to take a variable which is of one type and change its type to another we use the following syntax.
(data-type) expression;
For example, suppose we wish to find the quotient upon dividing a by b. We could use the following code.
quotient = (int)(a/b);
When evaluating arithmetic expressions, Java uses the standard hierarchy.

Parentheses

Unary Operators

Casting, Multiplicative Operators

Additive Operators

Assignment Operators
Whenever we want to make it clear that a certain operation should be evaluated as a unit, then we can use paretheses, even though they may not be necessary.

For example,
a = 3 + 4*5;
could also be written
a = 3 + (4*5);
even though the multiplication would be performed first.

When performing complicated arithmetic expressions, parentheses can be useful is helping us organize.

We also discussed logical comparisons.
  1. Equality - ==

  2. Greater Than or Greater Than or Equal To - > >=

  3. Less Than or Less Than or Equal To - < <=

  4. Not Equal To - !=
All of these operators are binary operators. That is they work with two operands. But suppose we can to determine whether or not the number a is between 2 and 3. We would not do the following.
2 < a < 3
Instead we need a way to combine the two logical comparisons 2 < a and a < 3.

We do this with the logical and. The symbol for the logical and is &&.

So in Java to accomplish the logical comparison we want we would write
(2 < a) && (a < b)
Another logical comparison availabe to us is the logical or. The symbol for this is ||. This is the pipe symbol. So we could write the following expression.
(2 < a) || (a < b)
The difference between these two expressions is that with the logical and, both smaller expressions have to be true in order for the whole expression to be true. With a logical or, only one of the expressions need to be true in order for the whole expression to be true.

The following is the truth table for the logical and and logical or.

Input 1 Input 2 Logical and Logical or
True True True True
True False False True
False True False True
False False False False


Because of the importance of logical comparisons, there is a primitive data type in Java which that has only a logical value. This data type is called a boolean, and this the keyword Java uses to declare a logical variable. It has only two possible values, true and false. A boolean variable may be used as the condition in a logical comparison.

The Class Hierarchy Recall that a class an abstract thing and an object is a concrete example of a class.

The JDK that you can download contains many classes that are already written. These include classes to deal with things like processing strings and mathematical operations.

Theses classes are seperated into packages. A package is a collection of related classes. In order to refer to a package we use the following notation.
A.B
A represents a directory where the directory B is located. An example of a package is java.lang.

Within the JDK there is a directory called java. And within that directory is another directory called lang. Inside this directory are class files that make up the package.

Whenever we write a Java program we have this package at our disposal. There are other packages that are contained in the JDK that are useful to us. In order to have access to those, we use the import statement.

import java.io.*;
This tells the compiler to find the java directory. Inside that directory find a directory called io, and then load all the class files it finds there.

A class may contain data, methods, or a combination of both.

A class method is a group of declarations and executable program statements that perform a particular activity.

The signature of a method is a combination of its access control keyword, return type, name, and formal parameter list.

For example, suppose I have the following method.

public int half(int a) {
...
}
public is the access control keyword. The return type is an integer. The name of the method is half. The formal parameter list is int a. The combination of these is called the signature of the method.

Whenever we write a Java program that can be executed with the interpreter, this program is called an application. Another type of Java program is called an applet, which must be run in a browser or the appletviewer.

Let us take another look at this example program and determine what the rest of it means.
public class DecimaltoBinary {
   int original;

   public DecimaltoBinary(int decimal) {
      original = decimal;
   }

   public void convert() {
      int quotient;
      int remainder = 0;

      int num = (int)(Math.log(original)/Math.log(2));

      while (num>=1) {
         quotient = (int)(original/Math.pow(2,num));
         remainder = (int)(original - quotient*Math.pow(2,num));
         System.out.println(quotient);
         original -= quotient*Math.pow(2,num);
         num--;
      }
     
      System.out.println(remainder);
      
   }

   public static void main(String[] args) {
      DecimaltoBinary myConverter = new DecimaltoBinary(32);
      myConverter.convert();
   }
}