Lab: 1 - The Hello World Application

Assigned
Friday, 25 August 2023
Summary
You will submit this lab for grading (5 points). The objective of this lab is to create the traditional first program and practice submitting multiple files to Gradescope.

OBJECTIVE:

The objective of this lab is to create the traditional first program, to print “Hello, World” and practice submitting multiple files to Gradescope.

Part 0: Preparation

First, make sure that both partners can log into MathLAN so that we can begin troubleshooting your account, if necessary.

You will be using the terminal program on MathLAN to complete your labs with your partner for the first few labs. So, you need to remember concepts learned in previous courses.

If you have forgotten Linux commands, take a look to any of these resources:

You will be using a basic text editor for the first two weeks or so. You may use any that are available on MathLAN (pico, vi, emacs, gedit). If you decide to use emacs, check Prof. Johnson’s instructions out for Emacs Text Editor.

We’ll be using Java 17 in this course. Java 17 may not be the default Java; so you may have to update your .bashrc to specify the path to Java 17.

Open a terminal window and type java --version (note the two dashes).

  • If it responds with a version of 17.0 or higher, you are done with Part 0. Move on to Part 1.
  • If it does not respond with a version of 17.0 or higher, you will need to follow these instructions.
    1. In the terminal window, type which java to identify the location of java. You should see something like the following
      /usr/bin/java
      
    2. Edit the file ~/.bashrc (aka /home/username/.bashrc) and add the following line (check the path where java is installed, the “jdk-1.17.0.” directory name might be slightly different in your computer/session).
      export PATH=/usr/lib/jvm/java-1.17.0-openjdk-amd64/bin:$PATH 
      
    3. In the terminal window, type source ~/.bashrc. This reloads your updated .bashrc file. Also, you can close and reopen the terminal window.
    4. In the terminal window, verify the java location. This time, you should see something like the following.
      /usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java 
      
    5. In the terminal window, type java --version again. You should see something like the following to confirm that update.
      openjdk 17.0.7 2023-04-18 
      OpenJDK Runtime Environment (build 17.0.7+7-Debian-1deb11u1) 
      OpenJDK 64-Bit Server VM (build 17.0.7+7-Debian-1deb11u1, mixed mode, sharing) 
      

Remember to do this section for BOTH PARTNERS so you set up for the semester!

If you install Java on a personal computer, try to get Java 17 so that we will be able to compile your assignments. Oracle usually will let you choose.

Part 1: Basic Compilation Pipeline

When learning a new programming language, our first concern before worrying about how the programming language operates, i.e., its semantics, is how to get stuff to appear on the screen—anything! Imagine the computer program development process as a pipeline, a series of steps where the end result is a computer program. For Racket, the pipeline was very straightforward:

1. Type definitions into DrRacket's "definitions window"
	 		|
			|
			V
2. Use those definitions by typing expressions into DrRacket's "interactions window"

which is part of the reason we choose Racket/Scheme for CSC 151!

C is a little bit more involved. In it’s simplest version, the C workflow is as follows.

1. Write the program
	 |
	 |
	 V
2. Compile the program
	 |
	 |
	 V
3. Run the program

More frequently, we include at least one other step.

1. Write the program files
	 |
	 |
	 V
2. Compile the program files
	 |
	 |
	 V
3. Link files and libraries 
	 |
	 |
	 V
4. Run the program

C doesn’t have an interactive environment (commonly known as a REPL or a read-eval print loop) to try our C commands or expressions. Instead, we must write complete programs, compile them using a compiler, and run the resulting executable.

You may have recalled initially having difficult getting a program to work because you messed up one of these steps—for example, getting the syntax of a complete program wrong, not having your source files in the correct place, or invoking gcc with the wrong parameters. But once you had that template of a basic program and the series of commands you needed to invoke, you were set!

Being a descendent of C, Java’s pipeline is nearly identical to the basic C pipeline.

1. Write .java program files (source file)
	 |
	 |
	 V
2. Compile .java files using javac
	 |
	 |
	 V
3. Run the program using the Java application launcher tool (java)

In fact, rather than using gcc, you simply use the javac program instead which compiles Java programs. However, unlike gcc, the javac program produces a Java class file as output, a file with a .class extension. This is not a standalone program like what gcc produces. It is a file that contains Java bytecode which is your code in a low-level form that the Java Virtual Machine (JVM) and Java Runtime Environment (JRE) can execute.

For example, here is an example workflow for compiling at running the canonical “Hello World!” program:

$ emacs HelloWorldApp.java
------
// Your First Program
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}
------
$ javac HelloWorldApp.java
$ ls 
HelloWorldApp.class        HelloWorldApp.java 
$ java HelloWorldApp 
Hello World! 

Note that you do not need to add the .class extension when you run HelloWorld. Java looks for the HelloWorld.class file for you!

Part 2: First Program

So, given the above information, create a folder (workspace for the semester and for this lab) then create a Java program that will print “Hello, World!” (or some variant) to the screen:

  1. Create a source file; write Java code using the text editor of your preference (pico, vi, emacs, gedit)
  2. Compile the program using the command javac <filename.java>.
  3. Run the program using java <filename>

Step 2 generates a .class file. This file contains intermediate instructions from an instruction set called Java bytecode. The class files are not directly executable. Instead, they are interpreted by a program called java, which is done in Step 3. Note that in the third step you just need to provide the name of the Java program without specifying .class extension.

Following those steps, create a program that prints “Hello, World” to standard output (the computer screen). You will hand in your code! Make sure you remember to include header documentation that includes the names of both partners, the date, and your Academic Honesty Statement.

Step by step instructions at “Hello World!” for Solaris OS, Linux, and Mac OS X

Handling Common Errors

When learning a new language, you will encounter plenty of errors and mistakes. One of the most helpful things you can do is identify - early - mistakes that you might make so that you can avoid them in the future.

Answer the following questions and create a PDF file that you submit for this lab via Gradescope.

  1. To compile and run Java programs, when do you need to use .java extension? What error you get when running Java programs using .class extension?
  2. What happens if you compile or run a source file that does not exist?
  3. Is Java case-sensitive? You can check this by running the program using Main instead of main inside your source file.
  4. What happens if the signature of the main method is not exactly as presented in the lecture e.g., different argument type or number of arguments?
  5. A big difference between C and Java is that Java functions (methods) need to be placed inside a class. What happens if you declare a function, which returns void, outside a class?
  6. The other major difference is the presence of public and static on the definition of the main method. Can you remove public or static keywords without receiving errors?

Lab Submission

For this lab, you will individually submit the following files:

  • Source code file that end with .java extension. Do NOT include files that end with .class extension.
  • A text file that contains your commands for compiling and running your program.
  • A PDF file (Handling Common Errors - Answers).
  • In addition, compress the folder containing only your .java files by running the following command on MathLAN:
    tar -cvzf nameOfCompressedFolder.tar.gz nameOfFolder/
    

    If you do it correctly, in the terminal output, you will see the folder and each file within the folder listed. nameOfFolder is the folder that contains your Java and text files for this lab. I strongly recommend creating subfolders for each lab or assignment. Once we start using the Eclipse IDE, make sure that Eclipse is creating subfolders within your workspace since this will save you time and frustration. nameOfCompressedFolder is a user-defined name for the compressed folder. The letter c indicates compressing the contents of a folder. Remember to specify .tar.gz extension at the end of your compressed folder.

Submit nameOfCompressedFolder.tar.gz along with the other files. EACH STUDENT must upload a copy of the compressed file. If you can do this correctly, then we know you will be able to complete programming assignments.

You may want to see the contents of the compressed folder later. Run the following command to extract the contents of your compressed folders. Note that letter x indicates extracting the contents of a folder:

tar -xvzf nameOfCompressedFolder.tar.gz

Note: Download the file you submitted and see the content of the compressed folder. This way you are certain you submitted the correct files. If you submit empty .tar.gz files you won’t get the points for this lab.

For those with more time …

This exercise is completely optional and intended for students who finish early or who want more programming experience.

Use what you know about forming the main method, extrapolate from creation of procedures in C to create a program that produces the output of this silly song. Use static methods for each verse and for repeated text. Here are the lyrics:

	I once wrote a program that wouldn't compile
	I don't know why it wouldn't compile, 
	My TA just smiled.
	
	My program did nothing
	So I started typing.
	I added System.out.println("I <3 coding"),
	I don't know why it wouldn't compile, 
	My TA just smiled.
	
	"Parse error," cried the compiler
	Luckily I'm such a code baller.
	I added a backslash to escape the quotes,
	I added System.out.println("I <3 coding"),
	I don't know why it wouldn't compile, 
	My TA just smiled.
	
	Now the compiler wanted an identifier
	And I thought the situation was getting dire.
	I added a main method with its String [] args,
	I added a backslash to escape the quotes,
	I added System.out.println("I <3 coding"),
	I don't know why it wouldn't compile, 
	My TA just smiled.	

	Java complained it expected an enum
	Boy, these computers are really dumb!
	I added a public class and called it Scum,
	I added a main method with its String [] args,
	I added a backslash to escape the quotes,
	I added System.out.println("I <3 coding"),
	I don't know why it wouldn't compile, 
	My TA just smiled.

Grading

The major objective for this lab is to ensure that students can complete the major steps required for submitting Java programs and text files for labs and assignments. Therefore, each student must complete this lab in their own MathLAN account and report any errors so that we can correct them early in the semester.

Grading criteria:

  • [1/2 point] Author(s) are identified in both Java and text files submitted. The Java file contains an Academic Honesty Statement (at the top of the program)
  • [1/2 point] Compressed folder (with proper file extension)
  • [1 point] Java program (.java file only) compiles and prints “Hello, World” (or some variant) to the screen when run
  • [3 points] Questions under “Handling Common Errors” are submitted as a PDF file to Gradescope

Acknowledgements

This lab is adapted from the Getting Started with Java lab by Sam Rebelsky, Java Tutorials and Building Java Programs by Stuart Reges and Marty Stepp.