Question

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 method creates a single-dimensional array of length 5 elements and initialize it with random integers between 1 and 100. The program displays the original array, then calls each of the above methods and displays their results as shown below. Document your code and organized your output following these sample runs.

Sample run 1:

Original array: 3, 5, 2, 6, 1

Max value:      6

Min value:      1

Squared array: 9, 25, 4, 36, 1

Reversed array: 1, 36, 4, 25, 9

Sample run 2:

Original array: 3, 2, 3, 7, 2

Max value:      7

Min value:      2

Squared array: 9, 4, 9, 49, 4

Reversed array: 4, 49, 9, 4, 9

Sample run 3:

Original array: 2, 2, 2, 2, 2

Max value:      2

Min value:      2

Squared array: 4, 4, 4, 4, 4

Reversed array: 4, 4, 4, 4, 4

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

Source code:

import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the length of array");
int n=s.nextInt();
int[] array=new int[n];
System.out.println("Enter values into array");
for(int i=0;i<5;i++)
{
array[i]=s.nextInt();//assigning values to array
}
System.out.print("Orginal array:");
for(int i=0;i<5;i++)
{
System.out.print(array[i]+",");
}
System.out.println();
System.out.println("Max value: "+arrayMax(array));
//print max value returned from method
System.out.println("Min value: "+arrayMin(array));
//print min value returned from method
arraySquared(array);
//method to get the squars passing array as argument
arrayReversed(array);
}
public static int arrayMax(int[] array)
{
int max=array[0];
for(int i=0;i<5;i++)
{
if(array[i]>max)
//condition:if element greater than max then max=new element
{
max=array[i];
}
}
return max;//send max element to main
}
public static int arrayMin(int[] array)
{
int min=array[0];
for(int i=0;i<5;i++)
{
if(array[i]<min)
//if element less than min then assign element to min
{
min=array[i];
}
}
return min;//return min to main
}
public static void arraySquared(int[] array)
{
System.out.print("Squared array:");
int[] array1=new int[5];//new array to store result elements
for(int i=0;i<5;i++)
{
array1[i]=array[i]*array[i];//square and store in new array
System.out.print(array1[i]+",");
}
System.out.println();
}
public static void arrayReversed(int[] array)
{
System.out.print("Reversed array:");
for(int i=4;i>=0;i--)//loops goes from backwards to print reverse
{
System.out.print(array[i]+",");
}
System.out.println();
}
}

output:

//Please comment if you have any doubt

Add a comment
Know the answer?
Add Answer to:
Please do in Java as simply as possible :) Exercise #3: Design and implement a program...
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
  • 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...

  • 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...

  • 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...

  • 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

  • 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...

  • 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...

  • 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...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • 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...

  • PLEASE DO THE PSEUDOCODE FOR THE PROGRAM BELOW Program 3: Design (pseudocode) and implement (source code)...

    PLEASE DO THE PSEUDOCODE FOR THE PROGRAM BELOW Program 3: Design (pseudocode) and implement (source code) a program (name it DistinctValues) to display only district values in an array. The program main method defines a single-dimensional array of size 10 elements and prompts the user to enter 10 integers to initialize the array. The main method then calls method getValues() that takes an integer array and returns another single-dimensional array containing only distinct values in the original (passed) array. Document...

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
Active Questions
ADVERTISEMENT