First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values. You should also prompt the user for the name of the output file, and then print your results to an output file with the name that the user specified. Your program should allow the user to re-enter the input file name if one or more of the exceptions in the catch clauses are caught. Your methods for getting data and printing results should each throw a FileNotFoundException which should be caught in the main method. • Use try-with-resources statements e in your methods for getting and printing the data, and so avoid the need to explicitly close certain resources. • In your readData method, use hasNextDouble to check ahead of time whether there's a double in the data. That way when you try to get the nextDouble, your code won't throw a NoSuchElementException. You can use a writeFile method that does all the work (i.e., does not call a writeData method the way that Horstmann's readFile method calls a readData method). Use a try-with-resources statement in your writeFile method when creating a new PrintWriter. The inputValues come from a single line in a text file (data.txt) such as the following: 0.5 3 10 70 90 80 20 The output in the output file must give the weighted average, the data and weight that were used to calculate the weighted average, and the number of values dropped before the weighted average was calculated. Your output should look very much like the following: "The weighted average of the numbers is 42.5, when using the data 10.0, 70.0, 90.0, 80.0, 20.0, where 0.5 is the weight used, and the average is computed after dropping the lowest 3 values." Write the output to a file with the filename that the user chose to name the output file (e.g., output.txt). Don't hard-code the output file name in your program.

Programming with Microsoft Visual Basic 2017
8th Edition
ISBN:9781337102124
Author:Diane Zak
Publisher:Diane Zak
Chapter10: Classes And Objects
Section: Chapter Questions
Problem 2E: Open the Palace Solution.sln file contained in the VB2017\Chap10\Palace Solution folder. Use Windows...
icon
Related questions
Question

 

Code isn't running, how do i get it to print to a txt file 

 

 

 

 

public class WeightedAvgDataAnalyzer {

public static void description() {
System.out.println("This program calculates the weighted average of "
+ "all those numbers except the lowest n numbers. \n");
}//end
// get numbers from user
public static ArrayList<Double> getNumbers(Scanner en) {
ArrayList<Double> list = new ArrayList();

while(en.hasNextDouble()) {
list.add(en.nextDouble());
}

return list;
}//end
//gets the lowest number from user and return it as n
public static int getNumberLowest(Scanner scan) {

int n = scan.nextInt();
return n;
}//end
//get the weight
public static double getWeight(Scanner en) {
double weight = en.nextDouble();
return weight;
}//end

//remove Lowest And CalAvg -- returns the avgrage
public static double calculateWeightedAverage(ArrayList<Double> list, int lowest) {
double first = list.get(0);
double sec = list.get(0);
for (double n : list) {
if (first > n) {
sec = first;
first = n;
} else if (sec < n) {
sec = n;
}
}
double avg = 0;
double total = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) != first && list.get(i) != sec) {
total = total + list.get(i);
}
}
avg = total / (list.size() - lowest);
return avg;
}//end
//display results
public static void displayResults(ArrayList<Double> list, int lowest, double avg, double weight) {
System.out.print("The weighted average of the numbers is " + avg * weight + ", when using the data \n");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + ", "); // list each num and adds a common after eac one
}
System.out.println("where "+ weight + " is the weight used, and the \n"
+ "average is computed after dropping the lowest " + lowest + " values.");
}//end

// define the main method
public static void main(String[] args) {

//Program Description
description();
//display results
String fileName = "";
double weight = 0;
int lowest = 0;
ArrayList<Double> list = new ArrayList();

System.out.println();
//get file name
Scanner en = new Scanner(System.in);
System.out.println("Enter file name: ");
fileName = en.next();

try(PrintWriter out = new PrintWriter(new File(fileName))){
weight = getWeight(en);
lowest = getNumberLowest(en);
list = getNumbers(en);
double average = calculateWeightedAverage(list, lowest);
displayResults(list, lowest, average, weight);

} catch (IOException ex) {
System.out.println("There was an error try again");
ex.printStackTrace();
}





}
}

First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the
DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below.
The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though
you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of
the input file.
The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest
numbers to drop, and the numbers to be averaged after dropping the lowest n values.
You should also prompt the user for the name of the output file, and then print your results to an output file with the name that the
user specified.
Your program should allow the user to re-enter the input file name if one or more of the exceptions in the catch clauses are
caught.
Your methods for getting data and printing results should each throw a FileNotFoundException which should be caught in the
main method.
• Use try-with-resources statements e in your methods for getting and printing the data, and so avoid the need to explicitly close
certain resources.
• In your readData method, use hasNextDouble to check ahead of time whether there's a double in the data. That way when you try
to get the nextDouble, your code won't throw a NoSuchElementException.
You can use a writeFile method that does all the work (i.e., does not call a writeData method the way that Horstmann's readFile
method calls a readData method). Use a try-with-resources statement in your writeFile method when creating a new PrintWriter.
The inputValues come from a single line in a text file (data.txt) such as the following:
0.5 3 10 70 90 80 20
The output in the output file must give the weighted average, the data and weight that were used to calculate the weighted average,
and the number of values dropped before the weighted average was calculated.
Your output should look very much like the following: "The weighted average of the numbers is 42.5, when using the data 10.0, 70.0,
90.0, 80.0, 20.0, where 0.5 is the weight used, and the average is computed after dropping the lowest 3 values."
Write the output to a file with the filename that the user chose to name the output file (e.g., output.txt). Don't hard-code the output
file name in your program.
Transcribed Image Text:First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values. You should also prompt the user for the name of the output file, and then print your results to an output file with the name that the user specified. Your program should allow the user to re-enter the input file name if one or more of the exceptions in the catch clauses are caught. Your methods for getting data and printing results should each throw a FileNotFoundException which should be caught in the main method. • Use try-with-resources statements e in your methods for getting and printing the data, and so avoid the need to explicitly close certain resources. • In your readData method, use hasNextDouble to check ahead of time whether there's a double in the data. That way when you try to get the nextDouble, your code won't throw a NoSuchElementException. You can use a writeFile method that does all the work (i.e., does not call a writeData method the way that Horstmann's readFile method calls a readData method). Use a try-with-resources statement in your writeFile method when creating a new PrintWriter. The inputValues come from a single line in a text file (data.txt) such as the following: 0.5 3 10 70 90 80 20 The output in the output file must give the weighted average, the data and weight that were used to calculate the weighted average, and the number of values dropped before the weighted average was calculated. Your output should look very much like the following: "The weighted average of the numbers is 42.5, when using the data 10.0, 70.0, 90.0, 80.0, 20.0, where 0.5 is the weight used, and the average is computed after dropping the lowest 3 values." Write the output to a file with the filename that the user chose to name the output file (e.g., output.txt). Don't hard-code the output file name in your program.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 9 steps with 9 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning