Object-Oriented Language and Theory: Lab 1: Environment Preparation and First Programs
Object-Oriented Language and Theory: Lab 1: Environment Preparation and First Programs
Object-Oriented Language and Theory: 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
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
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 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
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.
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
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 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
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
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.
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