Chapter 16 Exercises
Exercise 1. Traverse an array of integers and print each number on a new line.
Solution:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
}
}
Exercise 2. Traverse a String array and print each string on a new line.
Solution:
public class Main {
public static void main(String[] args) {
String[] words = {"Hello", "World", "Java", "Programming"};
for (String word : words) {
System.out.println(word);
}
}
}
Exercise 3. Add all elements of an integer array and print the result.
Solution:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
sum += number;
}
System.out.println("Sum: " + sum);
}
}
Exercise 4. Find the maximum value in an integer array and print the result.
Solution:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
System.out.println("Maximum: " + max);
}
}
Exercise 5. Count how many times a specific number appears in an integer array.
Solution:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 2, 4, 2, 5};
int numberToFind = 2;
int counter = 0;
for (int number : numbers) {
if (number == numberToFind) {
counter++;
}
}
System.out.println("The number " + numberToFind + " appears " + counter + " times.");
}
}
Exercise 6. Print the positions of a specific number in an integer array.
Solution:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 2, 4, 2, 5};
int numberToFind = 2;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == numberToFind) {
System.out.println("The number " + numberToFind + " is at position " + i);
}
}
}
}
Exercise 7: Create an array of integers and calculate the average of its elements.
Solution:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
sum += number;
}
double average = (double) sum / numbers.length;
System.out.println("Average: " + average);
}
}
Exercise 8. Traverse a string array and convert each string to uppercase, then print them.
Solution:
public class Main {
public static void main(String[] args) {
String[] words = {"hello", "world", "java", "programming"};
for (String word : words) {
System.out.println(word.toUpperCase());
}
}
}
As we have seen, to convert text to uppercase in Java, we use the toUpperCase() method from the String class. This method takes a text string and returns a new string with all letters converted to uppercase. Here I explain how to do it in a simple way. Let’s look at another simple example:
String text = “hello world”;
String textInUppercase = text.toUpperCase();
System.out.println(textInUppercase);
Exercise 9. Traverse an array of integers and multiply each element by 2, printing the results.
Solution:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number * 2);
}
}
}
Exercise 10. Traverse a String array and print only even-numbered elements.
Solución:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int evenCounter = 0;
for (int number : numbers) {
if (number % 2 == 0) {
evenCounter++;
}
}
System.out.println("Number of even elements: " + evenCounter);
}
}