
This comprehensive guide covers practical Java programming exercises focused on arrays. It includes creating arrays, calculating sums and averages, checking element presence, matrix addition, reversing arrays, finding maximum and minimum elements, and verifying if an array is sorted. Each problem is explained with code logic and implementation tips to help learners master array manipulation in Java.
In this tutorial, we explore practical Java programming exercises centered around arrays, a fundamental data structure. These exercises help solidify your understanding of array manipulation, iteration, and common algorithms used in Java programming.
This guide is based on a series of practice problems designed to apply concepts from Chapter 6 of a Java course. Each problem is tackled with detailed explanations and code snippets to help you grasp the logic and implementation.
Create an array of five float values and calculate the sum of these values.
float[] marks = {45.7f, 67.28f, 63.44f, 100.0f, 50.0f};
float sum = 0;
for (float mark : marks) {
sum += mark;
}
System.out.println("Sum of marks: " + sum);
Write a program to check whether a specific float value is present in the array.
float target = 45.7f;
boolean isPresent = false;
for (float mark : marks) {
if (mark == target) {
isPresent = true;
break;
}
}
if (isPresent) {
System.out.println(target + " is present in the array.");
} else {
System.out.println(target + " is not present in the array.");
}
Calculate the average of all marks stored in the array.
float sum = 0;
for (int i = 0; i < marks.length; i++) {
sum += marks[i];
}
float average = sum / marks.length;
System.out.println("Average marks: " + average);
Create two 2x3 matrices and add them.
int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}};
int[][] matrix2 = {{4, 5, 6}, {7, 8, 9}};
int[][] result = new int[2][3];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print result
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
Write a program to reverse the elements of an array.
int[] arr = {1, 2, 3, 4, 5};
int n = arr.length;
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
// Print reversed array
for (int num : arr) {
System.out.print(num + " ");
}
Find the maximum value in an integer array.
int[] numbers = {10, 20, 5, 30, 25};
int max = numbers[0];
for (int num : numbers) {
if (num > max) {
max = num;
}
}
System.out.println("Maximum element: " + max);
Check whether the array is sorted in ascending order.
int[] arr = {1, 2, 3, 4, 5};
boolean isSorted = true;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
isSorted = false;
break;
}
}
if (isSorted) {
System.out.println("Array is sorted.");
} else {
System.out.println("Array is not sorted.");
}
These practice problems cover essential array operations in Java, including creation, traversal, searching, aggregation, matrix operations, and sorting checks. Mastering these will build a strong foundation for more advanced Java programming concepts.
Remember, repetition and practice are key. Revisit these problems multiple times if needed, and try to implement variations to deepen your understanding.
Happy coding!
Paste a YouTube link and let Magica create the key takeaways.
Summarize another video