Preparation Open Blue) and create a project named Assignment9. Download the data
ID: 3709489 • Letter: P
Question
Preparation Open Blue) and create a project named Assignment9. Download the data9.txt file from D2L->Content->Assignment9 and save in the Assignment9 project folder. In BlueJ, in the Assignment9 project, create a new class named sumofSquares. Assignment Write a program that reads the integer values from the data file, square each data value, then sum the results of squaring. Count the number of data points read. After all data points have been read, squared, and the squares summed, print the results as in the example below (this is an example, not the actual results): The Sum-of-squares for the 50 data points is 12345Explanation / Answer
Hi,
I have written the program with a simple Scanner. So you update the file path in Scanner constructor according to your file directory.
Below is the program:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class SumOfSquares {
public static void main(String[] args) throws IOException {
int count = 0;
int sum = 0;
//Put your file path which you have downloaded.
Scanner in = new Scanner(new File("/home/sugamagarwal/workspace1/WebServiceProject/src/data9.txt"));
//Traversing through file to get next element
while(in.hasNext()) {
count++;
sum += in.nextInt();
}
System.out.println("The sum of squares for the "+ count + " data points is "+sum);
in.close();
}
}
Steps of the Problem:
Create one input stream or scanner object with the file path.
traverse the file with hasNext function in while loop.
and update the counter in each iteration to get the number of data points. and update the sum with the nextInt() function value which will return next integer in file.
In the last I have printed the values.