1. Create, using NetBeans, a complete Java program called CalcWeightedAvg accord
ID: 3681108 • Letter: 1
Question
1. Create, using NetBeans, a complete Java program called CalcWeightedAvg according to the following guidelines. The program should use methods to carry out all computations. The method readIntValues should take no arguments, prompt the user to enter one line of between 5 and 10 space-separated integers with values between 0 and 100 (inclusive) and return an array list of these integers. The method getWeight should take no arguments, and it should prompt the user for a double with value greater than 0.0 but less than or equal to 1.0, and return as a double the value the user entered. The method calcWeightedAvg should return a double and take two parameters: an array list of integers and a double called weight (with values greater than 0.0 but less than or equal to 1.0). It should calculate the average of the values in the integer array by removing the lowest value from considera on and calcula ng the average of the other values, and then mul ply that average by the weight, and return the result. For example, if calcWeightedAvg is invoked as calcWeightedAvg(intArrayList, weight) (with weight = 0.50) then it should calculate the average of all values but the lowest value in intArray, mul ply that average by 0.50 and return the result to the main method, which will then display that value to the user. Example: integer input: 20 40 60 80 100 weight input: 0.50 calcWeightedAvg invoked as calcWeightedAvg(intArrayList, weight) returns 35.0, because 20 is dropped from considera on when calcula ng the average. Thoughts: *) You should need to change only a handful of lines in the program from Lesson 11. See Horstmann sec on 6.8. *) Your output should be something like this: “With input values 20 40 60 80 100, the lowest value is 20, and with a weight of 0.50, the weighted average of the remaining 4 values is 35.0.”
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
public class CalcWeightedAvg {
public static ArrayList<Integer> readIntValues(){
Scanner sc = new Scanner(System.in);
String input;
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.println("Enter one line of between 5 and 10 space-separated "+
"integers with values between 0 and 100 (inclusive)");
input = sc.nextLine();
for(String s : input.split("\s+")){// splitting by space
int x = Integer.parseInt(s);
list.add(x);
}
return list;
}
public static int smallest(ArrayList<Integer> list){
int small = list.get(0);
for(int x: list){
if(small > x)
small = x;
}
return small;
}
public static double getWeight(){
Scanner sc = new Scanner(System.in);
double weight = 0;
System.out.println("Enter a double with value greater than 0.0 but less than or equal to 1.0");
weight = sc.nextDouble();
sc.close();
return weight;
}
public static double calcWeightedAvg(ArrayList<Integer> list, double weight){
int sum = 0;
int smallest = smallest(list); // getting smallest element
for(int x : list){
if(smallest != x){ // ignoring smallest element
sum +=x;
}
}
double avg = ((double)sum)/(list.size()-1);
return avg*weight;
}
public static void main(String[] args) {
ArrayList<Integer> list = readIntValues();
double weight = getWeight();
double average = calcWeightedAvg(list, weight);
int smallest = smallest(list);
System.out.print("With input values ");
for(int x: list)
System.out.print(x+" ");
System.out.println("the lowest value is "+smallest+", and with a weight of "+weight+", "+
"the weighted average of the remaining "+(list.size()-1)+" values is "+average);
}
}
/*
Output:
Enter one line of between 5 and 10 space-separated integers with values between 0 and 100 (inclusive)
20 40 60 80 100
Enter a double with value greater than 0.0 but less than or equal to 1.0
0.50
With input values 20 40 60 80 100 the lowest value is 20, and with a weight of 0.5, the weighted average of the remaining 4 values is 35.0
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.