Computer Programming 2
1
Java Language Fundamentals
Week002 – Java Language Fundamentals
Laboratory Exercise 002
Objective/s:
At the end of this activity, you should be able to:
Write a program based on the knowledge of different parts of a java program.
Apply the coding standards for the different parts of the program.
Write a program with proper comments and documentation.
What to Prepare for the Activity:
Java Standard Edition (SE) Development Kit 8 (JDK8)
A text editor, an example of this is Notepad or Notepad++.
Procedure:
1. Write a simple java program with the following specification:
a. Create a java file named LaboratoryExercise2
b. Create a variable with ‘int’ data types without initializing it.
c. Print the variable using System.out.println()
d. Save and compile.
Test Stem / Question Choices
A: LaboratoryExercise2.java:5: error:
1: What did the compiler say?
variable a might not have been
initialized
B: nothing
C: the initial value of the an int variable which is zero
D: printed the name of the variable which is ‘a’
2: What is the output when you run the B: The program will not run because of the compiler
program? error
B: nothing
C: 0
D: Hello World!
Assessments
2. Open the file that you have created and replace the entire codes with this:
public class LaboratoryExercise2 {
public static void main(String[] args) {
System.out.println("\"Hello\" world!");
}
}
a. Save the file.
b. Compile and run the program.
Test Stem / Question Choices
A: LaboratoryExercise2.java:5: error:
3: What did the compiler say? variable a might not have been
initialized
B: nothing
C: Compiler warning
D: Displays Hello World!
4: What is the output when you run the B: nothing
program?
B: Hello World!
C: “Hello World!”
D: “Hello” World!
3. Replace the entire codes with this:
public class LaboratoryExercise2 {
public static void main(String[] args) {
int a = "Hello world!";
System.out.println("a");
}
}
a. Save the file.
b. Compile and run the program.
Test Stem / Question Choices
A: LaboratoryExercise2.java:5: error:
5: What did the compiler say? variable a might not have been
initialized
B: nothing
Computer Programming 2
3
Java Language Fundamentals
C: Compiler warning
D: Displays Hello World!
6: What is the output when you run the B: nothing
program?
B: Hello World!
C: a
D: “Hello” World!
4. Replace the System.out.println("a"); with this System.out.println(A);.
a. Save the file.
b. Compile and run the program.
Test Stem / Question Choices
7: What did the compiler say? A: Compiler Error
B: nothing
C: Compiler warning
D: Displays Hello World!
8: What is the output when you run the B: nothing
program?
B: Hello World!
C: A
D: The program did not run.
5. Replace the System.out.println(A); with this System.out.println(a);.
a. Save the file.
b. Compile and run the program.
Test Stem / Question Choices
9: What did the compiler say? A: Compiler Error
B: nothing
Assessments
C: Compiler warning
D: Displays Hello World!
10: What is the output when you run the B: nothing
program?
B: Hello World!
C: a
D: The program did not run.
Only nos. 6 and 7 will be uploaded to the link provided on the course page.
The filename for No. 6 should be Lab2_6 and Lab2_7 for no. 7.
Only Java files will be accepted.
Since there are multiple Java files, save them into one folder. The folder name should be
in this format:
Lab002_<lastname_firstname>
Example: Lab002_Blanco_Maria
Compress the folder into .rar or .zip format before uploading.
6. Write a Java program that will display the text below including the new line.
"I am writing a program that will display "text" with escape characters.
At the beginning of this sentence it has tab escape character.
There are two new lines before this sentence and you also have to
include 'single quote' and \back slash\ characters in this sentence."
7. From the given the table below, write a program that display the names together with the
values.
The variables should have valid names (should follow coding guidelines)
Correct initial value and data types that fit on the initial value.
Write comments for each statement. The comment should clearly describe the
statement.
Name Initial Value
Gross sales 0.0
Age 1
Computer Programming 2
5
Java Language Fundamentals
Final false
Location 0x11
Old File 067
Default ‘a’
Name “noname”
The expected output is:
Gross sales = 0.0
Age = 1
Final = false
Location = 17
Old File = 55
Default = a
Name = noname
Assessments