OOP-Lab01-EnvironmentSetup&JavaBasics
OOP-Lab01-EnvironmentSetup&JavaBasics
Lecturer:
Teaching Assistant:
Introduction
In this lab, we prepare for the development environment, then we see some simple examples and programs
written in the environment. We compile and run the programs on the command line with Java JDK and on
the Eclipse IDE.
Keywords: JDK, JRE, Java installation, programming text editor, IDE
0 Assignment Submission
For this lab class, you will have to turn in your work twice, specifically:
Right after the lab class: for this deadline, you should include any work you have done within the
lab class time to github.
10PM the day after the class: for this deadline, you should include all the two programs (2.2.5 &
2.2.6) and six applications in the exercise section (6.1, 6.2, 6.3, 6.4, 6.5 & 6.6) of this lab , into a
directory namely “Lab01” and push it to your master branch of the valid repository.
Each student is expected to turn in his or her own work and not give or receive unpermitted aid.
Otherwise, we would apply extreme methods for measurement to prevent cheating. Please write
down answers for all questions into a text file named “answers.txt” and submit it within your
repository.
1 Getting Started
1.1 Java Development Kit
Java Platform, Standard Edition Development Kit (JDK) is a development environment for building
applications, applets, and components using the Java programming language. There are many releases of
the platform (the latest being JDK 15) that are available to download. Among them, JDK 8 is the most
widely-used version of Java and it is also the last long-term support (LTS) release that contains JavaFX
(which we will be working extensively with later on in this course). For the above reasons, it is required
that JDK 8 is installed for all the labs in this course. However, if you have installed a later version, you
can still install JavaFX separately (there will be an installation guide in the JavaFX lab in this case).
Page 1 of 21
3. In case a JDK version number is returned (e.g., JDK x.x.x), then JDK has already been installed.
When the JDK version is prior to 1.8, a message "Command 'javac' not found", or a message “'javac'
is not recognized as an internal or external command, operable program or batch file.”, proceed to
step 2 to install Oracle JDK 8. Otherwise, proceed to 1.2.
Note: Linux usually chooses OpenJDK as its default JDK since OpenJDK is open source. However,
Oracle JDK is not completely compatible with OpenJDK and it is recommended to use Oracle JDK .
- Linux:
1. Create installation directory. We shall install Oracle JDK 8 under “/usr/local/java” directory.
$ cd /usr/local
$ sudo mkdir java
2. Extract the downloaded package (e.g., jdk-8u241-linux-x64.tar.gz) to the installation directory.
$ cd /usr/local/java
$ sudo tar xzvf ~/Downloads/jdk-8u241-linux-x64.tar.gz
// x: extract, z: for unzipping gz, v: verbose, f: filename
3. Inform the Linux to use this JDK/JRE
// Setup the location of java, javac and javaws
$ sudo update-alternatives --install "/usr/bin/java" "java"
"/usr/local/java/jdk1.8.0_241/bin/java" 1
// --install symlink name path priority
$ sudo update-alternatives --install "/usr/bin/javac" "javac"
"/usr/local/java/jdk1.8.0_241/bin/javac" 1
$ sudo update-alternatives --install "/usr/bin/javaws" "javaws"
"/usr/local/java/jdk1.8.0_241/bin/javaws" 1
Page 2 of 21
$ sudo update-alternatives --set javac /usr/local/java/jdk1.8.0_241/bin/javac
$ sudo update-alternatives --set javaws /usr/local/java/jdk1.8.0_241/bin/javaws
2 First Programs
2.1 Java Programming Steps
The steps in writing a Java program are illustrated in the following steps and in Figure 2.
Page 3 of 21
Step 1: Write the source code such as the code shown in Figure 3. and save in, e.g., “HelloWorld.java”
file.
Step 2: Compile the source code into Java portable bytecode (or machine code) using the JDK's Java
compiler by issuing the following command.
$ javac HelloWorld.java
Step 3: Run the compiled bytecode using the JDK's Java Runtime by issuing the following command.
$ java HelloWorld
Page 4 of 21
For the better illustration, we can watch the following demo videos.
https://www.youtube.com/watch?v=G1ubVOl9IBw
https://www.youtube.com/watch?v=2Xa3Y4xz8_s
Step 3: Write the source code. The source code is shown in Figure 5.
Step 4: Compile. On a Command Prompt or a Terminal, change the current working directory 1 into the
directory where we have saved the source code. Then issue the following commands.
$ javac HelloWorld.java
$ java HelloWorld
Step 3: Write the source code. The source code is shown in Figure 6
1
In various operating systems, the cd <desired directory name> command (cd stands for change directory) allows us to change
the current working directory to the desired directory. Besides, in Windows 10, to access another drive, we type the drive's
letter, followed by ":". For instance, to change the current working drive to drive D, we issue the command “d:”
Page 5 of 21
Figure 6. The First Dialog Java Application
Step 4: Compile. On a Command Prompt or a Terminal, change the current working directory into the
directory where we have saved the source code. Issue the following commands.
$ javac FirstDialog.java
$ java FirstDialog
Step 3: Write the source code. The source code is shown in Figure 7
Step 4: Compile. On a Command Prompt or a Terminal, change the current working directory into the
directory where we have saved the source code. Issue the following commands.
$ javac HelloNameDialog.java
$ java HelloNameDialog
Page 6 of 21
Step 2: Save the file.
From the Notepad interface, choose File Save. Browse the desired directory, change the file n
ShowTwoNumbers.java,” and click the “Save”
button.
Step 3: Write the source code. The source code is shown in Figure 8
Figure 8. Java Application showing two entered numbers and their sum
Step 4: Compile. On a Command Prompt or a Terminal, change the current working directory into the
directory where we have saved the source code. Issue the following commands.
$ javac ShowTwoNumbers.java
$ java ShowTwoNumbers
Page 8 of 21
Figure 9. Eclipse IDE
Installation guide:
Note: You should install Java 8 or a later version before installing an IDE.
In this instruction guide, we need no installer; we just download the ZIP file and unzip them.
- Netbeans: Download the binary file at the following link. Read README.html for more details.
The application is inside the bin directory.
https://www.apache.org/dyn/closer.cgi/netbeans/netbeans/11.2/netbeans-11.2-bin.zip
If you want to use pre-Apache Netbeans versions, you can see them here (this may not compatible
with later Java version).
- Eclipse: We recommend Eclipse IDE for Enterprise Java Developers. Download the suitable
binary file at the following link. https://www.eclipse.org/downloads/packages/
4 Javadocs help:
▪ Open index.html in the docs folder (download from
https://www.oracle.com/technetwork/java/javase/documentation/jdk8-doc-downloads-
2133158.html)
Page 9 of 21
Figure 10. Java Conceptual Diagram
Page 10 of 21
Figure 11. Java SE API
Page 11 of 21
Figure 13. Create new Java project
4. On the pop-up window, choose Java Project, then click Next > button. If you cannot find it, type
the filter text.
5. On the New Java Project window, let the Project name be “JavaBasics”. Then, click Finish
button.
Page 12 of 21
Figure 16. Open Associated Perspective Window
7. Close the Welcome page; then the Java perspective shows up.
6 Exercises
6.1 Write, compile and run the ChoosingOption program:
Note: We use JavaBasics project for this exercise.
Step 1: Create a class.
- Choose File New Class
Page 13 of 21
- On the pop-up window, set the Name same as the class name in the Figure 19, which is
“ChoosingOption”
Step 2: Write the program. The source code is illustrated in Figure 21.
Page 14 of 21
Page 15 of 21
Page 16 of 21
Figure 21. Choosing Option Application
Page 17 of 21
- Choose Always save resources before launching, then click OK button
Questions:
- What happens if users choose “Cancel”?
- How to customize the options to users, e.g. only two options: “Yes” and “No”, OR “I do”
and “I don’t” (Suggestion: Use Javadocs or using Eclipse/Netbean IDE help).
Page 18 of 21
Figure 25. InputFromKeyboard Application
6.5 Write a Java program to sort a numeric array, and calculate the sum and
average value of array elements.
Note: You must create a new Java project for this exercise.
- The array can be entered by the user or a constant.
6.6 Write a Java program to add two matrices of the same size.
Note: You must create a new Java project for this exercise.
- The matrices can be entered by the user or constants.
Page 20 of 21
7 References
Hock-Chuan, C. (2020, January). How to Install JDK 13 (on Windows, macOS & Ubuntu) and Get
Started with Java Programming. Retrieved from Nanyang Technological University:
https://www3.ntu.edu.sg/home/ehchua/programming/howto/JDK_HowTo.html
Page 21 of 21