Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with arrays. Computing averages, sorting the array,

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with arrays. Computing averages, sorting the array,

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

    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with arrays. Computing averages, sorting the array,

    When posting code, please use the highlight tag to preserve formatting.

    Your first step is to figure out which part of this fails. Step through this with a debugger, or at least add some print statements, to figure that out. You have to test each part of your program in isolation instead of giving us a whole big mess and saying that it doesn't work.

    Why not use Arrays.sort() instead of trying to implement an algorithm you aren't even sure works?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Arraylist of averages of two arrays list
    By Hazmat210 in forum Collections and Generics
    Replies: 11
    Last Post: April 5th, 2012, 07:38 PM
  2. Need helpt with sorting arrays?
    By xion0374 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2012, 06:00 PM
  3. Sorting Arrays
    By Bryan29 in forum Collections and Generics
    Replies: 5
    Last Post: November 28th, 2011, 07:21 AM
  4. Digital Watch program and Sorting arrays
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 5th, 2011, 05:21 PM
  5. Computing Arrays...
    By sarahmiller1980 in forum Collections and Generics
    Replies: 1
    Last Post: January 31st, 2011, 07:50 AM

Tags for this Thread