Laboratory Worksheet: Arrays and Algorithm Operations
(Java)
Name: ________________________________
Section: ______________________________
Date: ________________________________
Objectives
• Declare and initialize arrays in Java
• Perform traversal, searching, and updating
• Work with one-dimensional and multi-dimensional arrays
• Analyze time complexity of basic operations
Requirements
• Java JDK installed
• IDE (VS Code / IntelliJ / NetBeans)
• Basic knowledge of Java syntax
Part 1: Array Basics (1D Array)
Task:
• Declare an array of 10 integers
• Accept user input for all elements
• Display all elements using a loop
import [Link];
public class ArrayBasics {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[] arr = new int[10];
for(int i = 0; i < [Link]; i++) {
[Link]("Enter value " + i + ": ");
arr[i] = [Link]();
}
[Link]("Array elements:");
for(int i = 0; i < [Link]; i++) {
[Link](arr[i]);
}
}
}
Part 2: Traversal and Analysis
Task:
• Compute the sum and average
• Find the maximum and minimum values
Guide Questions:
• How many iterations does your loop perform?
• What is the time complexity? ____________________
Part 3: Searching Algorithms
Task:
• Implement Linear Search
• Implement Binary Search (array must be sorted)
int target = 50;
boolean found = false;
for(int i = 0; i < [Link]; i++) {
if(arr[i] == target) {
[Link]("Found at index: " + i);
found = true;
break;
}
}
if(!found) {
[Link]("Not found");
}
Part 4: Updating Elements
Task:
• Ask the user for an index
• Update the value at that index
[Link]("Enter index to update: ");
int index = [Link]();
[Link]("Enter new value: ");
arr[index] = [Link]();
Part 5: Multi-Dimensional Arrays (2D)
Task:
• Create a 3x3 matrix
• Input values
• Display in matrix format
int[][] matrix = new int[3][3];
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
matrix[i][j] = [Link]();
}
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
Part 6: String Arrays
Task:
• Store 5 student names
• Display all names
• Search for a specific name
Part 7: Challenge Problem
Task:
• Reverse an array
• Display original vs reversed
Algorithm Analysis Questions
• Traversal Time Complexity: ____________________
• Linear Search Time Complexity: ________________
• Binary Search Time Complexity: ________________
• Which algorithm is more efficient and why?
Reflection
What did you learn from this activity?
______________________________________________
______________________________________________
______________________________________________
Which part was challenging and why?
______________________________________________
______________________________________________
______________________________________________