0% found this document useful (0 votes)
1 views7 pages

Week 5 Lab Tutorial (Step-By-Step)

Uploaded by

is061270
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Week 5 Lab Tutorial (Step-By-Step)

Uploaded by

is061270
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Week 5 Lab Tutorial (Step-by-Step)

Course: BIC1224 – Object-Oriented Programming (Java)


Topic: Arrays (1D & 2D), Searching, Sorting, Basic Matrix Operations

Learning Outcomes (Week 5 Lab)


By the end of this lab, students will be able to:

● Declare, initialize, and traverse 1D arrays


● Take array input using loops
● Perform common array operations (sum, average, max, min)
● Implement linear search
● Implement bubble sort
● Use 2D arrays (matrix input/output)
● Perform matrix addition and basic analysis

Part A — Setup & Starter Template


Step 1: Create a New Project
1. Open your IDE.
2. Create a new Java project:
○ Project Name: BIC1224_Week5
○ Main Class: Main

Step 2: Use This Starter Template


import [Link];

public class Main {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Your code here

[Link]();
}
}
Part B — 1D Arrays (Step-by-Step)
Task 1: Declare and Input an Array
Step 3: Create an array of size N

1. Ask user for n


2. Create int[] arr = new int[n]
3. Input elements using loop

[Link]("Enter array size: ");


int n = [Link]();

int[] arr = new int[n];

for (int i = 0; i < n; i++) {


[Link]("Enter arr[" + i + "]: ");
arr[i] = [Link]();
}

✅ Skills: array declaration, input loop

Task 2: Display the Array


Step 4: Print all elements
[Link]("Array elements:");
for (int i = 0; i < n; i++) {
[Link](arr[i] + " ");
}
[Link]();

Task 3: Sum and Average


Step 5: Use accumulator
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
double avg = (double) sum / n;
[Link]("Sum = " + sum);
[Link]("Average = %.2f%n", avg);

Task 4: Find Maximum and Minimum


Step 6: Initialize with first element
int max = arr[0];
int min = arr[0];

for (int i = 1; i < n; i++) {


if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}

[Link]("Max = " + max);


[Link]("Min = " + min);

Part C — Searching (Linear Search)


Task 5: Linear Search
Step 7: Search a key and print index
1. Input key
2. Scan array
3. If found → print index
4. If not → show message

[Link]("Enter search key: ");


int key = [Link]();

int index = -1;


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
index = i;
break;
}
}

if (index != -1) {
[Link]("Found at index: " + index);
} else {
[Link]("Not found.");
}

✅ Skills: search + break

Part D — Sorting (Bubble Sort)


Task 6: Bubble Sort (Ascending)
Step 8: Sort array
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

Step 9: Print sorted array


[Link]("Sorted array (ascending):");
for (int v : arr) {
[Link](v + " ");
}
[Link]();

Part E — 2D Arrays (Matrices)


Task 7: Input and Display a 2D Array (Matrix)
Step 10: Take rows & columns
[Link]("Enter rows: ");
int r = [Link]();
[Link]("Enter cols: ");
int c = [Link]();

int[][] mat = new int[r][c];

Step 11: Input matrix values


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link]("mat[" + i + "][" + j + "]: ");
mat[i][j] = [Link]();
}
}

Step 12: Display matrix neatly


[Link]("Matrix:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link](mat[i][j] + "\t");
}
[Link]();
}

✅ Skills: nested loops + 2D array structure

Task 8: Matrix Addition


Step 13: Input two matrices and add them
Assume same size r x c.

int[][] A = new int[r][c];


int[][] B = new int[r][c];
int[][] S = new int[r][c];

[Link]("Enter Matrix A:");


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
A[i][j] = [Link]();
}
}

[Link]("Enter Matrix B:");


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
B[i][j] = [Link]();
}
}

for (int i = 0; i < r; i++) {


for (int j = 0; j < c; j++) {
S[i][j] = A[i][j] + B[i][j];
}
}

[Link]("Sum Matrix:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link](S[i][j] + "\t");
}
[Link]();
}

Exercises (Week 5)
Exercise 1 — Reverse an Array
Input N numbers, then print them in reverse order.

Hint: loop from n-1 down to 0.

Exercise 2 — Count Even and Odd Numbers


Given an array, count:

● total even
● total odd

Exercise 3 — Second Largest Element


Find the second largest number in an array.

Hint: track largest and secondLargest.

Exercise 4 — Remove Duplicates (Basic)


Input array and print only unique values.

Hint: Use nested loops or a temporary array.

Exercise 5 (Challenge) — Menu-Driven Array Program


Create a menu that repeats until exit:

1. Input array
2. Display array
3. Find sum and average
4. Find max and min
5. Search key
6. Sort ascending
7. Sort descending
8. Exit

You might also like