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

Write a JAVA program that reads a list of 25 values from the user. Put the value

ID: 3820661 • Letter: W

Question

Write a JAVA program that reads a list of 25 values from the user. Put the values in an array. The program should read the array and then calculate and display the average of the even input values and the average of the odd input values. You should have a class and a tester. In the class, you will have three instance variable, 1) an array to hold the numbers, 2) a double to hold the odd average and a double to hold the even average. The constructor should accept as input the array from the tester class and assign it to the instant variable; it should also assign 0 to the even and odd average variables. You should then have a method that will read the array and then calculate the average of the even input values and the average of the odd input values. Your program will need a getOddAverage and a getEvenAverage method. The tester class will ask the user to input numbers (consider partially filled as well). It will then create an object passing the filled array to the class. The tester should then call the calculate the averages method. You will then need to print the even and odd average.

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class A{
   int array[];
   double odd,even;
   A(int array[]){
       this.array = array;
       odd = even = 0;
   }
   public double getOddAverage(){
       int odd = 0;
       double sum = 0;
       for(int i=0;i<array.length;i++){
           if(array[i]%2==1){
               odd++;
               sum+=array[i];
           }
       }
       return sum/odd;
   }
   public double getEvenAverage(){
       int even = 0;
       double sum = 0;
       for(int i=0;i<array.length;i++){
           if(array[i]%2==0){
               even++;
               sum+=array[i];
           }
       }
       return sum/even;
   }
}
class Ideone
{
   static final int MAX = 25;
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       Scanner obj = new Scanner(System.in);
       System.out.println("Enter the 25 numbers");
       int arr[] = new int[MAX];
       for(int i=0;i<MAX;i++){
           arr[i] = obj.nextInt();
       }
       A a = new A(arr);
       System.out.println("Odd Average : "+a.getOddAverage());
       System.out.println("Even Average : "+a.getEvenAverage());
      
   }
}

OUTPUT :

6 34 9 5 9 567 5 44 8 7 2 8 4 9 4 2 576 90 4 9 56 34 98 4 67

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