Object-Oriented Language and Theory: Lab 01: Environment Setup and Java Basics
Object-Oriented Language and Theory: Lab 01: Environment Setup and Java Basics
Object-Oriented Language and Theory: Lab 01: Environment Setup and Java Basics
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
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. For this course, since we will be
working extensively with JavaFX later on, it is required that JDK 8 is installed (since later versions no
longer include JavaFX). 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). Therefore, we recommend you
to install JDK 8 for all the labs in this course.
- 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 17
Figure 1-Development Environment
2 First Programs
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 3 of 17
Figure 2-Compile a Java application by command line
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
Page 4 of 17
2.2.1 Write, compile the first Java application:
Step 1: Create a new file. From the Notepad interface, choose File → New File.
Step 2: Save the file. From the Notepad interface, choose File → Save. Browse the desired directory,
change the file name to “HelloWorld.java” and hit the “Save” button.
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 directory1 into the
directory where we have saved the source code. Then issue the following commands.
$ javac HelloWorld.java
$ java HelloWorld
Step 2: Save the file. From the Notepad interface, choose File → Save. Browse the desired directory,
change the file name to “FirstDialog.java,” and click the “Save” button.
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 17
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 2: Save the file. From the Notepad interface, choose File → Save. Browse the desired directory,
change the file name to “HelloNameDialog.java,” and click the “Save” button.
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
Step 2: Save the file. From the Notepad interface, choose File → Save. Browse the desired directory,
change the file name to “ShowTwoNumbers.java,” and click the “Save” button.
Step 3: Write the source code. The source code is shown in Figure 8
Page 6 of 17
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
2.2.5 Write a program to calculate sum, difference, product, and quotient of 2 double numbers which
are entered by users.
Notes
- To convert from String to double, you can use
double num1 = Double.parseDouble(strNum1)
- Check the divisor of the division
Page 7 of 17
You should handle the case where the values of the coefficients produce infinitely many solutions and the
case where they produce no solution.
Hint:
Use the following determinants:
𝐷 = |𝑎++ 𝑎+, 𝑎,+ 𝑎,, | = 𝑎++ 𝑎,, − 𝑎,+ 𝑎+, 𝐷+ = |𝑏+ 𝑎+, 𝑏, 𝑎,, | = 𝑏+ 𝑎,, − 𝑏, 𝑎+, 𝐷, = |𝑎++ 𝑏+ 𝑎,+ 𝑏, |
= 𝑎++ 𝑏, − 𝑎,+ 𝑏+
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 17
▪ Click the link Java SE API
Page 10 of 17
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 11 of 17
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 12 of 17
- 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 13 of 17
Figure 21-Choosing Option Application
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).
6.3 Write a program to display a triangle with a height of n stars (*), n is entered by users.
E.g. n=5:
Page 15 of 17
*
***
*****
********
***********
Note: You must create a new Java project for this exercise.
6.4 Write a program to display the number of days of a month, which is entered by users
(both month and year). If it is an invalid month/year, ask the user to enter again.
Note: You must create a new Java project for this exercise.
- The user can either enter a month in its full name, abbreviation, in 3 letters, or in number.
To illustrate, the valid inputs of January are January, Jan., Jan, and 1.
- The user must enter a year in a non-negative number and enter all the digits. For instance,
the valid inputs of year 1999 is only 1999, but not 99, “one thousand nine hundred ninety-
nine”, or anything else.
- A year is either a common year of 365 days or a leap year of 366 days. Every year that is
divisible by 4 is a leap year, except for years that are divisible by 100, but not by 400. For
instance, year 1800 is not a leap year, yet year 2000 is a leap year. In a year, there are twelve
months, which are listed in order as follows.
Septembe Octobe Decembe
Month January February March April May June July August November
r r r
Abbreviation Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.
In 3 letters Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
In Number 1 2 3 4 5 6 7 8 9 10 11 12
Days of
Month in
31 28 31 30 31 30 31 31 30 31 30 31
Common
Year
Days of
Month in 31 29 31 30 31 30 31 31 30 31 30 31
Leap Year
Page 16 of 17
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.
7 Assignment Submission
You must put 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, written by yourself, into a directory namely “Lab01” and push it to your master
branch of the valid repository before the deadline announced in the class.
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.
8 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 17 of 17