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 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...
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 code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
Fill in the code for the function below called containsAvgValue. It takes as input a float...
Fill in the code for the function below called containsAvgValue. It takes as input a float array of any length and returns 1 if the array happens to contain a value that equals the average of all the values in the array or 0 otherwise. For example, when give the array [2.0, 2.0] it should return 1 since the average value is 2.0 and the array contains that value. To help you, I’ve included a function that computes the average...
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
in java pls Write a method that will receive 2 numbers as parameters and will return...
in java pls Write a method that will receive 2 numbers as parameters and will return a string containing the pattern based on those two numbers. Take into consideration that the numbers may not be in the right order. Note that the method will not print the pattern but return the string containing it instead. The main method (already coded) will be printing the string returned by the method. Remember that you can declare an empty string variable and concatenate...
In JAVA Find the code for sorts in your language some where online. You will copy...
In JAVA Find the code for sorts in your language some where online. You will copy code from the internet (with citation!) and modify it to test. Make a random array of integers, then perform each search on them. LINEAR SEARCH Write a linear search method to find a number in the list. Call this before each of your sort methods. Include a comment explaining how Linear Search works, when to use it, when you cannot. BINARY SEARCH Write a...
(in java) a. Include a good comment when you write the method described below: Write a...
(in java) a. Include a good comment when you write the method described below: Write a method factorial which receives a positive integer n and calculates n! (n factorial), defined as follows: n!=1*2*…*(n-1)*n. For example, 5! = 1*2*3*4*5 = 120. The method should return this value to the calling program. b. Write a driver program (main method) which will read an integer from the console, and pass it to the method to calculate its factorial. The main method then prints...