Java Programming Labs

Master arrays, methods, and exception handling with hands-on coding challenges.

Java Intermediate Concepts - Module 2

Build on your fundamentals with arrays, reusable methods, and error handling.

Lab 4: Arrays & Iteration
Intermediate
Coding Challenge
Your Task: Create a program that works with an array of integers - declare, initialize, iterate, and calculate statistics.

Detailed Requirements:
1. Declare an int array named "numbers" with at least 5 values. Arrays store multiple values of the same type.
   Syntax: int[] numbers = {10, 25, 8, 42, 15};

2. Use a for loop to print all elements with their index positions.
   Access elements by index: numbers[0] is the first element
   Array length: numbers.length gives you the size

3. Calculate and print the sum of all elements using a loop.

4. Find and print the maximum value in the array by comparing each element.

Key Concepts:
� Arrays are zero-indexed (first element is at index 0)
numbers.length returns the array size (no parentheses!)
� Use for (int i = 0; i < numbers.length; i++) to iterate

Expected Output:
Element at index 0: 10 Element at index 1: 25 Element at index 2: 8 Element at index 3: 42 Element at index 4: 15 Sum: 100 Maximum: 42

Requirements Checklist

Declare an int array with at least 5 values
Use a for loop to iterate through the array
Print each element with its index
Calculate the sum of all elements
Find and print the maximum value
Code compiles and runs correctly
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Array declaration: int[] numbers = {10, 25, 8, 42, 15};
� Array length: numbers.length (no parentheses)
� Access element: numbers[i] where i is the index
� Sum pattern: sum += numbers[i]; inside loop
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 5: Methods & Functions
Intermediate
Coding Challenge
Your Task: Create reusable methods to perform calculations. Methods break code into modular, reusable pieces.

Detailed Requirements:
1. Create a method "add" that takes two int parameters and returns their sum.
   Syntax: public static int add(int a, int b) { return a + b; }

2. Create a method "isEven" that takes an int and returns a boolean (true if even, false if odd).
   Use modulo: return number % 2 == 0;

3. Create a method "greet" that takes a String name and returns a greeting message.
   Returns: "Hello, " + name + "!"

4. Call all three methods from main() and print the results.

Method Anatomy:
public static - accessible from main, belongs to class
int/boolean/String - return type (what the method gives back)
� Parameters in parentheses - inputs the method needs
return - sends a value back to the caller

Expected Output:
5 + 3 = 8 Is 4 even? true Is 7 even? false Hello, Alice!

Requirements Checklist

Create method "add" with two int parameters returning int
Create method "isEven" returning boolean
Create method "greet" with String parameter
Use return statements in all methods
Call methods from main() and print results
Code compiles and runs correctly
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Method with return: public static int add(int a, int b) { return a + b; }
� Boolean method: public static boolean isEven(int n) { return n % 2 == 0; }
� Call method: int result = add(5, 3);
� Methods must be outside main() but inside the class
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 6: Exception Handling
Intermediate
Coding Challenge
Your Task: Handle runtime errors gracefully using try-catch blocks. Exceptions prevent program crashes from invalid operations.

Detailed Requirements:
1. Create a try-catch block to handle division by zero (ArithmeticException).
   Division by zero crashes the program without exception handling.

2. Handle ArrayIndexOutOfBoundsException when accessing invalid array index.
   Trying to access array[10] on a 5-element array throws this exception.

3. Handle NumberFormatException when parsing invalid string to number.
   Integer.parseInt("abc") throws this exception.

4. Use finally block to execute code regardless of whether exception occurred.

Try-Catch Structure:
try { // risky code that might throw exception } catch (ExceptionType e) { // handle the error } finally { // always runs }

Expected Output:
Attempting division by zero... Error: Cannot divide by zero! Attempting invalid array access... Error: Array index out of bounds! Attempting to parse invalid number... Error: Invalid number format! Finally block executed - cleanup complete.

Requirements Checklist

Use try-catch for ArithmeticException (division by zero)
Use try-catch for ArrayIndexOutOfBoundsException
Use try-catch for NumberFormatException
Include at least one finally block
Print error messages in catch blocks
Code compiles and handles all exceptions
Output
// Click "Run Code" to compile and execute
Hints & Tips
� Division by zero: catch (ArithmeticException e)
� Invalid array index: catch (ArrayIndexOutOfBoundsException e)
� Parse error: catch (NumberFormatException e)
� Finally always runs: finally { /* cleanup */ }
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below