Question

The input file should contain (in order): the weight (a number greater than zero and less...

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.

The program should also write to an output file (rather than the console, as in Horstmann). So 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.

Remember to get the name of the output file from the user; do not hard-code that name into your program.

  • Your program should allow the user to re-enter the input file name if an exception is caught in the main method
  • Your methods for reading from and printing to a file should each throw a FileNotFoundException, which should be caught in the main method as is done in Horstmann's DataAnalyzer.java (for the input file). Note that you may deal with just a FileNotFoundException in the while loop that tests for correct input. That is, you don't need to deal with either the NoSuchElementException or the IOException as Horstmann does. Also, you should deal with (just) a FileNotFoundException when calling the method that writes to an output file. That method call should be in main, and it should come after the end of the while loop.
  • Use try-with-resources statements in your methods for reading and writing data, and so avoid the need to explicitly close certain resources.

Remember to use a try-with-resources statement in your method to write to a file 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." In particular, you should give the weighted average, the data values that the user entered in the order that the user gave them, the weight, and the number of terms that were dropped before computing the weighted average.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

RATE THUMBSUP PLEASE

ANSWER:

EXPLANATION:

CODE:

import java.io.File;

public class WeightedAvgDataAnalyzer {

public static void main(String[] args) {

  

ArrayList<Double> input = getData();

System.out.println(input);

double weightedAverage = calcWeightedAvg(input);

printResults(input, weightedAverage);

}

public static ArrayList<Double> getData() {

ArrayList<Double> inputLine = new ArrayList<Double>();

Scanner console = new Scanner(System.in);

System.out.println("Enter 'data.txt' (without the quotes) as the name of the input file: ");

String inputFileName = console.next();

File inputFile = new File(inputFileName);

Scanner in = null;

try {

in = new Scanner(inputFile);

while (in.hasNextDouble()) {

inputLine.add(in.nextDouble());

}

} catch (FileNotFoundException fne) {

String errorString = fne.getMessage();

System.out.println("There was an error when trying to read from file "

+ inputFileName + ": " + errorString);

} finally {

if (in != null) {

in.close();

}

}

return inputLine;

}

public static double calcWeightedAvg(ArrayList<Double> data) {

double enteredWeight = Double.parseDouble(data.get(0).toString());

int numberofLowetValuesDropped =(int) Double.parseDouble(data.get(1).toString());

data.remove(1);

data.remove(0);

Collections.sort(data);

double total = 0;

for(int i=numberofLowetValuesDropped; i<data.size(); i++){

total = total + Double.parseDouble(data.get(i).toString());

}

data.add(0, enteredWeight);

data.add(1, Double.valueOf(numberofLowetValuesDropped));

double avg = total/(data.size() - numberofLowetValuesDropped);

return avg;

  

}

public static void printResults(ArrayList<Double> inputList, double weightedAvg) {

Scanner console = new Scanner(System.in);

System.out.println("Enter the name of the output file: ");

String outputFileName = console.next();

PrintWriter out = null;

if (inputList.size() > 0) { // we have a non-empty inputList

try {

out = new PrintWriter(outputFileName);

String s = "The weighted average of the numbers is "+weightedAvg+", " +

"when using the data "+inputList+", where "+inputList.get(0)+" is the weight used, " +

"and the average is computed after dropping the lowest "+inputList.get(1)+" values.";

out.print(s);

System.out.println("Your output is in the file " + outputFileName + ".");

} catch (FileNotFoundException fne) {

String errorString = fne.getMessage();

System.out.println("There was an error when trying to write to the output file "

+ errorString);

} finally {

if (out != null) {

out.close();

}

}

} else {

System.out.println("Problems reading data from input file; no output written to " + outputFileName);

}

}

  

}

HOPE IT HELPS YOU RATE THUMBSUP IT HELPS ME ALOT

THANKS

> the only thing that isnt working is the print Results

John smith87 Wed, Dec 8, 2021 11:15 AM

Add a comment
Know the answer?
Add Answer to:
The input file should contain (in order): the weight (a number greater than zero and less...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The input file should be called 'data.txt' and should be created according to the highlighted instructions...

    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...

  • create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in...

    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...

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

    create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files. Input File The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions...

  • Write a program that would ask the user to enter an input file name, and an...

    Write a program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and read the data in each line as a number (double). These numbers represent the temperatures degrees in Fahrenheit for each day. The program should convert the temperature degrees to Celsius and then writes the numbers to the output file, with the number of day added to the beginning of the...

  • Write a program that will take input from a file of numbers of type double and...

    Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Problem 1. A file that contain two columns of data are provided. You should write a...

    Problem 1. A file that contain two columns of data are provided. You should write a program that calculate the average of the two number in each row, and output them into a separate file. Some row contains non-numbers or may be missing an number, and your code should use Exception to catch these errors, and output an appropriate error message. For testing, your input data file should be: 1.2 3.5 one 4.4 2.5 three 4.1 9.9 1.9 18.5. 20.3

  • Write a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".

    Project DescriptionWrite a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".Project SpecificationsInput for this project:the user must enter the number of employees in the company.the user must enter as integers for each employee:the employee number (ID)the number of days that employee missed during the past year.Input Validation:Do not accept a number less than 1 for the number of employees.Do not accept a negative...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT