Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help modifying this code for (Intro to Java course)? I need the results to be placed in a new output file (For example, data file

Help modifying this code for (Intro to Java course)? I need the results to be placed in a new output file (For example, data file 4) instead of just having them just print out. I believe this involves BufferedWriter, but I'm not sure how to go about it. The two classes that are needed are below

 

-------------------------------------------------------------------------------------

import java.util.Scanner;

public class PeppersPillMill {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in); System.out.print("Enter Data file 1 path: "); String dataFile1 = scanner.nextLine();

System.out.print("Enter Data file 2 path: "); String dataFile2 = scanner.nextLine();

System.out.print("Enter Data file 3 path: "); String dataFile3 = scanner.nextLine();

TrialGroup groupOne = new TrialGroup(dataFile1); TrialGroup groupTwo = new TrialGroup(dataFile2); TrialGroup groupThree = new TrialGroup(dataFile3);

System.out.println("group 1: average = " + String.format("%.2f", groupOne.getAverage()) + " standard deviation = " + String.format("%.2f", groupOne.getStandardDeviation())); System.out.println("group 2: average = " + String.format("%.2f", groupTwo.getAverage()) + " standard deviation = " + String.format("%.2f", groupTwo.getStandardDeviation())); System.out.println("group 3: average = " + String.format("%.2f", groupThree.getAverage()) + " standard deviation = " + String.format("%.2f", groupThree.getStandardDeviation()));

} }

------------------------------------------------------------------------------------------------

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;

public class TrialGroup {

private String fileName; private int count; private int sum; private int sumOfSquares;

public TrialGroup(String fileName) { this.fileName = fileName; count = 0; sum = 0; sumOfSquares = 0; readFileContents(); }

public double getAverage() {

return (1.0 * sum / count); }

public double getStandardDeviation() { double average = getAverage(); double squareOfAverage = average*average; double averageOfSquares=sumOfSquares/count; return Math.sqrt(averageOfSquares-squareOfAverage);

}

public String getFileName() { return fileName; }

private void readFileContents() {

File file = new File(fileName); try { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { count += 1; int value = Integer.parseInt(scanner.nextLine()); sum += value; sumOfSquares += value * value; } scanner.close();

} catch (FileNotFoundException e) { e.printStackTrace(); }

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions