CS 214 Lab 9: Java Introduction CS 214 Lab 9: Java Introduction Begin by creating a new subdirectory named java within your new subdirectory. Then copy the program skeletons NameTester.java and Name.java from the labs/09/java class directory into your new directory. (There is also a Makefile there; feel free to copy it too.) Use a text editor to open these files, and take a moment to study them. The file NameTester.java should be essentially the file we created last time, while and Name.java should be empty, aside from documentation. Modules in Java. Java was designed around the object-oriented programming paradigm. Modules are essential to object-oriented programming and thus to Java. The import directive is used in Java for importing packages not natively imported by the java compiler. Any user-defined object declared in a program is either referred to in some manner by the import directive or is a compiled class somewhere in the classpath environment variable. Using your text editor's "cut-and-paste" capabilities, cut the entire definition of class Name from the file NameTester.java and paste it into the file Name.java. Remember that method declarations and definitions are not separated into 2 different files in Java as they are in C++. Using a Java Module. To use a Java module, the compiled module (.class file) must just be in the current directory or in a directory included by the classpath environment variable. Translation: Java source files (files with a .java extension) are compiled using the javac byte-compiler. The syntax: javac -deprecation SourceFile.java
generates bytecode as opposed to the object files generated in most languages. The bytecode is where the beauty of Java comes in. This bytecode is in a file SourceFile.class and now may be used by a Java interpreter on any platform. Our Java interpreter may be invoked by entering:
java SourceFile
Note: The .class extension should not be used here. By compiling a source file using the javac byte-compiler, each supporting class will be compiled as well -- no special linking commands are needed in Java! So now compile the NameTester.java source file, and then look in your directory to see both NameTester.class and Name.class. Testing. Now you may use the Java interpreter to run your NameTester program. Check to make certain that it runs correctly before continuing. Use script to create a script.java file in which you cat your source file, show that its builds correctly, and show that your program runs correctly. That concludes this part of the lab. Calvin > CS > 214 > Labs > 09 > Java This page maintained by Joel Adams.