CIS 121 January 28, 2000
Important Concepts
- Abstract Data Type (ADT) - a language has support for ADTs if it
offers support for encapsulation and information hiding.
- Access Control Keywords - allows us to restrict access to information.
- Classpath - list of directories in which the compiler and interpreter
will search for classes. If we use the statement
import a.b;
in our program, then the compiler will search through each directory
listed in the class path looking for a subdirectory called a. Inside
that subdirectory it will look for a file named b.class.
- Encapsulation - the concept of binding data to the code that is to
operate on it.
- Information Hiding - the ability to limit access to information.
- Default package - when classes are generated within the same directory
they are assumed to be part of the same package.
- Deprecated features - Java is an evolving language and some features are
are removed.
- finalize() - a method similar to a constructor but called when an object
is destroyed.
- inner class - another element that can be placed inside a class
definition.
- static initializer - another element that can be placed inside a
class definition. This code has the keyword static in front of it and nothing
else. The static initializer is executed when the class is loaded into
main memory. The static initializer is used to initialize class variables
just like a constructor is used to initialize instance variables.
- StreamTokenizer - a class available in the JDK. It allows us to get
tokens one at a time from a Reader. The StreamTokenizer gives us a token
return type for each token that is read. The important token types are
- TT_EOF (Token Type End of File)
- TT_NUMBER (Token Type Number)
- TT_WORD (Token Type Word)
import java.io.*;
import USA.USA;
class TestStream {
public static void main(String[] args) throws IOException {
StreamTokenizer fileParser;
String inputFileName;
FileReader inputFile;
int tokenType;
int numberCount = 0;
int wordCount = 0;
int otherCount = 0;
inputFileName = USA.getNameOfExistingFile("Enter name of input file: ");
inputFile = new FileReader(inputFileName);
fileParser = new StreamTokenizer(inputFile);
tokenType = fileParser.nextToken();
while (tokenType != StreakTokenizer.TT_EOF) {
switch (tokenType) {
case StreamTokenizer.TT_NUMBER : {
numberCount++;
System.out.println("Number: " + fileParser.nval);
break;
}
case StreamTokenizer.TT_WORD : {
wordCount++;
System.out.println("Word : " + fileParser.sval);
break;
}
default : {
System.out.println("Other token type : " + tokenType);
otherCount++;
}
}
if ((wordCount + numberCount + otherCount) % 20 == 0)
USA.pause();
tokenType = fileParser.nextToken();
}
System.out.println("In the file '" + inputFileName + "' we found: ");
System.out.println(" " + wordCount + " words, ");
System.out.println(" " + numberCount + " numbers, and ");
System.out.println(" " + otherCount + " other tokens.");
}
}
Recall our Classpath Example.
package sub;
public class Abc {
String name;
public Abc (String name) {
this.name = name;
}
public String toString() {
return("(Abc): " + name);
}
}
package sub;
public class Def {
Abc myAbc;
int age;
public Def (String name, int age) {
this.age = age;
myAbc = new Abc(name);
}
public String toString() {
return("I am a Def " + age + " years old and is called " + myAbc);
}
public static void main(String args[]) {
Abc anAbc = new Abc("John");
Def aDef = new Def("David", 21);
System.out.println("Abc object is : " + anAbc);
System.out.prnitln("Def object is : " + aDef);
}
}
import Sub.Def;
public class UseSub {
public static void main(String[] args) {
Def aDef = new Def("Mary", 19);
System.out.println("My Def: " + aDef);
}
}