008-005-008
Array Modifier Program
Hard
Problem Description
array Modifier Program
In this problem, you will create a program that passes an array {5, 10, 15} to a doubleValues() method, doubles each element, and displays the array before and after modification to standard output.
Learning Objective: Understand that modifying reference type arguments (arrays) inside a method affects the caller
Overview
Create a program that passes an array to a method and modifies the array elements inside the method. Learn how changes to reference type arguments are reflected in the caller.
Specifications
Define a
doubleValues()method:- Receives an int array as an argument
- Doubles each element of the array
In Main class:
- Create an array with
{5, 10, 15} - Output the array before modification as
Before: 5, 10, 15 - Call
doubleValues() - Output the array after modification as
After: 10, 20, 30
- Create an array with
Output Format
Before: 5, 10, 15
After: 10, 20, 30
Hints
- Modifying reference type arguments changes the caller's array too
- Modify array element: numbers[i] = numbers[i] * 2;
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up