Question

In: Computer Science

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

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

Solutions

Expert Solution

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:-->


Related Solutions

Write a program that calculates mean and standard deviation for four user entered values. The most...
Write a program that calculates mean and standard deviation for four user entered values. The most common measures of a sample are the mean and the standard deviation. the mean is the sum of the values divided by the number of elements in the data set. The dispersion - or spread in the values - is measured by the standard deviation The equation for the mean is x¯ = x1 + x2 + · · · + xn The equation...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
In Java I need a Flowchart and Code. Write the following method that tests whether the...
In Java I need a Flowchart and Code. Write the following method that tests whether the array has four consecutive numbers with the same value: public static boolean isConsecutiveFour(int[] values) Write a test program that prompts the user to enter a series of integers and displays it if the series contains four consecutive numbers with the same value. Your program should first prompt the user to enter the input size—i.e., the number of values in the series.
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...
*****Using Java Write a program that finds the standard deviation while also using a graphical user...
*****Using Java Write a program that finds the standard deviation while also using a graphical user interface.
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
What is the benefit of using mean deviation over standard deviation?
What is the benefit of using mean deviation over standard deviation?
I need to find the mean and standard deviation for this data SE-FamilySize 2 2 1...
I need to find the mean and standard deviation for this data SE-FamilySize 2 2 1 2 4 4 3 2 1 4 2 4 2 4 3 4 4 3 5 5 6 2 3 4 3 3 2 4 2 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT