Answer to Question #323984 in Java | JSP | JSF for stephanie

Question #323984

Write a program that declares and initializes an array of 7 integers. The program uses these static methods to perform different operations on the array:

void printArray(int[] arr) – takes the array as parameter and prints the array in horizontal order

int sumOfArray(int[] arr) – takes the array as parameter and returns the sum of the array

int getHighest(int[] arr) – takes the array as parameter and returns the highest value in the array

int resetArray(int[] arr) – takes the array as parameter and resets the value of each element to 0


Sample output:

Here’s the array: 

3 1 3 2 1 9 1

Sum: 20

Highest: 9

Here’s the array: 

0 0 0 0 0 0 0




1
Expert's answer
2022-04-06T13:55:31-0400


public class App {


	/**
	 * takes the array as parameter and prints the array in horizontal order
	 * 
	 * @param args
	 */
	static void printArray(int[] arr) {
		System.out.println("Here's the array:");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}


	/***
	 * takes the array as parameter and returns the sum of the array
	 * 
	 * @param arr
	 * @return
	 */
	static int sumOfArray(int[] arr) {
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
		}
		return sum;
	}


	/***
	 * takes the array as parameter and returns the highest value in the array
	 * 
	 * @param arr
	 * @return
	 */
	static int getHighest(int[] arr) {
		int highest = arr[0];
		for (int i = 1; i < arr.length; i++) {
			if (arr[i] > highest) {
				highest = arr[i];
			}
		}
		return highest;
	}


	/**
	 * takes the array as parameter and resets the value of each element to 0
	 * 
	 * @param arr
	 * @return
	 */
	static void resetArray(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			arr[i] = 0;
		}
	}


	public static void main(String[] args) {
		int[] arr = { 3, 1, 3, 2, 1, 9, 1 };
		printArray(arr);
		System.out.println("Sum: " + sumOfArray(arr));
		System.out.println("Highest: " + getHighest(arr));
		resetArray(arr);
		printArray(arr);
	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS