Here is the Homework question...


This is another project involving static methods and arrays. Once again, static methods are covered in the text in Chapter 5. Methods with arrays as parameters are covered in Chapter 6 sections 4 and 5. (This applies to editions 6 and 7.)

1. Write a method that takes an array of double as a parameter. The array is assumed full, there is a number for every index. Your method should return how many numbers (int) in the array are above average. The method should be defined in a class called MoreArrayMethods. The method should be called howMany.
2. Write a method that takes two parameters, both arrays of double; let us call the arrays one and two. The arrays don't have to be the same size. It is assumed that both arrays are full. Your method should calculate the mean for each of the two arrays. Then it should compare the means. If the mean of array one is bigger, your method should return the boolean value true, and otherwise it should return false. This method should be called firstBigger, and should also be defined in the class MoreArrayMethods.
3. Write a main (or two mains) that test your methods out. Don't submit this testing method, I will use my own.

Here is the StatCalculator that must be called into the main:

public class StatCalculator {
	private int howMany = 0;
	private double sum = 0;
	private double squareSum = 0;
	
	/**
	 * Adds 1 to count of numbers entered.
	 * 
	 * @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 is what I have so far: (Please help with the me with the methods first being that this is what I have to send in. Thank You)

public class MoreArrayMethods {
	
	
	
	public static double howMany(int [] one){
		
		int count = 0;
		
			StatCalculator s = new StatCalculator();
			
			for(int i = 0; i < one.length; i++)
				
				s.putNumber(one[i]);
			
			return s.getMean();
		 
		if (one[i] > s.getMean()){
			
			count++;
		}
}

	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();
		
		for(int i = 0; i < two.length; i++){
			
			s.putNumber(two[i]);
		
		}return s.getMean();
	 

			 
		if (one.s.getMean > two.s.getMean ){
			
			System.out.println("true");
		
		}
		
		else{ 
			System.out.println("false");
		}
	
	}
	
	
}

Here is the main:

public class TestingMethod {

	
	
	public static void main(String[] args) {
		
		double[] one = {14, -9, 7, 22, 99};
		
		double[] two = {5, -45, 18, 27, 100, 0};
		
		System.out.println(MoreArrayMethods.getMean(one));
	    	    
	System.out.println(MoreArrayMethods.getMean(two));
	    
	

	}

}

Recommended Answers

All 5 Replies

Use code tags. Click the button: "#" when creating a post

If you would like us to help you; as javaAddict said, use code tags.

And have a clear question that you need help with exactly, your not expecting us to do your homework for you, are you?

Try putting it in code. Here's how you should do it.

public class StatCalculator {
private int howMany = 0;
private double sum = 0;
private double squareSum = 0;

/**
* Adds 1 to count of numbers entered.
*
* @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);
}



}
public class MoreArrayMethods {



public static double howMany(int [] one){

int count = 0;

StatCalculator s = new StatCalculator();

for(int i = 0; i < one.length; i++)

s.putNumber(one[i]);

return s.getMean();

if (one[i] > s.getMean()){

count++;
}
}

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();

for(int i = 0; i < two.length; i++){

s.putNumber(two[i]);

}return s.getMean();



if (one.s.getMean > two.s.getMean ){

System.out.println("true");

}

else{
System.out.println("false");
}

}


}

I'm having de ja vu: http://www.daniweb.com/forums/thread262065.html

This code is much better than the one I linked. The highlighted line is wrong. Try declaring two StatCalculators (s1 and s2?). You can't call functions on a double in java.

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();
		
		for(int i = 0; i < two.length; i++){
			
			s.putNumber(two[i]);
		
		}return s.getMean();
	 

			 
		if ([B]one.s.getMean > two.s.getMean[/B] ){
			
			System.out.println("true");
		
		}
		
		else{ 
			System.out.println("false");
		}
	
	}
	
	
}

or try reading the date ..
it was homework of september 2009, I doubt he still cares

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.