Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

create a new Java application called WeightedAvgDropSmallest (without the quotation marks) according to the following guidelines. The program prompts the user for five to ten

create a new Java application called "WeightedAvgDropSmallest" (without the quotation marks) according to the following guidelines.

The program prompts the user for five to ten numbers all on one line, separated by spaces, calculates the weighted average of all those numbers except the lowest n numbers, where n and the weight are also given by the user, and displays all the numbers, the weight, the number of lowest numbers dropped, and the calculated average to the user.

The program uses methods to:

Get the numbers used to calculate the average

Get the number of lowest numbers to drop before calculating the average

Get the weight, a double greater than 0 and less than or equal to 1

Calculate the weighted average of the numbers (except the lowest n numbers) entered by the user

Print the results

The first method should take no arguments and return an array list of doubles.

The second method should take no arguments and return a single integer, the number of the lowest numbers to drop before calculating the average.

The third method should take no arguments and return a double (the weight)

The fourth method should take three arguments: an array list of numbers (the return value of the first method above); an integer (the number of smallest items to drop before calculating the average); and a double (the weight). This method should return a double (the weighted average of all the numbers except the lowest n values).

The fifth method should take four arguments: an array list of numbers (the return value of the first method above); an integer (the number of smallest numbers to drop before calculating the average); a double (the weight); and a double (the weighted average returned from the fourth method above). This method should have no return value.

For example:

If the user gives these numbers for calculating the average:

40 60 80 100 20

and the user gives the number 2 to indicate how many of the lowest values should be dropped before calculating the average, and gives a weight of 0.5, then the program should give as output:

The weighted average of the numbers is 40.0, when using the data 40.0, 60.0, 80.0, 100.0, 20.0, where 0.5 is the weight used, and the average is computed after dropping the lowest 2 values.

Notes

This PA is a follow on to the previous module on arrays. It requires the use of ArrayLists and five methods. The PA describes each of the methods as far as parameters to pass and if there is a return value.

This PA does not require any exception handling. Also, it does not use files.

The first method where you are getting the values of type double from the user. I reused the while/hasNextDouble loop from previous assignments to extract the numeric values from the string (strOfNums) and then used the add method to store them in an ArrayList (called numsArray). Heres an example where curNum is type Double, count is type int:

Scanner lineOfNums = new Scanner(strOfNums);

while (lineOfNums.hasNextDouble()) {

curNum = Double.valueOf(lineOfNums.next());

numsArray.add(count, curNum);

count++;

}

The second and third methods are easy as they are just prompting the user for a value how many numbers to drop (type integer), and the weight (type double).

In the fourth method, remove the number of lowest values one approach is to use a nested for loop. The outer loop iterates over how many lowest values must be removed with the inner loop actually going through the ArrayList searching for the current lowest value. There is a remove method for ArrayLists.

Then once the lowest values have been removed, use a for loop to multiply each element value by the weight. Then go through the ArrayList again and accumulate a sum total in preparation for calculating the average. Pass the average back to main.

The fifth method simply outputs the values in the ArrayList using a for loop. Take note of the formatting requirements in the PA description.

The main method declares an ArrayList and some variables and then calls the five methods. It should be structured similar to this:

public static void main(String[] args) {

ArrayList numsArray;

// declare other variables

// call to 1st method, returns filled ArrayList numsArray

// call to 2nd method, returns number of values to drop (int)

// call to 3rd method, returns weight (double)

// call to 4th method, example call: wtAvg = calcAvg(numsArray, numOfDrops, weight);

// call to 5th method, example call: outResult(numsArray, numOfDrops, weight, wtAvg);

}

Here is an example run:

run:

Enter 5 to 10 numbers separated by blank spaces: 8.5 6.7 2.1 5.7 0.6 10.3

How many numbers should be dropped: 2

What is the weight: .5

The weighted average of the numbers is 3.90,

when using the data 4.25, 3.35, 2.85, 5.15,

where 0.50 is the weight used,

and the average is computed after dropping the lowest 2 values.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions