Tags: appreciate, basic, calculating, compensate, essentials, function, greatly, howeverthe, java, median, mode, program, write
calculating mean, median and mode
On Java Studio » Java Essentials
5,739 words with 6 Comments; publish: Mon, 24 Sep 2007 21:17:00 GMT; (15085.94, « »)
If anyone could write this program and give me tips, I would greatly appreciate it and would compensate however:
The basic function of this program is to accept data from the user to represent examination scores and to process statistics about those scores. As long as the user inputs scores, the program will continue to accept more scores. There should be NO PROMPT for a score. Any score over 100 or less than 0 will be ignored by the program. The user can put in as many as 1,000,000 pieces of valid data. To do this, you will need an array of size 1,000,000 and you will need to use a while or a do-while loop to read in the data in to this array. As soon as the user puts in ctrl-Z (end-of-file), or 1,000,000 pieces of data, the program stops accepting input and processes the data. The program will then compute the mean, median, mode, and standard deviation of the valid data entered, and print these statistical values.
For this program, you are required to create and use three meaningful static functions. These functions are of your own design, but there are some very natural tasks in this project that are suitable for functions. These tasks are: accepting input, computing the mean, computing the median, computing the mode, and printing the output. Each of these is best done with a function. Although you are not required to have these specific functions, use of functions is required. You are welcome to use the 搒ort?function presented in class as one of these functions.
http://javaessentials.developerfaqs.com/q_java-essentials_7148.html
All Comments
Leave a comment...
- 6 Comments

I think your instructions are quite clear. write a (do-)while -loop that runs until
user presses CTRL-Z or 1 000 000 pieces of data have been entered. Check the
data correction inside the loop, and put the correct data in an array. After the loop
ends calculate the median, mode, etc. And print the answers to user.
Don't except everything do be done for you. You got to put in some effort. If you have
some specific problem with YOUR solution, ask about that.
kari-matti
#1; Sat, 14 Jul 2007 00:26:00 GMT

this is what I have so far, but I am a novice and don't quite know how to organize my functions, or write the do-while loop so that the correct data gets put into a seperate array. i also have to create a new array for the mode, to keep track of how often each valid data point is occuring- any thoughts? again, anything is greatly appreciated
import java.util.Scanner;
class Statistics
public static void main (String[] args) {
{Scanner keyboard = new Scanner (System.in);
System.out.println("Enter data [0-100] on new lines");
System.out.println("Enter CTRL-Z to process data");
System.out.println("Accepts up to 1,000,000 valid data values");
int[] StatArray = new int[1000000]
int ctr = 0;
//while loop for counter
while (keyboard.hasNext() && ctr < 1000000) {
StatArray[ctr] = keyboard.nextint();
ctr++;
}
sort(StatArray, ctr);
for (int i = 0; i < ctr; i++)
System.out.print(fred + " ");
}
public static void sort(double[] a, int numberUsed) {
int indexOfNextSmallest;
double temp;
for (int ctr = 0; ctr < numberUsed; ctr++) {
indexOfNextSmallest = ctr;
for (int ctr1 = ctr+1; ctr1 < numberUsed; ctr1++) {
if (a[ctr1] < a[indexOfNextSmallest]) {
indexOfNextSmallest = ctr1;
}
}
temp = a[ctr];
a[ctr] = a[indexOfNextSmallest];
a[indexOfNextSmallest] = temp;
}
}
}
int x, y, z;
int a;
int u, g;
u = (x + y + z) / 3.0;
System.out.println ("Average =" +u);
g = Math.sqrt((((x - u)*(x - u)) + ((y - u)*(y - u)) + ((z - u)*(z - u)))/ctr);
System.out.println ("Standard Deviation =" +g);}}
\\Mode Function, using frequency:
public class Array6 {
public static void main (String[] args) {
int[] StatArray = {0, 1, 1, 2, 0, 2, 2, 1};
int[] frequency = new int[101];
for (int ctr = 0; ctr < StatArray.length; ctr++)
frequency[StatArray[ctr]]++;
for (int ctr = 0; ctr < 3; ctr++)
System.out.printf("%d occurs %d times in fred\n", ctr, frequency[ctr]);
}
}
do
while (i<=100 || i<=0);
#2; Sat, 14 Jul 2007 00:26:00 GMT

Hi,
You have to correct your brackets and also at the beginning you are supposed to put
public class Statistics {
There is no bracket before
Scanner Keyboard = new Scanner(System.in);
also i dont think the program will allow you to use [ ] inside the statement
System.out.println("Enter data [0-100] on new lines")
no brackets in the middle come up with something else :P
and also use the code bar so that it can be seen easier :)
Regards
#3; Sat, 14 Jul 2007 00:26:00 GMT

You also have two main methods in your program, one in Statistics class, and one in Array6 class.
This is how you would do printing in C, I don't know whether it works in Java, propably not.
for (int ctr = 0; ctr < 3; ctr++)
System.out.printf("%d occurs %d times in fred\n", ctr, frequency[ctr]);
instead do it like this
for (int ctr = 0; ctr < 3; ctr++)
System.out.println(ctr+" occurs "+frequency[ctr]+" times in fred\n");
kari-matti
Message was edited by:
kari-matti
Message was edited by:
kari-matti
#4; Sat, 14 Jul 2007 00:26:00 GMT

- Does the code you posted compile? Run?%#5; Sat, 14 Jul 2007 00:26:00 GMT

- I'd recommend not having an array of one million entries. Calculate those values on the fly.%#6; Sat, 14 Jul 2007 00:26:00 GMT