Master arrays, methods, and exception handling with hands-on coding challenges.
Build on your fundamentals with arrays, reusable methods, and error handling.
int[] numbers = {10, 25, 8, 42, 15};numbers[0] is the first elementnumbers.length gives you the sizenumbers.length returns the array size (no parentheses!)for (int i = 0; i < numbers.length; i++) to iterateElement 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
int[] numbers = {10, 25, 8, 42, 15};numbers.length (no parentheses)numbers[i] where i is the indexsum += numbers[i]; inside loopReview feedback below
public static int add(int a, int b) { return a + b; }return number % 2 == 0;"Hello, " + name + "!"public static - accessible from main, belongs to classint/boolean/String - return type (what the method gives back)return - sends a value back to the caller5 + 3 = 8
Is 4 even? true
Is 7 even? false
Hello, Alice!
public static int add(int a, int b) { return a + b; }public static boolean isEven(int n) { return n % 2 == 0; }int result = add(5, 3);Review feedback below
array[10] on a 5-element array throws this exception.Integer.parseInt("abc") throws this exception.try {
// risky code that might throw exception
} catch (ExceptionType e) {
// handle the error
} finally {
// always runs
}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.
catch (ArithmeticException e)catch (ArrayIndexOutOfBoundsException e)catch (NumberFormatException e)finally { /* cleanup */ }Review feedback below