//I need to get an array of scores from the user in an applet, then with the array I need to compute the average, count how many scores were above and below the average, sort the array in ascending order (without arrays.sort) and display it all on the applet.
It's fine that they are all integers instead of floats or doubles.
The average keeps coming out weird. I'll put a number in ten times and then it will give me an average lower than that number. And the below average always shows it gets more than it should.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Practice extends Applet implements ActionListener {
Label prompt1;
TextField input1;
Label prompt2;
TextField input2;
boolean done;
int number;
int computeAverage;
int aboveAverage;
int belowAverage;
int next;
int[] score = new int[10];
//A bubble sort I found on the internet, I don't know how to implement it.
for(int x = 0; x < score.length; x++)
for (int y = 0; y < score.length - 1; y++){
int tempa = score [y];
int tempb = score [y+1];
if(score[y] > score[y+1]){
score[y]=tempb;
score[y+1] = tempa;
}
//My method that gives an average lower than it should be.
int computeAverage(int score[], int length) {
int total = 0;
for (int counter = 0; counter < score.length; counter++)
total += score[counter];
int average = total / score.length;
return average;
}
//This is supposed to count how many score are above the average, but it always gives one to the next method belowAverage even if it shouldn't.
int aboveAverage(int score[], int length) {
int average = (computeAverage(score, length));
int total = 0;
for (int counter = 0; counter < score.length; counter++) {
if (score[counter] >= average) {
total++;
}
}
{
return total;
}
}
//This one always says that there is one below average even if there isn't.
int belowAverage(int score[], int length) {
int average = (computeAverage(score, length));
int total = 0;
for (int counter = 0; counter < score.length; counter++) {
if (score[counter] < average) {
total++;
}
}
{
return total;
}
}
//Normal working textfield and prompt.
public void init() {
prompt1 = new Label("Enter a Score");
add(prompt1);
input1 = new TextField(10);
input1.addActionListener(this);
add(input1);
}
//I need to get my sorted array printed here somehow.
public void paint(Graphics g) {
g.drawString("Enter -1 to quit early", 10, 50);
if (done) {
g.drawString(
"The class average is "
+ (computeAverage(score, score.length)), 10, 90);
g.drawString(
"The # of scores greater than or equal to the average is "
+ aboveAverage(score, score.length), 10, 110);
g.drawString("The # of scores less than the average is "
+ belowAverage(score, score.length), 10, 130);
}
}
public void actionPerformed(ActionEvent e) {
number = Integer.parseInt(input1.getText());
if (number > 100) {
showStatus("Scores over 100 are invalid");
}
if ((number < 0) || (next >= 9)) {
done = true;
input1.setEditable(false);
showStatus("No more scores can be entered");
} else {
score[next++] = number;
}
repaint();
}
}