Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program with an array that is intiliazied with test data. Use any primit

ID: 3695982 • Letter: W

Question

Write a program with an array that is intiliazied with test data. Use any primitive data type of your choice. The program should also have the following methods. getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array. getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array. getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array. getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array. Demonstrate each of the methods in the program Also you need to test the code with double, float and long arrays, for example with a double array as follows: double [] darray = { 11.0, 18.0, 21.0, 33.0, 53.0, 72.0 }; So you need overloaded methods for this and 2 other types of arrays as well.

Explanation / Answer

import java.util.Scanner;

public class VarnickPass11 {
public static void main(String[] args) {
final int ARRAY_SIZE = 5;
int[] numbers = new int[ARRAY_SIZE];
getValues(numbers);
getTotal(numbers);
getAverage(numbers);
getHighest(numbers);
getLowest(numbers);
}

public static int getTotal(int[] x) {
int total = 0;
for (int index = 0; index < x.length; index++) {
total += x[index];
}
System.out.println("The total is " + total);
return total;
}

public static double getAverage(int[] x) {
int total = 0;
for (int index = 0; index < x.length; index++) {
total += x[index];
}
double average = (total / 2.0);
System.out.println("The average is " + average);
return average;
}

public static int getHighest(int[] x) {
int highest = x[0];
for (int index = 1; index < x.length; index++) {
if (x[index] > highest)
highest = x[index];
}
System.out.println("The highest value is " + highest);
return highest;
}

public static int getLowest(int[] x) {
int lowest = x[0];
for (int index = 1; index < x.length; index++) {
if (x[index] < lowest)
lowest = x[index];
}
System.out.println("The lowest value is " + lowest);
return lowest;
}

private static void getValues(int[] array) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a series of " + array.length + " numbers.");

for (int index = 0; index < array.length; index++) {
System.out.print("Enter number " + (index + 1) + ": ");
array[index] = keyboard.nextInt();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote