The first thing to do when using a computer language for the first time is to write the "Hello, World" program. The purpose is to become familiar with an editor and compiler for the language, and to work out the details of creating a working program. Of course, the "Hello, World" program does not do very much. But if you can get it to work, everything else is just a matter of learning the language.
There are several options for writing Java programs. They range from the very simple, in which you edit and save a file in a folder, compile and run with command line commands, to very complicated, using an IDE (interactive development environment) that manages (and hides) all the details from you. In this class we take an intermediate approach.
IDEs will not be taught in lecture, but you are free to use one if you wish. The problem with using IDEs at this stage in your introduction to Java is that some important logistical details are hidden from you. This is fine for experienced programmers who know and appreciate these details, but not for the beginner.
But the simple approach is too simple. So we will use an approach that applies "packages" and "jars", for organizational and distribution reasons, but uses simple text editing and command-line compiling for the complete experience that is denied in IDEs.
As you will in most of the lab assignments in this class, you will write several programs following the specifications below. After you complete the programs, you will assemble the various files into a single ".jar" file (named lab1a.jar), and post that to your student UNIX account for grading. In Java and in UNIX, filename case matters, so make sure that you spell and case files exactly as specified.
To prepare for this lab, do the following:
Warning: Do not, at this time or any point in the future, delete the .login, .cshrc, and .emacs files in your directory. If you would like to customize your account, do so by adding new commands to the end of these files, not replacing the files. |
This assignment is worth 20 points, and consists of 2 exercises. Zero points will be awarded for programs with misspelled filenames, or incorrect case. Zero points will be awarded for programs that do not compile. Partial points will be awarded for programs that are run but do not conform to all specifications, at the discretion of the grader. Be sure to complete both exercises and post the lab1a.jar before midnight of the due date, because there is a 5-point-per-day lateness penalty for late work. Check the course outline for due dates for lab assignments.
// javac compsci61b/intro/HelloWorld.java // java -ea compsci61b.intro.HelloWorld // jar cf lab1a.jar compsci61b/* // java -ea -cp lab1a.jar compsci61b.intro.HelloWorld // Programmer: ?? ?? // Assignment: HelloWorld.java // Date: 06/??/2007 // Version: original package compsci61b.intro; // the folder where this .java file MUST be stored public class HelloWorld // class name MUST match filename { public static void main(String[] argv) // container for statements to be executed by the program { // to output "Hello! My name is ?? ??" on one line to the console (without the quote marks) System.out.print("Hello!"); // output to the console System.out.print(" "); // followed by a space System.out.print("My name is "); // followed by more text System.out.println("?? ??"); // more text and a EOL (end of line) } }
Here are the detailed specifications:
Note: to list file in a ".jar" file, use this command: jar tf lab1a.jar. To extract files from a ".jar" file, use this command: jar xf lab1a.jar. It fully recreates the folder structure, and also creates a folder names META-INF which can be discarded.
Do not post this version of lab1a.jar -- wait until you complete the remaining exercise, which adds to it.
String s; // a declaration statement for a string reference variable (local) UNUSABLE UNTIL ASSIGNED s = "Hello"; // an assignment statement, refering "s" to the immutable string value "Hello" System.out.println(s); // output the value referred to by "s" s = "?? ??"; // "s" now refers to the value "?? ??" System.out.println("Hello! My name is " + s); // outputs a label and a referenced value, with an EOL // another way -- the long way, explicitly creating an object String name = new String("Hello"); // spins off new object with same value as "Hello", referenced by another local variable System.out.println(name); // releasing the reference name = null; // without references, new String("Hello") is ready for "garbage collection"
Remember that the filename and class name must match, and the "package" statement tells where the source file must be stored.
Once the program is fully working, further embellish it by adding two or more String variables, assigning values to them, and printing them with text labels.
With your files from exercise 1 still in place, complete exercise 2. Then when you recreate lab1a.jar, the files from both exercises will be in the single ".jar" file.
Post your lab1a.jar file to your student UNIX account for grading and credit.
[ Home | Contact Prof. Burns ]