Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Help coding

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
Hi,

I am currently working on a project and was looking for some assistance in completing it. The project is for school and consist of coding a stats class that contains a number of statistical functions.

Below is the info.

Project Summary
Code a java class named "Stats" that includes the following "statistical" methods. Each of these methods accepts an int array as an input parameter and calculates a specific statistical value. The input parameter "int n" is used to pass the number of elements in the array that you should use in your calculations.

int Maximum( int ar[], int n)
{
// returns the maximum element value in the array "ar"
}
int Minimum(int ar[], int n)
{
// returns the minimum element value in the array "ar"
}
float Mean(int ar[], int n)
{
// returns the mean value for the array ar"
}
float Median(int ar[], int n)
{
// returns the median value for the array "ar"
}
float StandardDeviation( int ar[], int n)
{
// returns the standard deviation for the array "ar" (use formula below)
}

The main function
Your main function is used to call your statistical functions. It is up to you to generate some test data and check to make sure your methods work correctly. You do not have to provide code in your main() function when you turn in this project. The code for you main() function will be provided by the grading program.

Processing
The processing for each method is yours to figure out, however your methods should be able to handle an input array consisting of any number of elements.

What I have coded so far is here:
// Stats.Java
// Statistical calculator

public class Stats
{
//returns the Maximum value in the array "ar"
int Maximum( int ar[], int n)
{
// n = no of elements in array
int max =ar[0];
for ( int i=0; i<n; i++)
{
if ( ar[i] > max ) max = ar[i];
}
return max;
}

//returns the Minimum value in the array "ar"
int Minimum( int ar[], int n)
{
// n = no of elements in array
int min =ar[0];
for ( int i=0; i<n; i++)
{
if ( ar[i] < min ) min = ar[i];
}
return min;
}
//returns the Median Value for the array "ar"

//returns the Mean Value for the array "ar"

//returns the Standard Deviation for the array "ar"

//Data to test scropt that is not going to be included in final project
public static void main(String[] args)
{
int array [] = {10, 20, 30, 40, 50};
}
}

The main is not to be included in the submitted work but should be used to test the functions that I have so far. Any assistance will be greatly appreciated. I want to make sure I am on the right track. I used visual basic for the first section and am pretty new to java.

Mc

(instanceof Sidekick)
Posts: 8791
• Number of slices to send:
Optional 'thank-you' note:
Hi, welcome to the Ranch!

Looks like solid start. Lines like this one:

if ( ar[i] > max ) max = ar[i];

express what you're doing perfectly well. You can take a bit of a shortcut with methods on the Math class - I'll make you go look for them yourself. That would show your instructor a little more knowledge of the library but what you have surely solves the problem.

Here's a hint on building your main() or whatever method you use to test. Automate it so you can run it easily and make it show the results. Something like:

System.out.println( Stats.max( array, array.length ) == 50 );

I think the instructor is missing a neat opportunity by not asking to see your tests. If you work on writing them for readability they document what the code does very nicely. Many of us think they have significant lasting value as examples and docs.

maurice malone
Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
thanks

 He's giving us the slip! Quick! Grab this tiny ad! Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat