OBJECTIVE:
The objective of this lab is to create the traditional first program, to print “Hello, World” and practice submitting multiple files to Gradescope.
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:
Directory Operations
in the Linux Command Line - Cheat Sheet.File Commands
in the Unix/Linux Command Reference from FOSSwire.com.C
course).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).
which java
to identify the location of java
. You should see something like the following
/usr/bin/java
export PATH=/usr/lib/jvm/java-1.17.0-openjdk-amd64/bin:$PATH
source ~/.bashrc
. This reloads your updated .bashrc file. Also, you can close and reopen the terminal window.java
location. This time, you should see something like the following.
/usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java
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.
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!
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:
javac <filename.java>
.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
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.
.java
extension? What error you get when running Java programs using .class extension?Main
instead of main
inside your source file.main
method is not exactly as presented in the lecture e.g., different argument type or number of arguments?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
?public
and static
on the definition of the main
method. Can you remove public
or static
keywords without receiving errors?For this lab, you will individually submit the following files:
.java
extension. Do NOT include files that end with .class
extension.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.
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.
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:
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.