We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,642 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Declare and Instantiate Objects: Arrays

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());
	}
}
3
Contributors
2
Replies
10 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
smash92
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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?

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

What exactly is the problem you are having?

JamesCherrill
... trying to help
Moderator
8,682 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0721 seconds using 2.88MB