Java Program- Write two overload methods that return the average of an array wit
ID: 3565820 • Letter: J
Question
Java Program-
Write two overload methods that return the average of an array with the following headers:
public static int average(int[] array)
public static double average(double[] array)
Write a test program that prompts the user to enter ten double value, invokes this method, and displays the average value.
NOTE -
Note that you need to write 2 overloaded methods as specified. Design the main method of your program such that it allows the user to re-run the program with different sets of inputs. Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.
Explanation / Answer
import java.util.Scanner;
class Temps
{
public static int average(int[] array)
{
int sum = 0;
for (int i = 0; i < array.length; i++)
{
sum = sum + array[i];
}
return (sum / array.length);
}
public static double average(double[] array)
{
double sum = 0;
for (int i = 0; i < array.length; i++)
{
sum = sum + array[i];
}
return (sum / array.length);
}
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
//int array[] = new int[10];
double array1[] = new double[10];
String ans = "y";
do
{
for (int i = 0; i < array1.length; i++)
{
System.out.println("Enter grade " + (i + 1) + ": ");
array1[i] = Double.parseDouble(input.nextLine());
}
System.out.println("Average is: " + (Math.round(average(array1) * 100) / 100.0));
System.out.println("Do you want to enter another set of numbers?");
ans = input.nextLine();
} while (ans.equalsIgnoreCase("y"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.