1. Create, using NetBeans, a complete Java program called CalcWeightedAvg accord
ID: 3681106 • 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 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 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, multiply that average by the weight, and return the result.
For example, if calcWeightedAvg is invoked as calcWeightedAvg(intArray, weight) (with weight = 0.50) then it should calculate the average of all values in intArray, multiply 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: 0 20 40 60 80 100
weight input: 0.50
calcWeightedAvg invoked as calcWeightedAvg(intArray, weight) returns 25.0.
Thoughts:
*) The method readIntValues will probably be the most challenging one to write, since the integer values you want will be embedded in the line of input, which is a string. I'll give some sugges ons about how this might be done.
*) Print some explanatory informa on with your result, such as, “With input values 0 20 40 60 80 100, and with a weight of 0.50, the weighted average of the all the values is 25.0.”
Explanation / Answer
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
public class HelloWorld{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
public void calcWeightedAvg(int numbers[],double weight)
{
int sum=0;
double avg=0;
for(int i=0;i<numbers.length;i++)
sum+=numbers[i];
avg=(sum/numbers.length)*weight;
System.out.println("calculated weight is"+avg);
}
public double getWeight()
{
try
{
System.out.println("Enter weight in range of 0 and 1");
double weight=Double.parseDouble(bufferedReader.readLine());
if(weight<0 || weight>1.0)
{
System.err.println("Invalid weight value");
System.exit(0);
}
return weight;
}
catch (IOException e) {
e.printStackTrace();
return 0;
}
}
public void readIntValues()
{
System.out.println("Enter 5-10 values between 0 and 100");
String input=null;
try {
int size=0;
input = bufferedReader.readLine();
String[] StringNumbers=input.split(" ");
int[] numbers=new int[StringNumbers.length];
for(int i=0;i<StringNumbers.length;i++)
{
try
{
if(Integer.parseInt(StringNumbers[i]) >=0 && Integer.parseInt(StringNumbers[i])<=100)
{
numbers[i]=Integer.parseInt(StringNumbers[i]);
size++;
}
}
catch (NumberFormatException ex) {
}
}
System.out.println(size);
if(!(size>=5 && size<=10))
{
System.err.println("Invalid input values.Enter 5-10 values between 50-100");
System.exit(0);
}
calcWeightedAvg(numbers,getWeight());
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String []args){
HelloWorld test=new HelloWorld();
test.readIntValues();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.