Code-Alongs
Overview
Join us for interactive coding sessions where we'll work through real-world examples and practice implementing loops, arrays, UPER problem-solving framework, and pass-by-value concepts in Java.
Available Code-Along Topics
- Code-Along 1: Loops - Practice implementing different types of loops and their applications
- Code-Along 2: Arrays - Learn array manipulation and common array algorithms
- Code-Along 3: UPER - Apply the UPER framework to solve coding problems
- Code-Along 4: Pass-by-value - Explore Java's pass-by-value mechanism and its implications
Code-Along 1: Loops
In this interactive session, we'll practice implementing various types of loops in Java:
- While loops for condition-based iteration
- For loops for count-based iteration
- Nested loops for complex patterns and multi-dimensional data
- Loop control statements (break and continue)
Sample Exercises
// Exercise 1: Sum all numbers from 1 to n using different loop types
int sumWithWhile(int n) {
int sum = 0;
int i = 1;
while (i <= n) {
sum += i;
i++;
}
return sum;
}
int sumWithFor(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
// Exercise 2: Print a pattern using nested loops
void printTriangle(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
Code-Along 2: Arrays
In this session, we'll explore array manipulation and common array algorithms:
- Creating and initializing arrays
- Accessing and modifying array elements
- Iterating through arrays
- Common array operations (searching, sorting, filtering)
- Multi-dimensional arrays
Sample Exercises
// Exercise 1: Find the maximum value in an array
int findMax(int[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("Array cannot be empty");
}
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
// Exercise 2: Reverse an array in-place
void reverseArray(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
// Swap elements
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
// Move indices
left++;
right--;
}
}
Code-Along 3: UPER
Learn to apply the UPER (Understand, Plan, Execute, Reflect) problem-solving framework:
- Understand: Define the problem clearly and identify constraints
- Plan: Develop a strategy and break the problem into smaller steps
- Execute: Implement your solution with careful coding
- Reflect: Evaluate your solution, optimize, and learn from the process
Sample Problem: Find Pairs with Sum
Given an array of integers and a target sum, find all pairs of integers in the array that add up to the target sum.
Understand
- Input: An array of integers and a target sum
- Output: All pairs of integers that add up to the target sum
- Constraints: The same element cannot be used twice in a pair
Plan
- Use a hash set to store visited elements
- For each element in the array, check if (target - current element) exists in the hash set
- If it does, we've found a pair
- Add the current element to the hash set for future checks
Execute
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
public class PairSum {
public static List findPairsWithSum(int[] arr, int targetSum) {
List result = new ArrayList<>();
HashSet visited = new HashSet<>();
for (int num : arr) {
int complement = targetSum - num;
if (visited.contains(complement)) {
result.add(new int[]{complement, num});
}
visited.add(num);
}
return result;
}
}
Reflect
- Time Complexity: O(n) where n is the number of elements in the array
- Space Complexity: O(n) for storing visited elements
- Optimization: Could handle duplicates better
Code-Along 4: Pass-by-value
Understand Java's pass-by-value mechanism and its implications:
- How Java handles primitive types vs. reference types
- Common misconceptions about pass-by-reference in Java
- Techniques for handling mutable objects
- Immutable objects and their benefits
Sample Code
// Example: Pass-by-value with primitives
public static void modifyValue(int x) {
x = x * 2; // This change is not reflected outside the method
System.out.println("Inside method: x = " + x);
}
// Example: Pass-by-value with reference types
public static void modifyArray(int[] arr) {
// Changes to the object's state ARE reflected outside the method
arr[0] = 100;
// But reassigning the reference is NOT reflected outside
arr = new int[]{200, 300}; // This change is not reflected outside
System.out.println("Inside method: arr[0] = " + arr[0]);
}
// Main method to demonstrate
public static void main(String[] args) {
int value = 10;
System.out.println("Before: value = " + value);
modifyValue(value);
System.out.println("After: value = " + value); // Still 10
int[] numbers = {1, 2, 3};
System.out.println("Before: numbers[0] = " + numbers[0]);
modifyArray(numbers);
System.out.println("After: numbers[0] = " + numbers[0]); // Now 100
}