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 to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
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...
I need to make JAVA program that calculates the area of a brick of a specific...
I need to make JAVA program that calculates the area of a brick of a specific color. I have class Brick with the following UML: String color int number int length int width I made methods: getColor(), getNumber(), getLength(), getWidth() in the class Brick So it starts like this: ##### public Brick(String color, int number, int length, int width) { this.color = color; this.number = number; this.length = length; this.width = width;    }   public String toString() {   return number...
*****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 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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT