Question

Write a three parameter generic function that will average the numbers selected from a list but...

Write a three parameter generic function that will average the numbers selected from a list but only those between low and high, inclusive.   That is, this is a three parameter function, with list,

low, and high cutoff values. Be careful, the average depends only on the selected numbers.

B. Test your function with the salesRevenue list and having a low = 30 and high = 100. Your function should return the average of those numbers. Print this out, properly identified.

   salesRevenue = [ 20, 30, 35, 15, 80, 100]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Main {

public static <T extends Comparable<T>> double average(T arr[], T low, T high) {

double sum = 0;

int count = 0;

for (int i=0; i<arr.length; i++) {

if (low.compareTo(arr[i]) <= 0 && high.compareTo(arr[i]) >= 0) {

sum = sum + (int)arr[i];

count ++;

}

}

return sum / (double)(count);

}

public static void main(String[] args) {

Integer salesRevenue[] = {20, 30, 35, 15, 80, 100};

System.out.println(average(salesRevenue, 30, 100));

}

}

Add a comment
Know the answer?
Add Answer to:
Write a three parameter generic function that will average the numbers selected from a list but...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a python function that can find the average of given list of numbers. It prints...

    Write a python function that can find the average of given list of numbers. It prints all numbers whose values is less than the average on the screen. Demonstrate with three example data on how it works?

  • 3. Write a function that takes, as arguments, three numbers, and returns their average. Round the...

    3. Write a function that takes, as arguments, three numbers, and returns their average. Round the values to the nearest tenth. Name this function calculate Average (p1, p2, p3). For example, >>>calculate Average (2.5, 3.5, 9) average of those 3 values, using decimal division. Round the values to the nearest tenth. Name your function get Average String (p1, p2, p3). For example, >>>get Average String (1.5, 3.5, 4) should return the string “The average value is 3.0.” Your sentence must...

  • FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter....

    FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter. It returns the index of the minimum row in the list. The minimum row is the row with the smallest sum of elements. If the list has multiple rows that achieve the minimum value, the last such row should be returned. If the list is empty the function should return -1. The following shows several sample runs of the function: Python 3.4.1 Shell File...

  • More Practice: Writing Lists& Strings . Write a function average_ evens that takes a list of...

    More Practice: Writing Lists& Strings . Write a function average_ evens that takes a list of numbers as parameters, and adds all the even numbers together and returns their average using a list Example function call: print (average_evens (I-2, -3, 4, 0, 1, 2,3 Outputs: 0 Write a function match that takes two strings as a parameter and returns how many letters the strings have in common. You should treat upper and lower case letters as the same letter ('A,...

  • This is a python work, thank for helping! 7. (6 points) Write a function, countOfAndSmalest Number...

    This is a python work, thank for helping! 7. (6 points) Write a function, countOfAndSmalest Number Between(low, high, ignore, listOfNumbers), that takes as input three numbers and a list of numbers, and returns a list containing two items: 1) how many items in listOfNumbers are greater than low, less than high, and not equal to ignore 2) the smallest number in listOfNumbers that is greater than low, less than high, and not equal to ignore (or None if there is...

  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of...

    Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of first list in Linked List#1, and numbers of second list in Linked List#2. Do not insert both lists in a single Linked List. List#1. 5, 78, 45, 23, 11, 89, 10, 78, 6, 99, 876, 5, 67, 13 List#2. 5, 89, 688, 52, 557, 953, 5, 7, 55, 35, 89, 99, 99, 6, 557, 89, 5, 99, 6, 2, 45, 12, 7, 6, 94,...

  • Write a complete program that uses the functions listed below. Except for the printOdd function, main...

    Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...

  • Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that...

    Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that consumes a list of numbers and prints a histogram graph using asterisks to represent each number in the list. Use one output line per number in the list. You may assume that only integers are passed to this function. Your function should ignore negative values. Include a docstring! Test Result print_histogram([ 0, 2, 4, 1]) ** **** * print_histogram([10, 5, 3, -1, 8]) **********...

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT