IST 140 Programming Assignment 13 – Rework Programming Assignment 12, but instea
ID: 3690196 • Letter: I
Question
IST 140
Programming Assignment 13 – Rework Programming Assignment 12, but instead of ge ng the integer values from the user via System.in, get only input and output le names from the user. The input le should contain the weight and integer values as speci ed below. (See Horstmann, pp. 319, 320, and pp.350, 351). Also, include a method called printResults that prompts the user for the name of an output le and then prints the results to that user-designated output le.
1. Create, using NetBeans, a complete Java program called CalcWeightedAvg according to the following guidelines.
Your main program should contain just three lines like these:
ArrayList<Double> inputValues = getData();
double weightedAvg = calcWeightedAvg(inputValues);
printResults(inputValues, weightedAvg);
The inputValues come from a single line in a text le such as the following: 0.5 70 80 90 10
The output le should contain something like the following:
“The weighted average of the numbers is 40, when using the data 0.5 70 80 90 10, where 0.5 is the weight, and the average is computed a er dropping the lowest of the rest of the values.”
To create the input le, while in NetBeans with your project open, click to highlight the top-level folder of your project, which should be called CalcWeightedAvg. Then
File-New File...
Next>
Keep the Project name at the top; keep Filter blank
Categories Other (at the bo om of the categories list)
File Types Empty File (at the bo om of the les list)
FileName: data.txt
Folder: this should be blank; if it's not, delete whatever's there
Finish
In the empty le data.txt that you just created, add a single line of data like the example above, where the rst number is a double (greater than 0.0 and less than or equal to 1.0) and the other numbers are the values you'll work with. Also, instead of displaying the output to the console, let the user choose a le name (as in Horstmann's example), and write the output to a le with that user-supplied name (e.g., output.txt).
Thoughts:
*) It's important that your input le is where NetBeans will look to nd it. The above instruc ons should make sure that that happens.
*) Note that if you run this progam from the command line, the input le should be in the same directory as the .class le.
Explanation / Answer
import java.util.*;
public class HelloWorld{
public static ArrayList<Double> getData()
{
Scanner read=new Scanner(System.in);
ArrayList<Double> inputValues=new ArrayList<Double>();
System.out.println("Enter integers ina single line");
String input = read.nextLine();
String[] token = input.trim().split("\s+");
for (int i = 0; i < token.length; i++)
inputValues.add(Double.parseDouble(token[i]));
return inputValues;
}
public static void printResults(ArrayList<Double> inputValues,double wavg)
{
System.out.print("The weighted average of the numbers is "+wavg+" , when using the data ");
for(int i=1;i<inputValues.size();i++)
System.out.print(inputValues.get(i)+" ");
System.out.print("where "+inputValues.get(0)+" is the weight ");
}
public static double calcWeightedAvg(ArrayList<Double> inputValues)
{
double total=0,weight=inputValues.get(0);
for(int i=1;i<inputValues.size();i++)
total+=inputValues.get(i)*weight;
total/=((inputValues.size()-2));
return total;
}
public static void main(String []args){
ArrayList<Double> inputValues = getData();
double weightedAvg = calcWeightedAvg(inputValues);
printResults(inputValues, weightedAvg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.