In: Computer Science
JAVA
I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close 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
NOTE: If you are stuck somewhere feel free to ask in the comments but do not give negative rating rating to the question.....as it affects my answering rights.......
calling the functions again and again slows down your program.....instead you should make use of local variables there.....as in calculatemean() func...also in the calculatedeviation() func you should have only run a for-each loop and there was no need for placeholder stuff.....
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
//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
}//end for loop
}//ends user input method
public void calculateMean() {
double sum=0;
for (Double num : getTenUserNums()){//short version of for loop
sum+=num;
}
setMean(sum/getTenUserNums().length);
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;
for (Double num : getTenUserNums()){
sumMeanSquared += Math.pow( num - getMean(),2);
}
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
Output:-->