Lab 5: Weighted Average And Min Index

Due Friday 2/28/2020

Overview

The purpose of this lab is to give students practice with arrays and loops in Java by implementing two very common routines: finding the weighted average of a set of numbers, and finding the minimum out of a set of unordered numbers.

Code

Click here to download the skeleton code for the assignment. At the end, you should upload Lab5.java to canvas. A number of JUnit tests have been provided for you under Test Packages. You should be sure that you can pass all of these tests before you submit your solution.

Of course, you don't have to worry about passing the tests for the extra credit if you don't do it.

Part 1: Weighted Average

As your first task, you will implement a method that takes the weighted average of a set of values. The unweighted average of a set of values is their sum divided by the number of values. For example, the average of 1, 2, 3, 4 is (1+2+3+4)/4 = 10/4 = 2.5. The weighted average, on the other hand, includes a set of weights. For instance, let's consider a student in this class who gets some specific scores on different components of the class

CategoryWeightValue
Programming Assignments45100
Labs2560
Final Project1598
Class Participation550
Reading Surveys520
Debugging Table5100

The unweighted average of this would be (100+60+98+50+20+100)/6 = 73.333, which is a low C. If, however, we use the weighted average, we instead have

\[ \frac{(45)(100) + (25)(60) + (15)(98) + (5)(50) + (5)(20) + (5)(100)}{45+25+15+5+5+5} = 83.2\]

So instead of summing the numbers, we sum the numbers times their weights, and we divide by the sum of the weights. So actually, because programming assignments (which the student did great on) were weighted higher and the reading surveys (which the student did terribly on) were weighted lower, the weighted average ends up being much higher than the unweighted average in this case.

In your program, both the values and their weights are represented as parallel arrays of doubles. What this means is that both arrays have the same length, and weights[i] is the weight for values[i], for every index i in range. Your job is to fill in the method

getWeightedAverage(double[] weights, double[] values)

The arrays are passed from elsewhere; you simply treat them as variables at runtime. If you run the tests, they will call your method with different values. Note that you can get the length of an array arr with arr.length. You may assume that weights and values have the same length. Finally, note that arrays are zero-indexed. This means that arr[0] accesses the first element, and arr[arr.length-1] accesses the last element.

Corner case

There is one corner case your program must handle. If the length of weights and values is 0, then you should return 0. If you don't handle this properly, you will fail the testGetWeightedAverageCornerCase() test.

Part 2: Minimum Index (+1 Point Extra Credit)

For one point of extra credit, you should take in an array and return the index of the minimum element in that array by filling in the getMinIndex method in Lab5.java. If there are ties, you should return the index of the first instance of the minimum element. For example, if the array is

double[] arr = {-2, 5, 7, -5, 8, 9, -3, 2, -5, 3};

Then the call

getMinIndex(arr)

should return 3, since the minimum element, -5, first occurs at index 3 in the array (it's in the 4th position, recalling that we are 0-indexed).

What To Submit

You must submit the file Lab5.java to Canvas.