Thursday June 15, 2000
Quiz 8
1. What statement is used to load a package into our program?
2. What are four things that a signature of a method can consist of?
3. What is a logical data type called?
4. Give the truth table for the logical and.
Yesterday we talked about the class hierarchy.
Recall that a package is a collection of related classes. A package is a
subdirectory which contains a collection of class files.
If you download the JDK on your home machine, you will find a directory
called java. Within that directory you will see other directories like
lang, io, etc.
If we wish to use the classes in a package, then they must be imported
into our program. In order to do this, we use a statement similar to the
following.
import a.b.*;
The compiler then searches for a directory called a (we will discuss later
where it searches), and inside that directory looks for a directory called
b. It then loads all the class files that it finds inside of b.
If we use the statement
import a.*;
,
then the compiler searches for a directory called a and loads all the
class files in it.
Classes
Classes are a very important structure in Java.
There are five primary components in a class.
- Instance Methods
- Instance Variables
- Class Methods
- Class Variables
- Constructors
Recall that a class is an abstract type of thing, and an object is a
concrete example of this thing. When we create an object based on a class
we say we are creating an instance of that class.
The difference between an instance variable and a class variable is that
instance variables are different for each new instance of a class, whereas
class variables are the same for every instance of a class. This is also
true for instance methods and class methods.
To distinguish a class variable from an instance variable and a class
method from an instance method we use the keyword
static.
An example of a class method would be the log method in the Math class.
Recall that when we try to invoke a method from a class we use a capital letter.
There is a class called Math in the java.lang package. Within that class
is a class method called log whose return type is double. To invoke this
method we make the call
a = Math.log(2);
When we make this call, the compiler searches for a class called Math,
and inside that class a class method called log.
The other primary component is the constructor. This is a very important
method in a class definition. The job of the constructor is to create a
new instance of the class. Since instance variables are different for
every instance of a class, the primary job of the constructor is to
initialize all instance variables for the new instance of the class we
are creating.
For example, suppose we are creating a class called Room. For each instance
of a room there are properties which are particular to the instance. For example,
each room has its own width and length. The constructor for the Room
class would therefore initialize the width and the length for the new instance
of the Room class we are creating.
Once we create a new instance of the Room class, we now have a Room object.
When we are writing the instance methods for a class, it is important to keep
in mind that all the instance variables of the class are known to that method.
That is, we can write our code assuming that the instance variables have been
initialized. For example, let us consider the following.
public class Car {
int numberOfDoors;
int numberOfMPG;
...
public void returnMPG() {
System.out.println("The number of MPG is " + numberOfMPG);
}
...
}
In the instance method (how do I know it is an instance method?) returnMPG(), the variable numberOfMPG is "known". So it is not necessary to try to input
the value of the variable from the user.
The constructor of a class has a special signature.
The signature is an access control keyword, normally public, followed by the name of the class followed by a formal parameter list. An example of a class
with a constructor is
public class Room {
int width;
int length;
public Room(int w,int l) {
width=w;
width=l;
}
...
}
Notice a couple of things here. The instance variables (how do I know they are instance variables?) are declared inside the class definition. Since the
variables width and length are instance variable, the constructor "knows" about them. That is why we can use the variables in the method.
Recall that there are two types of Java programs, applications and applets. In order to create an application we must place the following method into our
class definition.
public static void main(String[] args) {
...
}
This is a special method which is executed if we run the program. Since we see the keyword static, this would have to considered more of a class method than
an instance method, however, it is best to try to think of this method as a seperate entity. The rest of the class will contain methods and variables, and
this method will be used to create a new instance of our class and perform some activity. For example.
public class Room {
int width;
int length;
public Room(int w,int l) {
width=w;
length=l;
}
public void dimension() {
System.out.println(width*length);
}
public static void main(String[] args) {
Room myRoom = new Room(10,20);
myRoom.dimension();
}
}
Let us examine what happens in the main method. Recall that Java has primitive data types. However, we can also create our own data type. In fact, whenever
we make a new class definition we can use that class name as a data type. So in the first line of the method we use the name of the class, Room, as the data
type. We follow the convention of naming a variable beginning with a lower-case letter. new is a Java keyword which causes the constructor of the class to be
called. So here we are making a call to the constructor of the Room class and sending as parameters 10 and 20. Inside of the constructor, the first parameter
is known as w and has the value 10. The second parameter inside the constructor is known as l and has the value 20. In the constructor we initialize the
instance variables of the class, and, therefore, create a new instance of this class. So now we have an object called myRoom which is an object of type Room.
Now the object myRoom has access to the instance variables and instance methods in the class so we can make a call to the instance method dimension.
Obtaining Input
Obtaining Input from the user is an essential activity. In order to do this in our program we must import the java.io package.
A stream refers to any input source or output destination for data. There are two classes that Java provides in the java.io package. In order to get
input we use the class BufferedReader. The constructor for this class asks for a parameter which is of type Reader, which is another class in java.io. For
reasons we will discuss later we can substitute a variable of type InputStreamReader. InputStreamReader is a class whose constructor has as a parameter a
variable of type InputStream. There is a standard variable of this type in the System class. It is called in. So to create a new instance of the an
InputStreamReader we could use the following code.
InputStreamReader stream = new InputStreamReader(System.in);
Now to get input from the user we can pass this variable to the constructor of the BufferedReader class.
BufferedReader keyboard = new BufferedReader(stream);
Now if we invoke the method readLine() from the BufferedReader class, during execution the program will pause and wait for input from the user.
However, there is a problem. The readLine() method will input a variable of type String. String is a class which is found in the java.lang package.
So to get input from the user we could use the following code.
String s = keyboard.readLine();
During execution the program will pause and wait for input from the user. The input will be placed into the String variable s. Question. What if I want to
input an integer from the user.
Answer. A Wrapper class.
In order to understand what a wrapper class is we need to talk about a fundamental aspect of Java. Recall that the syntax of a class header is
<access control keyword> class classname <extends A> <implements B,C, etc.>
extends is a keyword that allows us to inherit properties of another class. When we extend a class definition we inherit data and methods from that class.
There is a special class defined in the java.lang package called Object. All objects that we create inherit from this class. We do not need to use an extends
clause. All objects that we create will have the data and methods of the Object object.
Therefore all objects in Java can be treated in a similar way since they all inherit from Object.
However, the primitive data types are not objects and cannot be treated as objects. If we do want to treat them as objects, then we must wrap them into an
object which has a variable which will contain the value of the data type. These wrapper classes are found in the java.lang package.
In order to convert the string we get from input into an integer we use the wrapper class Integer.
int num = new Integer(s).intValue();
Note here that I don't just create a new instance of the Integer object and assign that to num because num is not an object. However, there is an instance
method in the Integer class called intValue which returns the integer value of the object.
Obtaining Output
In order to obtain output we normally use the following code.
System.out.println(s);
out is a variable found in the System class. It is an object of a class found in the java.io package. One of the methods in that class is println.
Now that we understand more about classes let us go back and see if we understand what is happening in this program.
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();
}
}