Computer Programming (Java) – Grade 11
Learning Activity Sheets
Quarter 2 Week 11
Republic Act 8293, section 176 states that: No copyright shall subsist in any
work of the Government of the Philippines. However, prior approval of the
government agency or office wherein the work is created shall be necessary for
exploitation of such work for profit. Such agency or office may, among other things,
impose as a condition the payment of royalties.
Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand
names, trademarks, etc.) included in this activity sheet are owned by their respective
copyright holders. Every effort has been exerted to locate and seek permission to
use these materials from their respective copyright owners. The publisher and
authors do not represent nor claim ownership over them.
Published by the Department of Education – Schools Division of Tacloban City
Schools Division Superintendent: Mariza S. Magan
Assistant Schools Division Superintendent: Edgar Y. Tenasas
Development Team of the Activity Sheet
Writers: Maneth B. Vivero
Leah Amor V. Felicilda
Evaluator: Almer H. Bugal
Management Team:
CID Chief: Mark Chester Anthony G. Tamayo
Division EPS of LRMS: Gretel Laura M. Cadiong
Department of Education - Region No. VIII – Schools Division Office of
Tacloban City
Office Address: Real St., Tacloban City
COMPUTER PROGRAMMING (JAVA) GRADE 11
LEARNING ACTIVITY SHEET
Quarter 2 Week 11
Objectives: (TLE_ICTJAVA11-12PCO-IIb-26)
Produce output/data using computer system
Process entered data using appropriate software commands
Understand Java Arrays
Create a Java program applying Array ( 1 and 2 Dimensional Array)
Materials:
Computer
AIDE Application/NetBeans
Let’s kick it off!
Are you familiar with this image? Take a look and observe.
Figure 1. Array of chocolates
Are you taking it?
What did you see in the image above?
Are they the same shapes?
Are they all chocolates?
What is array?
What is the advantage of using array in programming?
1|Q2 W11
Here’s how it is!
Java Arrays
As you observe in the figure 1 in the previous page, they are all chocolates in different
shapes and design yet they are stored in one box. The same also is the concepts of array. In
programming, the lesser the memory you consumed the better. Imagine, if you have 1000
variables but holds the same type of values, do you need to store them in one variable each?
Remember, every time you declare a variable, it occupies space in the memory. The more
memory you occupies the slower it compiled/processed. So, instead of storing them in one
container each, why not just store it in one box to minimize the consumption of space. And
this is the concept of array. Array is a special variable that can holds two or more values of
the same type.
Illustration of One Dimensional Array:
Array name: ArrayNum
0 1 2 3 4 5 6 7 Indexes
10 15 2 25 3 34 40 45
2 0
Values
In the array named ArrayNum, it holds 8 values and these are 10,
15,22,25,30,34,40,45 and its indexes is from 0-7. Later, we will discuss deeper what is index,
values and how array works.
Types of Arrays
1. One Dimensional Array
2. Multidimensional Array
One Dimensional Array
Syntax in declaring one dimensional array:
Data_type ArrayName[];
Ex.: int intArray[];
Syntax in constructing one dimensional array:
ArrayName = new Data_type[size];
Ex.: intArray = new int[5];
Syntax in declaring and constructing one dimensional array
Data_type Arrayname[] = new Data_type[size];
Ex.: int intArray[] = new int[5];
Size – identifies the number of values the array is capable to hold.
Declaring and initializing an array.
Ex.: int intArray[] = {2,5,7,8};
2|Q2 W11
In this example, the array name is intArray with a datatype of integer. It has an
array length of 4. 1st element is 2, 2nd element is 5, 3rd element is 7 and 4th element is 8.
Illustartion:
Array name: intArray
Size: 4
0 1 2 3 Index
2 5 7 8
Values
Note: Index always start with 0 and the last index is size – 1. So, in the example the size
is 4 so the last index is 3
Displaying the value of Array code snippet:
[Link](intArray[0]); 2
[Link](intArray[1]); 5
[Link](intArray[2]); Output 7
[Link](intArray[3]); 8
Imagine if the array has 100 values and you need to access it. Do you need to type the
line of code 100 times? Of course No! This is where the loop is applied.
Copy the following code and run it in your editor:
Example # 1:
import [Link].*; Output:
public class Main{ 2
public static void main(String[] args) 5
{ 7
int intArray[] = {2, 5, 7, 8}; 8
for(int i=0; i<4; i++){
[Link](intArray[i]);
}
}}
Example # 2:
import [Link].*; [Link](“The inputted values are:”);
public class Main{ for(int i=0; i<4; i++){
public static void main(String[] args){ [Link](intArray[i]);
Scanner sc = new Scanner([Link]); }
int intArray[] = new int[4]; }
[Link](“Enter 4 numbers:”); }
for(int i=0; i<4; i++){
intArray[i] = [Link]();
}
3|Q2 W11
Output:
Assume the inputted numbers are: 2,5,7,8.
Display
Enter 4 numbers:
2
5
7
8
The inputted values are:
2
5
7
8
Two Dimensional Array
Two Dimensional Array is an array of arrays. The data is stored in rows and columns.
Syntax in Declaring 2 Dimensional Array
Data_Type[][] Array_Name;
Example: int[][] ArrayInt; // The data type is integer and the arrayname is
Array_Name.
Creating 2 Dimensional Array
Data_type[][] Array_Name = new int[row_size][column_size];
Example: int[][] Learners = new int[2][3];
Note: Row_size – is the number elements of the array can be store in the row.
Column_size – is the number of elements the column of the array can
be store in the column.
Initializing 2 Dimensional Array
Syntax: Data_type[][] Array_name = {{element1, element2,..},{element1,element2,..}};
Example: int[][] Arr = {{1,2,3}, {8,7,9}};
In the example above, the datatype is integer and the array name is Arr. It can hold of
6 values for the row is 2 and columns is 3.
Illustration: 2 Dimensional Array
ArrayName: Arr
Indexes col 0 1 2
0 1 2 3
row
1 8 7 9
Arr[0][0]=1;
Arr[0][1]=2;
Arr[0][2]=3;
Arr[1][0]=8;
Arr[1][1]=7;
Arr[1][2]=9;
Copy the following code and run it in your editor:
4|Q2 W11
Example # 1:
import [Link].*; Output:
public class Main{ 2
public static void main(String[] args) 5
{ 7
int intArray[] []= {{2, 5, 7},{ 8,10,12}}; 8
for(int r=0; r<2; r++){ 10
for(int c=0; c<3; c++){ 12
[Link](intArray[r][c]);
}
}
}}
Now do it!
ACTIVITY 1-11
I. Instructions: Fill in the blank of the missing code in the program below. 2
points each.
1. public class Main {
2. public static void main(String[] args) ____1___
3. int StudentsNo[] __2____ {101,111,112,113,114,115};
4. __3____(int x = 0; x<__4___, x++){
5. [Link](“StudentsNo [“+x+”]: ”+___5_____);
6. }
7. }
8. }
II. Instructions: Read and Trace the program above. Write the output of the
given program. 10 points. Write your output in the box provided.
5|Q2 W11
Ace it!
ASSESSMENT
Name: __________________________________________ Date: _______________
Grade and Section: _______________________________ Score: ______________
PROGRAMMING
I. Instructions Read and Trace the program. Answer the given question based
on the program given. Write your answer to the space provided.
import [Link].*;
public class Main{
public static void main(String[] args)
{
int ArrNum[][]= {{4,12,15,20},{ 21,22,30,40},{10,20, 20,15}};
[Link](“The value of the array are: ”]);
for(int r=0; r<3; r++){
for(int c=0; c<4; c++){
[Link](intArray[r][c]);
}
}
}}
______________1. What type of Array used in the program?
______________2. What is the name of the array?
______________3. How many values the array able to hold?
______________4. What type of loop used in the program?
______________ 5. What is the value of ArrNum[2][3]?
______________ 6. How many rows are there?
______________ 7. What is the data type of the array?
______________ 8. What is the value of variable r, when the loop stops?
______________ 9. What is the value of variable c, when the loop stops?
______________ 10. What index the value 30 is found?
6|Q2 W11
II. OUTPUT
Instructions Write the output of the program in test I. Write your answer in
the box provided.
Congratulations! You made it! You just finished Module 11.
7|Q2 W11
1. [Link]
8|Q2 W11