OOP-Lab01-EnvironmentSetup&JavaBasics (2)
OOP-Lab01-EnvironmentSetup&JavaBasics (2)
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.
▪ A week before the next 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
- 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 18
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 18
Figure 2. Compile a Java application by command line
Page 4 of 18
2.2 The Very First Java Programs
- 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 5: After completing this assignment, you should add and commit all of your work to the master
branch. Follow these two steps to achieve the desired result:
- Step 1: Add all changes in the current directory and its subdirectories to the staging area
- (master) $ git add .
- Step 2: Commit the change in the local repository.
- (master) $ git commit -m “Complete assignment 2.2.1”
-
2.2.2 Write, compile the first dialog Java program
- 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 “FirstDialog.java,” and click the “Save” button.
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 18
- 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 5: As step 5 in section 2.2.1, add and commit your work using 'git add' and 'git
commit -m <message>'.
- 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 “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
Page 6 of 18
- Step 5: As step 5 in section 2.2.1, add and commit your work using 'git add' and 'git
commit -m <message>'.
2.2.4 Write, compile, and run the following example:
- 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 “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
- Step 5: As step 5 in section 2.2.1, add and commit your work using 'git add' and 'git
commit -m <message>'.
2.2.5 Write a program to calculate sum, difference, product, and quotient of 2 double numbers which
are entered by users.
Notes
Page 7 of 18
- To convert from String to double, you can use
double num1 = Double.parseDouble(strNum1)
- Check the divisor of the division
- Don't forget to add and commit your work using ‘git add .’ and ‘git commit -m <message>’ command
After completing the code in section 2, you should push all of your changes, including assignment
2.2.1 to 2.2.6 to the master branch of the valid repository you have created.
Hint: You should use “git push origin <name of the branch that you want to push to the remote
repository>”, in this case is “master” , to push all of your works to the master branch.
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 be
compatible with later Java versions).
- 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 18
Figure 10. Java Conceptual Diagram
Page 10 of 18
5 Your first Java project
1. From the Eclipse install directory, run Eclipse IDE.
2. In the Eclipse IDE Launcher window, choose your workspace directory where you want to save the
project(s). If you want to use the chosen directory as the default, check the box. Then, click the
Launch button.
4. On the pop-up window, choose Java Project, then click Next > button. If you cannot find it, type
the filter text.
Page 11 of 18
5. On the New Java Project window, let the Project name be “JavaBasics”. Then, click the Finish
button.
7. Close the Welcome page; then the Java perspective shows up.
Page 12 of 18
6 Exercises
6.1 Write, compile and run the ChoosingOption program:
Note: We use the JavaBasics project for this exercise.
Step 1: Create a class.
- Choose File → New → Class
- On the pop-up window, set the Name same as the class name in the Figure 26, which is
“ChoosingOption”
Page 13 of 18
Figure 27. A New Class created
Step 2: Write the program. The source code is illustrated in Figure 28.
Page 14 of 18
Figure 30. Save and Launch
Step 4: Add, commit, and push all your work to the “‘master’ branch.
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.2 Write a program for input/output from keyboard
Note: We use the JavaBasics project for this exercise.
Step 1: Create a class.
- Choose File → New → Class
- On the pop-up window, set the Name as “InputFromKeyboard”
Step 2: Write the program. The source code is illustrated in Figure 32.
Step 3: Save and Launch.
- Method 1: Right-click on the InputFromKeyboard class →Run As →Java Application.
- Method 2: Click the button and choose the application as shown in the Figure 31
Page 15 of 18
Figure 32. InputFromKeyboard Application
6.3 Write a program to display a triangle with a height of n stars (*), n is entered by users.
E.g. n=5:
*
***
*****
*******
*********
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.
Page 16 of 18
- The user must enter a year in a non-negative number and enter all the digits. For instance,
the valid input 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, the year 1800 is not a leap year, yet the 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
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.
After completing all of your tasks, You should push all of your work you’ve done in this lab into a
“master” branch.
Page 17 of 18
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 18 of 18