Question

Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as...

Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows:

int arrayMax(int[] arr) determines and returns the largest value within an array
int arrayMin(int[] arr) determines and returns the smallest value within an array
void arrayReverse(int[] arr) reverses the array (for example: 7 8 1 2 becomes 2 1 8 7) 
void arraySquared(int[] arr) changes every value within the array to value²

Test your methods by creating an array of length 5 within your main() method and filling it with random integers between 1 and 1000. Your program should then invoke the methods in the order listed above to display the original array, the largest number in the array, the smallest number in the array, the reversed array, and the square of each value in the array. Your main() method should invoke each method exactly once, with each invocation use the original array (modified by a previous invocation of a method, if the array was modified by a method) as the actual parameter. All methods must be designed to handle arrays with any number of elements. Use only one array in your program. All printing must be done by the main() method. Document your code and organize the output using appropriate formatting techniques.

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

Here is code:

import java.util.Random;
public
class HelloWorld {

public
static void main(String[] args)
{

int arrayLength = 5;
int[] array = new int[arrayLength];
Random rand = new Random();
// creating random numbers
for (int i = 0; i < arrayLength; i++) {
array[i] = rand.nextInt(1000);
}
// printing the numbers
System.out.print("Elements in array : ");
for (int i = 0; i < arrayLength; i++) {
System.out.print(array[i] + " ");
}
// max element in array
System.out.print("\nMaximum Elements in array : " + arrayMax(array));
// min element in array
System.out.print("\nMinimum Elements in array : " + arrayMin(array));
// reversing the elements
arrayReverse(array);
System.out.print("\nReversing Elements in array : ");
for (int i = 0; i < arrayLength; i++) {
System.out.print(array[i] + " ");
}
// squaring the element in array
arraySquared(array);
System.out.print("\nSquaring of Elements in array : ");
for (int i = 0; i < arrayLength; i++) {
System.out.print(array[i] + " ");
}
}
// get max element
public
static int arrayMax(int[] arr)
{
int max = arr[0];
// loops from 0 to arr length
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
// get min element
public
static int arrayMin(int[] arr)
{
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min)
min = arr[i];
}
return min;
}
// get reverse element
public
static void arrayReverse(int[] arr)
{

int[] reversedArray = new int[arr.length];

for (int i = 0; i < arr.length / 2; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
// get square element
public
static void arraySquared(int[] arr)
{
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * arr[i];
}
}
}

output:

Elements in array 437 459 121 357 164 Maximum Elements in array 459 Minimum Elements in array 121 Reversing Elements in array

Add a comment
Know the answer?
Add Answer to:
Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as...
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
  • Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int...

    Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax (int [ ] arr) determines and returns the maximum value within an array int arrayMin (int [ ] arr) determines and returns the minimum value within an array void arraySquared (int [] arr) changes every value within the array to value2 void arrayReverse (int [ ] arr) reverses the array (for example: 7 8 12 becomes 2 18 7) Test your methods by...

  • Design and implement a Java program (name it ArrayMethods ), that defines 4 methods as follows:...

    Design and implement a Java program (name it ArrayMethods ), that defines 4 methods as follows: int arrayMax (int [ ] arr) determines and returns the ma ximum value within an array int arrayMin (int [ ] arr) determines and returns the minimum value within an array void arraySquared (int [ ] arr) changes every value within the array to value

  • Python please! Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods...

    Python please! Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7 8 9 becomes 9 8 7 ) The program main method creates a single-dimensional array of length...

  • C++ ArrayMethods Program

    Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an arrayint arrayMin(int[] arr) returns the minimum value in an arrayvoid arraySquared(int[] arr) changes every value in the array to its square (value²)void arrayReverse(int[] arr) reverses the array (for example: array storing 7   8   9  becomes 9   8   7 ) The program main method creates a single-dimensional array of length 5 elements and initialize it with random...

  • Please do in Java as simply as possible :) Exercise #3: Design and implement a program...

    Please do in Java as simply as possible :) Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7   8   9 becomes 9   8   7 ) The program main...

  • C++ Arrays & Methods

     #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the same order. Otherwise, it...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...

  • In Java code Exercise #1: (Java) Design and implement a program (name it MinMaxAvg) that defines...

    In Java code Exercise #1: (Java) Design and implement a program (name it MinMaxAvg) that defines three methods as follows: Method max (int x, int y, int z) returns the maximum value of three integer values. Method min (int X, int y, int z) returns the minimum value of three integer values. Method average (int x, int y, int z) returns the average of three integer values. In the main method, test all three methods with different input value read...

  • C++ Single Dimensional Arrays

    Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.  The program...

  • I need this in JAVA please: Design and implement a program (name it MinMaxAvg) that defines...

    I need this in JAVA please: Design and implement a program (name it MinMaxAvg) that defines three methods as follows: Method max (int x, int y, int z) returns the maximum value of three integer values. Method min (int X, int y, int z) returns the minimum value of three integer values. Method average (int x, int y, int z) returns the average of three integer values. In the main method, test all three methods with different input value read...

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