This notebook contains interactive exercises. Edit the code cells and run them to check your solutions.
Exercise 1 — Control Flow
Task: Complete the function checkAge(int age) that returns "Majeur" if age >= 18, otherwise "Mineur".
Then test it with ages 15, 18, and 25.
// Write your checkAge function hereStringcheckAge(int age){// TODO: implement thisreturn"";}// Test casesSystem.out.println("Age 15: "+checkAge(15));System.out.println("Age 18: "+checkAge(18));System.out.println("Age 25: "+checkAge(25));
Age 15:
Age 18:
Age 25:
Expected output:
Age 15: Mineur
Age 18: Majeur
Age 25: Majeur
Exercise 2 — Arrays & Methods
Task: Write a method sumArray(int[] arr) that returns the sum of all elements in the array.
Test it with the array {10, 20, 30, 40, 50}.
// Write your sumArray function hereintsumArray(int[] arr){// TODO: implement thisreturn0;}// Test caseint[] numbers ={10,20,30,40,50};System.out.println("Sum: "+sumArray(numbers));
Sum: 0
Expected output:
Sum: 150
Solutions
Run the cells below to see the solutions (after attempting the exercises yourself!).
intsumArray(int[] arr){int sum =0;for(int value : arr){ sum += value;}return sum;}// Test caseint[] numbers ={10,20,30,40,50};System.out.println("Sum: "+sumArray(numbers));