Question

Write a JAVA program with methods that initializes an array with 20 random integers between 1...

Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing

1)The initialized array.

2)Every element at an even index.

3)Every even element.

4)All elements in reverse order.

Requirements (and hints):

1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and you should only have one method that prints an array. Think through how to structure the method header and how to call this method. You can reuse the logic for the actual printing of an array (from Lab11b) or use a textbook example. The difficulty in this lab is thinking through how the array is passed each time. Keep in mind your printArray method should be generic enough to print any integer array that is passed to it.

2. Each of the bullets above should be a separate method. Think through how to name your method, what should be in the method header, how to call each of these methods and what if anything should be passed and returned. Realize that each of your methods might need to call the printArray method to accomplish the printing of the array.

3. The original array must be retained and reused by each of the methods you build. For this reason, think through how you will retain a copy of the original array to be reused each time one of your methods has altered the original array. (Hint: you could use Arrays.copyOf)

4. The main method should be basically a series of calls to your methods.

5. NO keyboard input for this lab.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

public class ManipulateArray {

  

   static void printArray(int arr[]){

       for (int i = 0; i < arr.length; ++i)

           System.out.print(arr[i]+ " ");

       System.out.println();

   }

  

   static void printEvenIndex(int arr[]){

       for (int i = 0; i < arr.length; ++i) {

           if(i%2==0)

           System.out.print(arr[i]+ " ");

       }

       System.out.println();

   }

   static void printEvenNumbers(int arr[]){

       for (int i = 0; i < arr.length; ++i) {

           if(arr[i]%2==0)

           System.out.print(arr[i]+ " ");

       }

       System.out.println();

   }

  

   static void printReverseArray(int arr[]){

       for (int i = arr.length-1; i>=0; --i)

           System.out.print(arr[i]+ " ");

       System.out.println();

   }

   public static void main(String[] args) {

Random r = new Random();

       int arr[] = new int[20];

       for(int i=0; i<20;++i)

           arr[i] = r.nextInt(50) + 1;

       System.out.println("Original Array: ");

       printArray(arr);

      

       System.out.println("Even Index of Array: ");

       printEvenIndex(arr);

      

       System.out.println("Evn numbers of Array: ");

       printEvenNumbers(arr);

      

       System.out.println("reverse Array: ");

       printReverseArray(arr);

      

      

   }

}
===============================================
See Output

Console 3 <terminated> ManipulateArray [Java Application] /Library/Internet Plug-Ins/JavaAppl Original Array: 20 49 3 6 26 33?
Thanks, PLEASE UPVOTE if its helpful

Add a comment
Know the answer?
Add Answer to:
Write a JAVA program with methods that initializes an array with 20 random integers between 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main...

    The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main with a single generic method that produces the same output. Call the new file GenericMethodsTest.java. OverloadMethods.java: // Printing array elements using overloaded methods. public class OverloadedMethods { // method printArray to print Integer array public static void printArray(Integer[] inputArray) { // display array elements for (Integer element : inputArray) { System.out.printf("%s ", element); } System.out.printf("\n"); } // method printArray to print Double array public...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Write a program that initializes an array with ten random integers and then prints out the...

    Write a program that initializes an array with ten random integers and then prints out the following: Every element at an even index; Every even element All elements in reverse order; Only the first and last elements; The minimum and maximum element The sum of all elements The alternating sum of all elements, where the alternating sum contains all elements at even index added, and the elements at odd index subtracted. Please write comments above the piece of code that...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Write array methods that carry out the following tasks for an array of integers by completing...

    Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...

  • Please help me with this in C# language. Returning an array from a function The goal...

    Please help me with this in C# language. Returning an array from a function The goal for this exercise is to make sure that you return an array from a method (as a return value). Also: to give you more practice creating and using methods/functions. What you need to do for this exercise: In the starter project, add code to the Returning_An_Array class, so that the RunExercise method does the following: Declare an integer array variable, but DO NOT ALLOCATE...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1...

    Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1 - 100) and returns the maximum number, minimum number, average of all numbers, and prints a Bar Chart to show the number distribution (1-9, 10-19,20-29, …80-89, 90-100). Note; maximum number, minimum number, average number, and the Bar Chart must use implemented as separate methods in a separate class. Method call should pass an array as an argument. Methods should accept an array as an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT