Share
Explore

JAVA: Exercises for Arrays, Methods, and Arrays with Methods

Student Lab Workbook: Exercises for Arrays, Methods, and Arrays with Methods

Introduction

This student lab workbook is designed to help you learn and practice essential programming concepts, such as arrays, methods, and the combined use of arrays and methods. The exercises are designed to be both instructive and challenging to enhance your learning experience.

Table of Contents

Arrays

Exercise 1: Initializing and Accessing Elements of the Array
Exercise 2: Looping through Arrays
Exercise 3: Array Manipulation
Methods

Exercise 4: Creating and Calling Methods
Exercise 5: Methods with Parameters
Exercise 6: Methods with Return Values
Arrays with Methods

Exercise 7: Passing Arrays as Method Parameters
Exercise 8: Returning Arrays from Methods
Exercise 9: Array Manipulation using Methods

Part 1: Arrays

Exercise 1: Initializing and Accessing Elements

Create an integer array called numbers with 5 elements.
Initialize the array with the numbers 2, 4, 6, 8, and 10.
Print the third element of the array.
image.png

Exercise 2: Looping through Arrays

Create a string array called fruits with the elements "apple", "banana", "grape", "orange", and "kiwi".
Use a for loop to iterate through the array and print each element.
image.png

Exercise 3: Array Manipulation

Create an integer array called grades with the elements 85, 90, 78, 92, and 75.
Calculate and print the average grade.
Replace the lowest grade with a 100.
Calculate and print the new average grade.

Part 2: Methods

Exercise 4: Creating and Calling Methods

Create a method called greet that prints "Hello, World!".
Call the greet method.

Exercise 5: Methods with Parameters

Create a method called add that takes two integer parameters and prints their sum.
Call the add method with the numbers 5 and 8.

Exercise 6: Methods with Return Values

Create a method called multiply that takes two integer parameters and returns their product.
Call the multiply method with the numbers 3 and 7, and print the result.

Part 3: Arrays with Methods

Exercise 7: Passing Arrays as Method Parameters

Create a method called printArray that takes an integer array parameter and prints its elements.
Call the printArray method with the array from Exercise 1.

Exercise 8: Returning Arrays from Methods

Create a method called reverseArray that takes a string array parameter and returns a new array with the elements in reverse order.
Call the reverseArray method with the array from Exercise 2, and print the resulting reversed array.

Exercise 9: Array Manipulation using Methods

Create a method called findMax that takes an integer array parameter and returns the maximum value in the array.
Call the findMax method with the array from Exercise 3, and print the maximum value.
Create a method called findMin that takes an integer array parameter and returns the minimum value in the array.
Call the findMin method with the array from Exercise 3, and print the minimum value.

Conclusion

Congratulations on completing these exercises! You've gained valuable experience working with arrays, methods, and the combination of both. Keep practicing and refining your skills to become an even better programmer.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.