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

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"));

   }
}

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