Question

Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a...

Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a number of properties about an array of doubles. There should be separate static methods to calculate the min, the max, the mean, the median, the standard deviation (make sure to make use of your mean method to implement this), and the mode. For the mode, you can assume that the data set is not multimodal. This class should not have a main method. Write a separate tester class called StatisticsTester which only has a main method. When run, it will ask the user for how many numbers they wish to enter and then allow the user to enter that many numbers. Store those numbers in an array and run each of the methods from the Statistics class on this data set. Print out the results.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Statistics.java:


class statistics {
static double myarray[]=new double[50];
static double sortedarr[]=new double[50];
  
public static double min(){
int i;
double min=0;
for (i=0;i<myarray.length;i++){
if(myarray[i]<=myarray[i+1]) //compare every 2 consecutive elements of array and store the min
min=myarray[i];
}
return min;
}
  
public static double max(){
int i;
double max=0;
for (i=0;i<myarray.length;i++){
if(myarray[i]>=myarray[i+1]) //compare every 2 consecutive elements of array and store the max
max=myarray[i];
}
return max;
}
  
public static double mean(){
int i;
double mean=0;
for (i=0;i<myarray.length;i++){
mean=mean+myarray[i];
}
mean=mean/myarray.length;
return mean;
}
  
public static double stdDeviation(){ //apply the formula (sum(i*i)/n)-(sum(i)/n)^2
int i;
double std=0,std1=0,std2=0;
for (i=0;i<myarray.length;i++){
std1=myarray[i]*myarray[i];
}
std1=std1/myarray.length;
std2=mean()*mean();
std=std1-std2;
return std;
}
  
public static double median(){
int n=myarray.length,c,d,med1;
double med=0,swap;

sortedarr=myarray;
//first apply bubble sort to sort the array
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (sortedarr[d] > sortedarr[d+1]) /* For decreasing order use < */
{
swap = sortedarr[d];
sortedarr[d] = sortedarr[d+1];
sortedarr[d+1] = swap;
}
}
}
//take median of sorted array
med1=n/2;
med=sortedarr[med1];
return med;
}
  
public static double mode(){
int i,n=myarray.length,k,j;
double mode=0,max=0;
for (i = 0; i < n - 1; i++)
{
mode = 0;
for (j = i + 1; j < n; j++)
{
if (myarray[i] == myarray[j]) {
mode++;
}
}
if ((mode > max) && (mode != 0)) {
k = 0;
max = mode;
}
  
}
return max;
}
}

StatisticsTester.java:


import java.util.Scanner;


public class StatisticsTester{
  
public static void main(){
statistics tester=new statistics();
Scanner in=new Scanner(System.in);
int number,i;
double min,max,mean,med,std,mode;
System.out.println("How many numbers do you want to enter?\n");
number=in.nextInt();
for(i=0;i<number;i++){
System.out.println("Enter the number\n");
tester.myarray[i]=in.nextDouble();
min=tester.min();
max=tester.max();
mean=tester.mean();
med=tester.median();
std=tester.stdDeviation();
mode=tester.mode();
System.out.println("In the class statistics, for the entered numbers\n min is "+min+"\nmax is "+max+"Median is "+med+"Standard deviation is "+std+"Median is "+med+"Mode is "+mode);
}
}
  
}

Add a comment
Know the answer?
Add Answer to:
Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java programming!!! Write the code fragment that will do the following: • Ask the user how...

    java programming!!! Write the code fragment that will do the following: • Ask the user how many numbers they want to enter • Declare and instantiate an array of doubles with as many elements as the number entered by the user • Write one 'for' loop to fill the array with data from the user • Write a second 'for' loop to calculate the sum of all of the numbers in the array • Print the calculated sum Note: Write...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the...

    pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the last assignment by providing the following additional features: (The additional features are listed in bold below) Class Statistics In the class Statistics, create the following static methods (in addition to the instance methods already provided). • A public static method for computing sorted data. • A public static method for computing min value. • A public static method for computing max value. • A...

  • Create a class called Play that has an InputReader as an instance variable. Be sure to...

    Create a class called Play that has an InputReader as an instance variable. Be sure to initialize it in the constructor. The class has two methods. Write a method with this signature: Write a method with this signature: public void stringPlay() The method prompts the user for a string, reads it in, and then displays the string as many times as the length of that string. The output string should be formatted with the first letter uppercase and the rest...

  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • Write another program called calculator that inherits from class average and also has methods that has:...

    Write another program called calculator that inherits from class average and also has methods that has: two methods multiply() to calculate the product of the numbers. One method will have two parameters which are both ints. The second method has three parameters: two ints and a Double. Another method “Power” that finds the power of a number using methods. For example, if the user types in: 5 and 3, the program should print out 125. 3. And another method “Factorial”...

  • Write another program called calculator that inherits from class average and also has methods that has:...

    Write another program called calculator that inherits from class average and also has methods that has: two methods multiply() to calculate the product of the numbers. One method will have two parameters which are both ints. The second method has three parameters: two ints and a Double. Another method “Power” that finds the power of a number using methods. For example, if the user types in: 5 and 3, the program should print out 125. 3. And another method “Factorial”...

  • C++, entry level no pointers or vectors. Write a class called Person that has two data...

    C++, entry level no pointers or vectors. Write a class called Person that has two data members - a string variable called name and a double variable called age. It should have a constructor that takes two values and uses them to initialize the data members. It should have get methods for both data members (getName and getAge), but doesn't need any set methods. Write a separate function (not part of the Person class) called stdDev that takes two parameters...

  • Java programming: Write a program called Distribution that reads a file of double values into an...

    Java programming: Write a program called Distribution that reads a file of double values into an array and computes and prints the percentage of those numbers within 1 standard deviation (SD), within 2 SDs, and within 3 SDs of the mean. The program should be modular and should at least contain the main method and a method for computing the above percentages given the numbers, the mean, and the standard deviation. Other required statistics should be computed in their own...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT