I'm having trouble with the calculate mean method in my Average Driver. The Average class is fine it's just declaring the objects and instantiating them I'm having a problem with. Any help would be greatly appreciated.

import java.util.Scanner;

public class Average {


		Scanner input = new Scanner(System.in);
		private int[] data = new int[5];
		private double mean;

		public Average() {
			System.out.println("Enter 5 digits.");
			for (int i=0; i<data.length; i++) {
				data[i] = input.nextInt();
			}
		}

		public void calulateMean() {
			double sum = 0;
			for(int i=0; i<data.length; i++) {
				sum+=data[i];
			}
			mean = sum/5;
		}

		public String toString() {
			String s = "The average of ";
			for (int i=0; i<data.length; i++)
				s+=data[i] + " ";
			s+= "is " + mean;
			return s;
		}

		public void selectionSort() {
			int maxIndex;;
			int maxValue;;
			for (int i=0; i<data.length-1; i++) {
				maxIndex = i;
				maxValue = data[i];
				for (int index = i + 1; index < data.length; index++) {
					if (data[index] > maxValue) {
						maxValue = data[index];
						maxIndex = index;
					}
				}
				data[maxIndex] = data[i];
				data[i] = maxValue;
			}
		}
	}

My Average Driver class.

public class AverageDriver {
	public static void main(String[] args) {

		Average avg = new Average();

		avg.selectionSort();
		avg.calculateMean();

		System.out.println(avg.toString());
	}
}

Recommended Answers

All 2 Replies

On the surface this looks like it will work, but I (personally) would put all questions outside of the Average class.

Are you supposed to have an array of something OTHER THAN the integers in the class?

What exactly is the problem you are having?

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.