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.
In this interactive session, we'll practice implementing various types of loops in Java:
// 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();
}
}
Join us to explore array manipulation techniques and common array algorithms:
// Exercise 1: Find the maximum value in an array
int findMax(int[] array) {
if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty");
}
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
// Exercise 2: Reverse an array in-place
void reverseArray(int[] array) {
int left = 0;
int right = array.length - 1;
while (left < right) {
// Swap elements
int temp = array[left];
array[left] = array[right];
array[right] = temp;
// Move towards the center
left++;
right--;
}
}
Learn to apply the systematic UPER framework to solve programming challenges:
Problem: Given an array of integers, find all pairs of numbers that sum to a specific target.
// Approach 1: Nested loops (O(n²))
// For each number, check if (target - number) exists in the array
// Approach 2: Using a HashSet (O(n))
// Use a set to track seen numbers and check if (target - current) exists
List findPairs(int[] numbers, int target) {
List pairs = new ArrayList<>();
Set seen = new HashSet<>();
for (int num : numbers) {
int complement = target - num;
if (seen.contains(complement)) {
pairs.add(new int[]{num, complement});
}
seen.add(num);
}
return pairs;
}
Explore Java's pass-by-value mechanism and understand its implications:
// Exercise 1: Comparing primitive and reference parameter behavior
void demonstratePassByValue() {
// Primitives
int x = 10;
System.out.println("Original x: " + x);
modifyPrimitive(x);
System.out.println("After method call, x: " + x); // x is still 10
// References
Person person = new Person("Alice");
System.out.println("Original name: " + person.name);
modifyObject(person);
System.out.println("After modifyObject: " + person.name); // name is changed
replacePerson(person);
System.out.println("After replacePerson: " + person.name); // still the modified name
}
void modifyPrimitive(int n) {
n = n + 5; // Only affects the local copy
System.out.println("Inside modifyPrimitive: " + n);
}
void modifyObject(Person p) {
p.name = p.name + " (modified)"; // Modifies the actual object
}
void replacePerson(Person p) {
p = new Person("Bob"); // Only reassigns the local reference
System.out.println("Inside replacePerson: " + p.name);
}
Need to schedule a code-along session? Have questions? Contact the Backend Development team: