Object-Oriented Language and Theory: Lab 1: Environment Preparation and First Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Object-Oriented Language and Theory

Lecturer: NGUYEN Thi Thu Trang, trangntt@soict.hust.edu.vn


Teaching Assistant: DO Minh Hieu, hieudominh@hotmail.com
Lab 1: Environment Preparation and First Programs

Introduction
In this lab, we prepare for the development environment, then we see some simple examples writen in the
prepared enviroment. We compile and run the programs on the command line with Java JDK.
Keywords: jdk, java installation, programming text editor

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. Of all the releases, Oracle
JDK 8 is recommended.
The installation steps are illustrated as follows.
Step 1: Check if JDK has been pre-installed
1. Open a Command Prompt (on Windows. Press Windows+R to open “Run” box. Type “cmd” and then
click “OK” to open a regular Command Prompt) or a Terminal (on Linux or macOS).
2. Issue the following command.
$ javac -version
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.

Step 2: Download Oracle JDK 8


1. Go to Java SE Development Kit (JDK) 8 download site at the following link.
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
2. Download the installation file, depended on the operating system, under “Java SE Development Kit
8u241” section. The recommended file for Linux is Compressed Archive file. We may need an Oracle
Account to download for the Oracle JDK License has changed for releases since April 16, 2019.

Step 3: Install and Configure


- Windows.
1. Install Oracle JDK 8. Run the downloaded installer and follow the instructions.
2. Configure. Launch Control Panel → System and Security → System → Advanced system settings
→ Environment Variables in Advanced tab. Then edit or create variable JAVA_HOME to point to
where the JDK software is located, e.g., "C:\Program Files\Java\jdk1.8.0_241").
- Linux.
1. Create installation directory. We shall install Oracle JDK 8 under “/usr/local/java” directory.
$ cd /usr/local
Page 1 of 7
$ 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

// Use this Oracle JDK/JRE as the default


$ sudo update-alternatives --set java /usr/local/java/jdk1.8.0_241/bin/java
// --set name path
$ 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
- macOS. Double-click the DMG file and follow the instructions.

Step 4: Verify the JDK Installation. Issue the following command.


$ javac -version

Figure 1-Development Environment

1.2 Programming Text Editor


In this lab, Visual Studio Code works as our development environment. The installation steps are illustrated
as follows.

Step 1: Download installer. Go to VS Code download site at the following link and download the suitable
installer. https://code.visualstudio.com/download

Step 2: Install Visual Studio Code. Run the downloaded installer and follow the installation instruction.

Page 2 of 7
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.
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

Figure 2-Compile a Java application by command line

Figure 3-The First Java Application

The result is shown in Figure 4.

Page 3 of 7
Figure 4-Result

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

2.2 The Very First Java Programs

2.2.1 Write, compile the first Java application:


Step 1: Create a new file. From the Visual Studio Code interface, choose File → New File.

Step 2: Save the file. From the Visual Studio Code interface, choose File → Save. Browse the desired
directory, change the file name to “HelloWorld.java,” and click “Save” button.

Step 3: Write the source code. The source code is shown in Figure 5.

Figure 5-The First Java Application

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

1
In various operating system, 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 4 of 7
2.2.2 Write, compile the first dialog Java program
Step 1: Create a new file. From the Visual Studio Code interface, choose File → New File.

Step 2: Save the file. From the Visual Studio Code interface, choose File → Save. Browse the desired
directory, change the file name to “FirstDialog.java,” and click “Save” button.

Step 3: Write the source code. The source code is shown in Figure 6

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

2.2.3 Write, compile the first input dialog Java application


Step 1: Create a new file. From the Visual Studio Code interface, choose File → New File.

Step 2: Save the file. From the Visual Studio Code interface, choose File → Save. Browse the desired
directory, change the file name to “HelloNameDialog.java,” and click “Save” button.

Step 3: Write the source code. The source code is shown in Figure 7

Figure 7- The First Input 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.
Page 5 of 7
$ javac HelloNameDialog.java
$ java HelloNameDialog

2.2.4 Write, compile, and run the following example:


Step 1: Create a new file. From the Visual Studio Code interface, choose File → New File.

Step 2: Save the file. From the Visual Studio Code interface, choose File → Save. Browse the desired
directory, change the file name to “ShowTwoNumbers.java,” and click “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

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 6 of 7
2.2.6 Write a program to solve:
- The first-degree equation (linear equation) with one variable
Note: A first-degree equation with one variable can have a form such as 𝑎𝑥 + 𝑏 = 0 (𝑎 ≠ 0), where x is
,
the variable, and a and b are coefficients. Given a and b, the equation has a unique solution 𝑥 = − -.
Additionally, we only consider real number in this task for simplicity.

- The system of first-degree equations (linear system) with two variables


Note: A system of first-degree equations with two variables 𝑥. and 𝑥/ can be written as follows.
𝑎 𝑥 + 𝑎./ 𝑥/ = 𝑏.
0 .. .
𝑎/. 𝑥. + 𝑎// 𝑥/ = 𝑏/
Given the coefficients 𝑎.. , 𝑎./ , 𝑎/. , 𝑎// , 𝑏. , and 𝑏/ , we derive the following determinants.
𝑎.. 𝑎./
D = 2𝑎 2 = 𝑎.. 𝑎// − 𝑎/. 𝑎./
/. 𝑎//
𝑏 𝑎./
D. = 3 . 3 = 𝑏. 𝑎// − 𝑏/ 𝑎./
𝑏/ 𝑎//
𝑎 𝑏.
D/ = 3 .. 3 = 𝑎.. 𝑏/ − 𝑎/. 𝑏.
𝑎/. 𝑏/
7 7
In case 𝐷 ≠ 0, the system has a unique solution (𝑥. , 𝑥/ ) = 6 78 , 79:.
In case 𝐷 = 0, the system has infinitely many solutions if 𝐷; = 𝐷< = 𝐷 = 0.
Otherwise, the system has no solution.
Additionally, we only consider real number in this task for simplicity.

- The second-degree equation with one variable


Note: A second-degree equation with one variable (i.e., quadratic equation) can have a form such as 𝑎𝑥 / +
𝑏𝑥 + 𝑐 = 0, where x is the variable, and a, b, and c are coefficients (𝑎 ≠ 0). Given a, b, and c, the equation
,
has the discriminant Δ = 𝑏 / − 4𝑎𝑐. In case Δ = 0, the equation has double root − /-. If Δ > 0, the equation
A,B√D A,A√D
has two distinct roots /-
and /-
. Otherwise, the equation has no solution.
Additionally, we only consider real number in this task for simplicity.

3 Assignment Submission
You must put all the six programs 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.

4 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 7 of 7

You might also like