Question

How do you code in Java a method (lets say its called PosOddAverage) that: - Takes...

How do you code in Java a method (lets say its called PosOddAverage) that:

- Takes an array of doubles as input
- Prints the avearge of positive odd numbers in the array
- Prints 'ERROR' for empty input array

An Example:

Input: [1.2,1.5,0.0,-2.3,3.5,7.1,-1.3]
Output: 4.03333333

Thanks :)

Homework Answers

Answer #1

public class OddNumber {

   public static void PosOddAverage(double[] arr) {
      
       int count=0;           // count of odd numbers in the array
       double sum=0;       // sum of odd numbers int the array
      
       if(arr.length!=0) {                               // if array is not empty then only we will calculate average
           for( int i=0;i<arr.length;i++) {   //traversing till array length to calculate sum of the odd numbers
              
               if((arr[i]*10)%2==1) {               // if number is odd and positive(-1!=1 satisfying positive condition) we will enter into loop(multiplying it by 10 eliminates decimal point then making %2 equals one then its an odd number
                   sum=sum+arr[i];                   // adding every odd number to sum value
                   count++;                                   // and incrementing the count of the odd numbers
               }
           }
           double Average=sum/count;       //average of the odd numbers in the array where average=sum of digits/count of digits
           System.out.printf("Average of Positive Odd: %f", Average);       //finally printing the average of the odd numbers in the array
       }  
       else {
           System.out.println("ERROR:Empty input Array");   // if array is empty prints relevant message
       }
   }
   public static void main(String[] args) {
      
       double arr[]= {1.2,1.5,0.0,-2.3,3.5,7.1,-1.3};
       PosOddAverage(arr);  
   }

}

Output:

Output: If empty array is present.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
python question lets say you had a string that was returned from a block of code...
python question lets say you had a string that was returned from a block of code that converted it from letters to numbers. for example, the user input was "hi", the code converted it into "89" as a string with a dictionary how could you take that "89" and go back into hi again? lets say the dictionary was keys and values of keys = alphabet and values = numbers the output would be back at "hi" again Convert into...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function returns an empty matrix. Otherwise, the function doubles every odd element of A and returns the resulting matrix. Notice that the output matrix will have all even elements. For example, the call B = matrix_problem([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. The function should work...
Write an application in Java which includes an algorithm that takes an array of any size,...
Write an application in Java which includes an algorithm that takes an array of any size, selects the high and low integer from the array of integers with each pass and builds a new array of integers by inserting the high and low selection with each pass. Your output should show the original array of integers and the output of each pass on a new line. Note: You can assume all integers are positive, but your code must work for...
Write a Java part code to do the following   : ** Suppose a file called infile stored...
Write a Java part code to do the following   : ** Suppose a file called infile stored in drive D: ,filled with five integers(1000,200,3030,40 and 500) Read  the integers from infile then store them in array called ar[] and print it elements. Method called search( ) to print the location of   value 500. Method called sort() to sort array elements. Build another file called outfile in drive D: to save the sorted array elements . Note : Use ArrayIndexOutOfBoundsException when you use array...
Code a calling and called a method that demonstrates that actual parameters, that are variables of...
Code a calling and called a method that demonstrates that actual parameters, that are variables of value types, do not have side effects but those of reference types can have such effects. Include appropriate display instructions to prove the point. Use a parameter that is an array of int as your reference type and an element of this array as your value type. Use java and explain the code, thanks
Write a Java method that takes a String as Input and replaces the first two occurrencesof...
Write a Java method that takes a String as Input and replaces the first two occurrencesof a given character to another provided character.   For example, if the input String is “Java is Great” and asked to replace the letter a with x then the output would be: “Jxvx is Great” The method takes a String and two characters and returns the modified String. Please make your own assumptions if needed, you do not need to write the main.
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2D array and keep a count of how many cells are full. You will be given the dimensions of the array. INPUT: All input has been handled for you: Two integers denoting the dimensions of the array. These are the integer variables rows and cols. A partially filled 2-D array....
Instructions Write a Java code Your goal is to take N integer inputs from the user...
Instructions Write a Java code Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort algorithm. The Merge step merges n elements which takes O(n) time. Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Any code that is found to exceed linear time will fail the tests. Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
answer for how it would be evaluated in Java without writing a code for it. a)...
answer for how it would be evaluated in Java without writing a code for it. a) 17 + 2 - 5 / 5 b) 2 + 3 * 5 - 2 c) 3 * 2 / 3 + 8 d) 6 % 2 * 10 Problem 2 : Write an application in java that inputs one number consisting of 5 digits (e.g. 12345) from the user, separates the number into its individual digits and prints the digits separated from one...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT