Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Standard Deviation Static
- import java.util.Scanner;
- import java.util.ArrayList;
- public class StandardDeviationStatic
- {
- public static void main(String args[])
- {
- /*Scanner sc = new Scanner(System.in);
- ArrayList values = new ArrayList();
- String b = "";
- boolean exit = true;
- while (exit != false)
- {
- System.out.print("Please enter a value (number): ");
- values = sc.nextInt();
- System.out.println("\nWould you like to continue? Please say yes or no.");
- b = sc.next();
- if (b.equalsIgnoreCase("no"))
- {
- exit = false;
- }
- else
- {
- exit = true;
- }
- }*/
- int std = 0;
- int[] valuesTest = new int[]{1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 8};
- int sumOfValues = 0;
- int numberOfValues = valuesTest.length;
- int average = 0;
- // getting the average
- for (int i = 0; i < valuesTest.length; i++)
- {
- average += valuesTest[i];
- }
- average /= valuesTest.length;
- for (int i = 0; i < valuesTest.length; i++)
- {
- sumOfValues += valuesTest[i];
- }
- for (int i = 0; i < valuesTest.length; i++)
- {
- std += (int) Math.sqrt(sumOfValues * (Math.pow((valuesTest[i] - average), 2)) / numberOfValues);
- }
- System.out.println("The Standard Deviation is: " + std);
- }
- }
- /*
- */
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Standard Deviation OOP
- public class StandardDeviation
- {
- private int[] values; // each value
- private int average; // average of all values
- private int numberOfValues; // number of all values
- private int sumOfValues; // sum of (values - average)^2
- public StandardDeviation()
- {
- values = new int[0];
- average = 0;
- numberOfValues = 0;
- sumOfValues = 0;
- }
- public StandardDeviation(int[] v, int a, int nov, int sov)
- {
- values = v;
- average = a;
- numberOfValues = nov;
- sumOfValues = sov;
- }
- public void setValues(int v[])
- {
- values = v;
- }
- public int formula()
- {
- // formula
- int std = 0;
- for (int i = 0; i < values.length; i++)
- {
- std = (int) Math.sqrt(sumOfValues * (Math.pow((values[i] - average), 2)) / numberOfValues);
- }
- return std;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Standard Deviation OOP
- import java.util.Scanner;
- public class StandardDeviationClient
- {
- public static void main(String args[])
- {
- StandardDeviation std = new StandardDeviation();
- Scanner sc = new Scanner(System.in);
- boolean ifEnter = true;
- int a = 0;
- System.out.println("Welcome to the Standard Deviation Class! We're going to have you input the values! Please enter: ");
- while (ifEnter == true)
- {
- a = sc.nextInt();
- std.setValues(a);
- }
- }
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement