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 9 of 9

Thread: need help with a function...

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

    Default need help with a function...

    Hi

    I need some help creating a function that will check if each element in the array "contestantScore[]" which is defined from user input, is less or more than the variable "average" which is defined from the sum of the array "contestantScore[] / contestantScore.length" and then returns either the String "above" or "below"

    I have racked my brain now for hours trying to create a function that will do this, going over course material etc....

    I'm not asking anyone to write the code but a point in the right direction would great.

    I'll put my code up so far so you can see what I'm doing : )

    The function I have up the top of my code doesn't work and returns errors...

    "average cannot be resolved into a variable"

    "contestantScore cannot be resolved into a variable"

    Sorry about the formatting, it looked better in eclipse : (

    import java.util.Scanner;

    public class pracQ3_2
    {
    //I need a function that will return the String "above" or "below" if each element in the array is less or more than the average.

    public String aboveOrBelow ()
    {
    String above;
    String below;

    if (contestantScore < average)
    {
    return below;
    }
    else
    {
    return above;
    }
    }

    public static void main(String[] args)
    {
    int [] contestantNum;
    double [] contestantScore;
    int index;
    double average;

    Scanner keyboard = new Scanner(System.in);

    contestantNum = new int [10];

    contestantNum[0] = 1;
    contestantNum[1] = 2;
    contestantNum[2] = 3;
    contestantNum[3] = 4;
    contestantNum[4] = 5;
    contestantNum[5] = 6;
    contestantNum[6] = 7;
    contestantNum[7] = 8;
    contestantNum[8] = 9;
    contestantNum[9] = 10;

    contestantScore = new double [10];

    for (index = 0; index < 10; index = index + 1)
    {
    System.out.print("Enter score for contestant number " + contestantNum[index] + ": ");
    contestantScore[index] = keyboard.nextDouble();
    }

    System.out.println();

    for (index = 0; index < 10; index = index + 1)
    {
    System.out.println("Contestant Number " + contestantNum[index] + " rated " + contestantScore[index] + " out of 10");
    }

    average = (contestantScore[0] + contestantScore[1] + contestantScore[2] + contestantScore[3] + contestantScore[4] + contestantScore[5] + contestantScore[6] + contestantScore[7] + contestantScore[8] + contestantScore[9]) / contestantScore.length;

    System.out.print(average);

    }

    }
    Last edited by fallout87; October 31st, 2011 at 03:22 PM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: need help with a function...

    you has not declared these two variables in the method of aboveOrBelow

    your method could be modified as followling, than called in the main method:

    public static String aboveOrBelow (double score, double average)
        {
            String above = "above";
            String below = "blow";
     
            if (score < average)
            {
                return below;
            }
            else
            {
                return above;
            }
        }

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: need help with a function...

    Or just get rid of the variables and use String literals.
    Improving the world one idiot at a time!

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: need help with a function...

    public String aboveOrBelow ()
    {
    String above;
    String below;
     
    if (contestantScore < average)
    {
    return below;
    }
    else
    {
    return above;
    }
    }

    Does your program compile????
    What are the contestantScore and average variables for this function? This function doesn't even know anything about them. It's scope is not in this function.
    EDIT: And also, contestantScore is not a variable but the array of integer.
    Last edited by Mr.777; November 1st, 2011 at 03:34 AM.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with a function...

    Hi

    Thanks for the replies...

    I'm trying to print if the contestantScore array elements are above or below the average using a function. I know I have to pass the contestantScore array elements and average variable into the function but can't figure out how to do this.

    I have also fixed the problem with declaring the string variables in the function. Thanks.

    So my now looks like this:

    public String aboveOrBelow (double [] contestantScore, double average)
    {
    String above = "above";
    String below = "below";

    if (contestantScore[index] < average)
    {
    return below;
    }
    else if (contestantScore[index] > average)
    {
    return above;
    }
    }

    Am I on the right track here???

    "[index]" comes up as an error saying "index cannot be resolved into a variable" Any suggestions? This is where I want the program to check each element of the array..

    Thanks again for the replies : )

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: need help with a function...

    What is index? From where index value is initiated?
    Where is index declared?
    You must read the beginners tutorials first. Coz you have a lack of understanding about declaration, initialization and their usage as well as scope.

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with a function...

    Have done a re-vamp of my code, first I'll put up what happens when you run the code then after that will be the actual code in eclipse.
    -----------------------------------------------------------------------------------------
    Enter score for contestant number 1: 1
    Enter score for contestant number 2: 2
    Enter score for contestant number 3: 3
    Enter score for contestant number 4: 4
    Enter score for contestant number 5: 5
    Enter score for contestant number 6: 6
    Enter score for contestant number 7: 7
    Enter score for contestant number 8: 8
    Enter score for contestant number 9: 9
    Enter score for contestant number 10: 10

    Contestant Number 1 rated 1.0 out of 10, they were above average
    Contestant Number 2 rated 2.0 out of 10, they were above average
    Contestant Number 3 rated 3.0 out of 10, they were above average
    Contestant Number 4 rated 4.0 out of 10, they were above average
    Contestant Number 5 rated 5.0 out of 10, they were above average
    Contestant Number 6 rated 6.0 out of 10, they were above average
    Contestant Number 7 rated 7.0 out of 10, they were above average
    Contestant Number 8 rated 8.0 out of 10, they were above average
    Contestant Number 9 rated 9.0 out of 10, they were above average
    Contestant Number 10 rated 10.0 out of 10, they were above average
    Average = 5.5
    -----------------------------------------------------------------------------------------
    import java.util.Scanner;

    public class pracQ3_2
    {
    public static void main(String[] args)
    {
    //variables
    int [] contestantNum;
    double [] contestantScore;
    int index;
    double average;
    String aboveOrBelow = "";

    Scanner keyboard = new Scanner(System.in);

    //Defining each element in contestNum Array
    contestantNum = new int [10];

    contestantNum[0] = 1;
    contestantNum[1] = 2;
    contestantNum[2] = 3;
    contestantNum[3] = 4;
    contestantNum[4] = 5;
    contestantNum[5] = 6;
    contestantNum[6] = 7;
    contestantNum[7] = 8;
    contestantNum[8] = 9;
    contestantNum[9] = 10;

    contestantScore = new double [10];

    //loop for defining the contestantScore elements
    for (index = 0; index < 10; index = index + 1)
    {
    System.out.print("Enter score for contestant number " + contestantNum[index] + ": ");
    contestantScore[index] = keyboard.nextDouble();
    }

    //calculating the Variable average
    average = (contestantScore[0] + contestantScore[1] + contestantScore[2] + contestantScore[3] + contestantScore[4] + contestantScore[5] + contestantScore[6] + contestantScore[7] + contestantScore[8] + contestantScore[9]) / contestantScore.length;

    //loop for defining the String variable "aboveOrBelow"


    if (contestantScore[index] < average)
    {
    aboveOrBelow = "below";
    }
    else if (contestantScore[index] > average)
    {
    aboveOrBelow = "above";
    }


    System.out.println();

    //loop for displaying "Contestant Number --- rated --- out of 10, they were above/below average" for each contestant
    for (index = 0; index < 10; index = index + 1)
    {
    System.out.println("Contestant Number " + contestantNum[index] + " rated " + contestantScore[index] + " out of 10, " + "they were " + aboveOrBelow + " average");
    }

    //This is just testing what the average is and won't be in the final program
    System.out.println("Average = " + average);
    }

    }

    ------------------------------------------------------------------------------------------

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: need help with a function...

    Why don't you put this code
    if (contestantScore[index] < average)
    {
    aboveOrBelow = "below";
    }
    else if (contestantScore[index] > average)
    {
    aboveOrBelow = "above";
    }
    inside this
    for (index = 0; index < 10; index = index + 1)
    {
    System.out.println("Contestant Number " + contestantNum[index] + " rated " + contestantScore[index] + " out of 10, " + "they were " + aboveOrBelow + " average");
    }

    Like,
    for (index = 0; index < 10; index = index + 1)
    {
    if (contestantScore[index] < average)
    {
    aboveOrBelow = "below";
    }
    else if (contestantScore[index] > average)
    {
    aboveOrBelow = "above";
    }
     
     
    System.out.println("Contestant Number " + contestantNum[index] + " rated " + contestantScore[index] + " out of 10, " + "they were " + aboveOrBelow + " average");
    }
    EIDT: And i will recommend you to study the basics.

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with a function...

    Doesn't matter.

    Figured it out now.

    Thanks anyway : )

Similar Threads

  1. function timeDiff
    By bobby321 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 25th, 2011, 02:23 PM
  2. About the ackermann function...
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 09:24 AM
  3. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  4. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 10:29 AM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM