Question

In: Computer Science

write this java code: One statistical operation that is sometimes performed on a set of data...

write this java code:

One statistical operation that is sometimes performed on a set of data values is to remove values that are far from the average. Write a program that reads real values from a text file, one per line. Store the data values as Double objects in an instance of the class java.util.ArrayList. Then Use an iterator to compute the average and standard deviation of the values. Display these results. Use a second iterator to remove any value that is more than two standard deviations away from the average. Use a for-each loop to display the remaining values and compute the new average. Display the new average. If the data values are , their average μ is their sum divided by n, and their standard deviation is

Solutions

Expert Solution

solution:

given data:

The program is given below: that read data from read_data.txt line by line and store into arraylist arr1, then find average and standarad deviation, then remove any value that is more than 2 standard deviation away from the average (this is done by adding value into arraylist arr2 which is not more than 2 standard deviation away from the average), then display values of arr2 using for each loop , then calculate and print new average.

import java.util.*;
import java.io.*;

public class Main
{
   public static void main(String[] args) {
   //create ArrayList object arr1
       ArrayList<Double> arr1= new ArrayList<Double>();
       //create ArrayList object arr2
       ArrayList<Double> arr2= new ArrayList<Double>();
       try {
       double sum=0,avg=0,std_sum=0,standard_deviation=0;
       int n,i;
       //create file instance
File file1 = new File("read_data.txt");
BufferedReader br= new BufferedReader(new FileReader(file1));
String Line="";
//read line by line
while ((Line=br.readLine())!= null) {
//convert string to Double and add object into arrayList
arr1.add(Double.parseDouble(Line));
}
//calculate sum
for(double val:arr1) {
sum=sum+val;
}
n=arr1.size();
//calculate Average
avg=sum/n;
  
//calculate Standard deviation
//first perform sum of (val-avg)^2
for(double val:arr1) {
std_sum=std_sum+Math.pow((val-avg),2);
}
//devide sum of (val-avg)^2 by n and take square root
standard_deviation=Math.sqrt(std_sum/n);
  
//print Average and standard_deviation
System.out.println("Average: "+avg);
System.out.println("Standard deviation: "+standard_deviation);
//remove value that is more than 2 standard_deviation away from the Average
for(i=0;i<n;i++) {
//System.out.println(i+ " " +arr1.get(i));
if(arr1.get(i)>=(avg-(2*standard_deviation)) && arr1.get(i)<=(avg+(2*standard_deviation)))
{
arr2.add(arr1.get(i));
}
}
double new_sum=0,new_avg=0;
//display remaining value using foreach loop
System.out.println("data: ");
for(double val1:arr2)
{
System.out.println(val1);
new_sum=new_sum+val1;
}
//calculate new Average
new_avg=new_sum/arr2.size();
//display new_avg
System.out.println("New Average: "+new_avg);
} catch (IOException e) {
e.printStackTrace();
}
   }
}

The screenshot of code is given below:

read_data.txt

12.50
13.23
11.50
200.10
30.20
15
14
19
50.10

Output:

please give me thumb up


Related Solutions

write JAVA code with the following condition Write the pseudocode for a new data type MyStack...
write JAVA code with the following condition Write the pseudocode for a new data type MyStack that implements a stack using the fact that you have access to a queue data structure with operations enqueue(), dequeue(), isEmpty(). Remember that every stack should have the operations push() and pop(). Hint: use two queues, one of which is the main one and one is temporary. Please note that you won’t be able to implement both push() and pop() in constant time. One...
AVL tree; Insert and Range Minimum operation in Java code.
AVL tree; Insert and Range Minimum operation in Java code.
Code in Java, Use both Set and TreeSet in only one main to show the differences...
Code in Java, Use both Set and TreeSet in only one main to show the differences between the Set and TreeSet that Set still remove the duplicate but won't sort the elements. Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word...
write a program in java that contain a class for botique . data member include code...
write a program in java that contain a class for botique . data member include code , color , size , quantity . your class should contains all accessor and mutator methods , non paraqmetric constructor , parametric constructor , input andvidsplay method
A) If the statistical test performed on the data gives p = 0.001 in the case...
A) If the statistical test performed on the data gives p = 0.001 in the case where the alternative hypothesis H1 is true, the resulting conclusion is (at 5% significance level) Choose one: 1. right negative 2. false negative 3. right positive 4. false positive 5. True, but it is not known whether it is positive or negative 6. False, but it is not known whether it is positive or negative 7. positive, but it is not known whether it...
QUESTION Write the main while loop for a Java program that processes a set of data...
QUESTION Write the main while loop for a Java program that processes a set of data as follows: Each line of the input consists of 2 numbers representing a quantity and price. Your loop should: 1. Read in the input 2. Calculate the tax. Tax will be 8.875% of price and will only be applicable if price is more than 110. Calculate the new price which is the price plus tax. 3. Calculate the final price by multiplying quantity by...
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
Write code in R for this questions,, will vote!! Load the Taxi.txt data set into R....
Write code in R for this questions,, will vote!! Load the Taxi.txt data set into R. (a) Calculate the mean, median, standard deviation, 30th percentile, and 65th percentile for Mileage and TripTime. (b) Make a frequency table for PaymentProvider that includes a Sum column. Report the resulting table. (c) Make a contingency table comparing PaymentType and Airport. Report the resulting table. (d) Use the cor() function to find the correlation between each pair of the Meter, Tip, Mileage, and TripTime...
Code in Java, Use both Set and TreeSet to show the differences between the Set and...
Code in Java, Use both Set and TreeSet to show the differences between the Set and TreeSet that Set still remove the duplicate but won't sort the elements. Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word and meanings as the...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again. public class Product { private String name; private double price; public Product(String productName, double productPrice) { name = productName; price = productPrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double amount) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT