Hello, everyone. I'm at the moment doing a homework assignment involving static methods and arrays. It consists of the following instructions.

I'm to create a class known as ArrayStatisticsMethods and in this class, there are definitions to four different "public static" methods which will be described. I'm provided the use of a problem known as StatCalculator which will be provided as well for it isn't modified in any way. The parameters for the methods are arrays and we refer to the array as full if something is assigned to every index in the array. Java can allow arrays to be of size 0 and for what I've been asked, there is no meaningful output and I can assume that arrays are not empty.

The instructions are as follows.
1) Write a method that takes a full array of double as a parameter and I'm to refer to this method as arrayMean. What it should do is to return the mean of the numbers in the array.
2) Write a method that takes a full array of double as a parameter and for this one, I should refer to this as arraySD. The method of arraySD is to determine and return the standard deviation of numbers in the array.
3) Write a method that takes a full array of double as a parameter however the method here requires me to calculate and return how many numbers in the array are above average and for that, the method is called aboveAverage.
4) Write a method that will take two parameters and both of them are arrays of double for I will refer to them as One and Two. They don't have to be the same size but the method should calculate the standard deviation for each of the two arrays and then it should compare the results. If the standard deviation of array "One" is more, then it is returned under the Boolean value as true but if it isn't, it's false. This method is named firstBigger.

Here is what I have so far and at this point, I really don't know what to do. I'm trying my best to find out what the heck I should do and it's driving me nuts. I've taken Java before but not like this so it has been more than a year since I had Java classes.

public class ArrayStatisticsMethods
{

	public static double arrayMean (double getMean)
	{
	return getMean;
	}
	
	public static double arraySD (double getSD)
	{
	return getSD;
	}
	
	public static double aboveAverage (int average)
	{
	return average;
	}
	
	public static double firstBigger(double[] One, double[] Two)
	{
		StatCalculator s = new StatCalculator();
		for(int i = 0; i < One.length;i++)
		{
			s.putNumber(One[i]);
		}
			return s.getMean();
	}
	
}

If anyone can help me, it would be greatly appreciative. I'll update if anything comes up.

Recommended Answers

All 16 Replies

Well. . you said you've taken Java before. Do you know what an array is? And what a parameter is?

Write a method that takes a full array of double as a parameter

public static double arrayMean (double getMean)

Your arrayMean method takes a double as a parameter. It is supposed to take an array of doubles as a parameter. Hint: the "main" method that you are probably used to seeing (as it has to be there to run a java program) takes a String array, declared as String[] args. Now apply that concept to your double.

Well. . you said you've taken Java before. Do you know what an array is? And what a parameter is?

public static double arrayMean (double getMean)

Your arrayMean method takes a double as a parameter. It is supposed to take an array of doubles as a parameter. Hint: the "main" method that you are probably used to seeing (as it has to be there to run a java program) takes a String array, declared as String[] args. Now apply that concept to your double.

An array is a special kind of object used to store a collection of data. A parameter is a variable that are listed as part of a method declaration.

I know what the main method is which is "public static void main(String[] args)". I applied this sort of concept and I have it known as the following. This is an example.

public static void arrayMean(double[] getMean)

I want to work through this one step at a time so I can hopefully get the idea.

to GeorgeH1: I don't get it. Can you be specific?

Yeah, you have the method header correct now. But you seem to be confused by something else: you're calling your variables "getMean" (etc). Do you realize that your method can return any double value if it is declared as returning a double? It does not have to return the value that you originally passed in. For example,

public static double kylesMethod(double[] dubs){
double someNewDouble = 0.0;
if (dubs.length > 0){
someNewDouble = dubs[0];
}
return someNewDouble;
}

Yeah, you have the method header correct now. But you seem to be confused by something else: you're calling your variables "getMean" (etc). Do you realize that your method can return any double value if it is declared as returning a double? It does not have to return the value that you originally passed in. For example,

public static double kylesMethod(double[] dubs){
double someNewDouble = 0.0;
if (dubs.length > 0){
someNewDouble = dubs[0];
}
return someNewDouble;
}

I think that example seems to work. I am able to do it with arrayMean and arraySD. Now for aboveAverage which requires me to calculate and return how many numbers in the array that are above average.

This is what I did.

public static double arrayMean(double[] Mean)
	{
		double getMean = 0.0;
		if (Mean.length > 0)
		{
			getMean = Mean[0];
		}
		return getMean;
	}
	
	public static double arraySD (double[] SD)
	{
		double getSD = 0.0;
		if (SD.length > 0)
		{
			getSD = SD[0];
		}
		return getSD;
	}

I think you are either naming things with non-descriptive names or you don't understand some concepts. Normally a getMean or getStandardDeviation function will look like this:

public static double getSD (int array[])
    {
        double sd;
        // fill in sd here using array[]
        return sd;
    }

    public static double getMean (int array[])
    {
        double mean;
        // fill in mean here using array[]
        return mean;
    }

The calls from main would be something like this. Assuming array[] is an already filled in array of integers.

double mean = ArrayStatisticsMethods.getMean (array);
double sd = ArrayStatisticsMethods.getSD (array);

You shouldn't have a VARIABLE name with the word "get" in it. get will be part of the function name. In fact, since the word "get" generally refers to something else in Java (a public function that returns a private class data member), you may want to replace "get" with "calc" so it's more obvious what you are doing.

public static double calcSD (int array[])
    {
        double sd;
        // fill in sd here using array[]
        return sd;
    }

    public static double calcMean (int array[])
    {
        double mean;
        // fill in mean here using array[]
        return mean;
    }

Calls from main would be:

double mean = ArrayStatisticsMethods.calcMean (array);
double sd = ArrayStatisticsMethods.calcSD (array);

If you name your functions with good names, it's more clear what you are trying to do. For example, it's hard to comment on whether kylesmethod is written correctly since we don't know what it's supposed to do.

arrayMean is a method that should let me return the mean of numbers in an array.
I used getMean as a variable which can help inside arrayMean and then I set it at Mean while returning getMean.
The same goes for getSD which I set it as a variable which can help inside arraySD or arrayStandardDeviation and then I set it as SD while returning getSD.
I'm now trying to figure out what to do for aboveAverage as well as firstBigger. Can you assist because my mind at the moment is blank.

Your functions which calculate the mean and standard deviation aren't going to work. Both these functions use arrays, but you are only looking at the first element of the array. You need to look at the whole array. Unless there's new code that you have not posted. I don't see how you can write these functions without using a loop somewhere.

You are moving on to the aboveAverage and the firstBigger functions, but there are still problems with what you've completed.

public class ArrayStatisticsMethods
{
	public static double arrayMean(double[] Mean)
	{
		double getMean = 0.0;
		for (int i = 0; i < Mean.length;i++)
		{
			
			getMean = Mean[0];
		}
		return getMean;
	}
	
	public static double arraySD (double[] SD)
	{
		double getSD = 0.0;
		for (int i = 0; i < SD.length;i++)
		{
			
			getSD = SD[0];
		}
		return getSD;
	}
	
	public static void aboveAverage (double HowMany)
	{
	double getHowMany = 0.0;
	
	}
	
	public static double firstBigger(double[] One, double[] Two)
	{
	StatCalculator s = new StatCalculator();	
	}
}

For arrayMean and arraySD, I added a for loop in the hopes that I can try to step through an array. I don't want to offend anyone but this is so confusing.

You aren't offending anyone and we're perfectly happy to help. I'm just at a loss as to how I can help.

Your arrayMean method shows that you are confused about how to access the indexes of an array. Read this then read this. If you have any questions about arrays or for loops after reading those, please let us know and we'll answer any questions. What you need to do is add up everything in the array. So you can use the index, i, of the for loop. In the for loop, add the value of the array at index i to your double variable.

It would be useful to revisit the original problem statement. I hadn't actually read it when I commented. I had just commented on the last few posts because I saw some things that I had a comment on. Here's your statement from post 1:

I'm to create a class known as ArrayStatisticsMethods and in this class, there are definitions to four different "public static" methods which will be described. I'm provided the use of a problem known as StatCalculator which will be provided as well for it isn't modified in any way. The parameters for the methods are arrays and we refer to the array as full if something is assigned to every index in the array. Java can allow arrays to be of size 0 and for what I've been asked, there is no meaningful output and I can assume that arrays are not empty.

The instructions are as follows.
1) Write a method that takes a full array of double as a parameter and I'm to refer to this method as arrayMean. What it should do is to return the mean of the numbers in the array.
2) Write a method that takes a full array of double as a parameter and for this one, I should refer to this as arraySD. The method of arraySD is to determine and return the standard deviation of numbers in the array.
3) Write a method that takes a full array of double as a parameter however the method here requires me to calculate and return how many numbers in the array are above average and for that, the method is called aboveAverage.
4) Write a method that will take two parameters and both of them are arrays of double for I will refer to them as One and Two. They don't have to be the same size but the method should calculate the standard deviation for each of the two arrays and then it should compare the results. If the standard deviation of array "One" is more, then it is returned under the Boolean value as true but if it isn't, it's false. This method is named firstBigger.

There's a lot to this and you need to not try to tackle it all at once. ignore the standard deviation, firstBigger, and aboveAverage functions for now. Getting a program that creates an array of doubles, passes that array to a function that calculates the mean, storing that calculation in a variable, then displaying it is enough to keep you busy for now.

There is something that is given to you that you haven't posted here as far as I can tell:

I'm provided the use of a problem known as StatCalculator which will be provided as well for it isn't modified in any way.

I don't know what this sentence means. Can I replace the word "problem" with the word "class"?

The spec doesn't make any mention of a main fucntion, but can we assume you are to write one? There must be a spec that you have been given. You have rephrased it in your own words, but since we don't know what StatCalculator is, it's hard to help.

Sure you can do that.
Here's the code for StatCalculator which has been provided and I'm not to modify it or edit it in any way. I'm to use it as it is.

public class StatCalculator {
	
	private int howMany = 0;
	private double sum = 0;
	private double squareSum = 0;
	
	/**
	 * Updates statistics with one more number.
	 * 
	 * @param x next number entered
	 */
	public void putNumber(double x){
		squareSum = squareSum + x*x;
		sum += x;
		howMany++;
	}
	

	/**
	 * 
	 * @return how many numbers entered
	 */
	public int getHowMany(){
		return howMany;
	}
	
	/**
	 * 
	 * @return average of numbers entered
	 */
	public double getMean(){
		return sum/howMany;
	}
	
	/**
	 * 
	 * @return standard deviation of numbers entered
	 */
	public double getSD(){
		double a = squareSum/howMany;
		double b = sum/howMany;
		return Math.sqrt(a - b*b);
	}

}

Here's a skeleton you can use. You don't need to calculate anything. That's provided for you already with the class you've been given. Add numbers to this class, then call its methods. You'll do this in YOUR functions, then call ITS functions:

public static double arrayMean (double array[])
{
        StatCalculator sc = new StatCalculator ();
        sc.putNumber (4.0);
        sc.putNumber (6.0);
        sc.putNumber (8.0);
        double mean = sc.getMean();
        return mean;
}

Change it so that it actually uses array[] instead of 4.0, 6.0., 8.0. Write a main function that initializes and puts numbers into the array, then call the functions and display the results.

After much thought, this is my code so far and I'm at the verge of going nuts. I had some help along the way but hopefully I want to figure this out so I can get this out of my chest.

/**
 * 
 * @author Jose Vega
 * @version 2/16/2010
 *
 */
public class ArrayStatisticsMethods extends StatCalculator
{
	public static double[] arrayMean(double[] Mean)
	{
		for (int i = 0;i < Mean.length;i++)
		{
		StatCalculator s = new StatCalculator();
		s.putNumber(Mean[i]);		
		}
		return Mean;
	}
	
	public static double[] arraySD (double[] SD)
	{
		for (int i = 0;i < SD.length;i++)
		{
		StatCalculator s = new StatCalculator();
		s.putNumber(SD[i]);
		}
		return SD;
	}
	
	public static void aboveAverage (int[] howMany)
	{
		int count = 0;
		for(int i = 0; i < howMany.length; i++)
		{
			if(howMany[i] > arrayMean)
			{
				count++;
			}
		}
	}
	
	public static double firstBigger(double One, double Two)
	{
		return One.arraySD() > Two.arraySD();
	}
}

I have at the moment three different errors.
The first is... arrayMean can't be resolved. I don't know what's going on but I have everything set.
The second is actually two errors in one in which on One.arraySD() and Two.arraySD() I can't invoke them on the primitive type double. If anyone can help me, it would be beneficial.

arrayMean needs to return a double, not an array. So does arraySD. See my last post. These functions return doubles. You need to also post the calls to these functions. Post everything, including main.

I'll reiterate that I think you should rename your variables. I named them this:

public static double arrayMean (double array[])

I named it array because it is an array of numbers. What you have is perfectly legal, but very confusing:

public static double[] arrayMean(double[] Mean)

There is one mean, not an array of them. The mean is calculated in the function. It isn't passed to the function. The compiler couldn't care less, but it's confusing to anyone reading the code.

I don't know why you are extending the StatCalculator class. I suppose you could and make it work, but I see no reason to do so.

Look at my skeleton:

public static double arrayMean (double array[])
{
        StatCalculator sc = new StatCalculator ();
        sc.putNumber (4.0);
        sc.putNumber (6.0);
        sc.putNumber (8.0);
        double mean = sc.getMean();
        return mean;
}

I see no call to sc.getMean () in your code. There needs to be one. Again, I have no idea what you are trying to do unless you post the code that calls these functions. Regardless, no matter what, functions that calculate means and standard deviations return doubles, not arrays.

i am ready to help you . Let me know how and what?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.