JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP....

80.2K

Verified Solution

Question

Programming

JAVA

I need to write a code that calculates mean and standarddeviation using arrays and OOP. This is what I have so far. I'mclose but my deviation calculator method is throwing errors.

Thanks so much :)

import java.util.Scanner;import java.util.Arrays;public class STDMeanArray {  private double[] tenUserNums = new double[10];//Initialize an array with ten index'  private double mean;  private double deviation;  Scanner input = new Scanner(System.in);//create new scanner object  /*//constructor  public STDMeanArray(double tenUserNum [], double mean, double deviation){    super();    this.tenUserNums = tenUserNum;    this.mean = mean;    this.deviation = deviation;  }//end constructor*/  //getters and setters  public double[] getTenUserNums() {    return this.tenUserNums;  }  public void setTenUserNums(double[] tenUserNums) {    this.tenUserNums = tenUserNums;  }  public double getMean() {    return this.mean;  }  public void setMean(double mean) {    this.mean = mean;  }  public double getDeviation() {    return this.deviation;  }  public void setDeviation(double deviation) {    this.deviation = deviation;  }  public void promptUser() {    System.out.print(\"Enter ten numbers \");//prompt user    //fill array here    for (int i = 0; i < tenUserNums.length; i++) {      tenUserNums[i] = input.nextDouble();//fill the array      //System.out.print(anotherList[i] + \" \");    }//end for loop    System.out.println(\"The Numbers you entered into the array are\");//test erase me!!!!!    System.out.printf(Arrays.toString(this.tenUserNums)); // displays user filled array//test remove me!!!!!!!!  }//ends user input method  public void calculateMean() {    for (Double num : getTenUserNums()) {//short version of for loop      setMean(getMean() + num);    }    setMean(getMean() / getTenUserNums().length);//set mean again    System.out.println(\"\n the mean is: \" + String.format(\"%.02f\",getMean()));//this is a test erase me!!!!!!  }//ends calculate mean method  public void calculateDeviation(){    double sumMeanSquared=0.0;    String placeHoldNum=\"0\";    for(int i=0; i < getTenUserNums().length; i++)    {      if (this.tenUserNums == null// If the array is empty          || i >= this.tenUserNums.length || i < 0) {// or the index is not in array range        System.out.println(\"\n\n No deletion operation can be performed\n\n\");//return No deletion operation can be performed      } //ends if statement      else      {        placeHoldNum += Character.toString(getTenUserNums().length).charAt(i);        sumMeanSquared += Math.pow( Double.valueOf(placeHoldNum) - getMean(),2);        placeHoldNum=\"\";      }    }    setDeviation(Math.sqrt(sumMeanSquared/10.0));    System.out.println(\"The deviation is; \" + getDeviation());  }//ends calculateDeviation method  public void loop(){    promptUser();    calculateMean();    calculateDeviation();  }  // public class STDMeanArrayMain {  //main method     public static void main(String[] args) {      STDMeanArray test1 = new STDMeanArray();      test1.loop();    }}//ends class

Answer & Explanation Solved by verified expert
4.3 Ratings (708 Votes)
NOTE If you are stuck somewhere feel free to ask in thecomments but do not give negative rating rating to thequestionas it affects my answering rightscalling the functions again and again slows down yourprograminstead you should make use of local variablesthereas in calculatemean funcalso in thecalculatedeviation func you should have only run a foreach loopand there    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students