Results 1 to 11 of 11
  1. #1
    wxboy911 is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Help with Code-Just not getting it....

    Hi, I am new to java and working on a homework assignment that won't compile. I am getting the error "cannot find symbol....Symbol: method calculateMean() in the location: class Average. I know this is something obvious that I am overlooking-I have been working on this for far too long. Here is the code...



    import java.util.Scanner;//enables the scanner for user input



    Java Code:
    public class Average
    { 
      private int [] data;//
      double mean;//variable for the average of the test scores
      int score;//score variable for the 
      double AverageScore;//
    /**
     * Thid method calculates the average or mean of the contents 
     * of the array.
     * */
      
    
    {
      
        data = new int[5];//our array with slots 0 through 4
        Scanner keyboard=new Scanner(System.in);
        
        for(int score = 0; score < data.length;score++);//storing test scores in the array
           {
             
          //requesting user input for the scores.
          System.out.print("Please enter  your test score" + (score + 1) +  ":");
          data[score] = keyboard.nextInt();
        }
      }
      /**
       * This method calculates the average or mean of the test scores that have
       * been entered by the user. 
       * */
        public void caclulateMean()
      {
        int score;
        double sum = 0;
        
        for (score = 0; score < data.length; score++)
        //{
          sum=sum + data[score];
        //}
        mean= (double)sum / (data.length);//operation of dividing the sum by the length of the array.
      }
    
      
      /**
       * The selectionSort method reads the data in the array and organizes it from highest to lowest.
       * */
      
      public void selectionSort()//method to rearrange the array from highest to lowest.
      {
        int maxIndex;//Variable for the organized data to be stored in.
        int maxValue;//Variable for the highest value in the array.
           
        for (int startScan = 0; startScan < data.length-1; startScan++)
        {
          maxIndex = startScan;
          maxValue = data[startScan];
          
          for (int index = startScan + 1; index < data.length; index++)
          {
             if (data[index] > maxValue)
               {
                  maxValue = data[index];
                  
                  maxIndex = index;
                  
          }
             }
    
             data[maxIndex] = data[startScan];
             
             data[startScan] = maxValue;
             }
    }
    
    
        public String toString()//returns the string of scores in decending order
          
        {
          String output;
          
          output="The test scores you provided in decending order are \n";
         
          
          for (int score = 0; score  < data.length;score++);
          {
          output=output + data[score] + " ";
        }
          output = output + "\nThe average of your scores is " + mean;
          
          return output;
          //System.out.println(calculateMean());
        }
        
    
        public static void main(String[] args)
        {
          //Average AverageScore = new Average();
          
          selectionSort();
          
          calculateMean();
          
          System.out.println(AverageScore);
        }
       }
    Any help would be greatly appreciated.
    Last edited by wxboy911; 05-05-2015 at 04:06 PM. Reason: fixed the code tags (not angular brackets but square brackets)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Just not getting it....

    You can edit your post to fix your code formatting by putting CODE tags around your code.

    Why did you comment out your main method?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    wxboy911 is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Re: Just not getting it....

    sorry about the code tags- I am shooting in the dark right now. I noticed that alleviated the error I was getting about the selectionSort() method(non-static mehtod cannot be referenced from static context.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Just not getting it....

    Right, that error message says it all- you're trying to call a non-static method from the static main() method, which you can't do. I recommend reading up on the difference between static and non-static.

    By the way, the code tags use square brackets, not angle brackets.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    wxboy911 is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Re: Just not getting it....

    Thanks- I will give that a look. Hoping to get this debugged by the due date Wednesday.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Just not getting it....

    The main() method is static, which means it doesn't belong to a particular instance. Instead, it belongs to the Average class.

    However, the other methods are non-static, which means they belong to particular instances of the Average class. In other words, you have to create an instance of the Average class in order to use them.

    It looks like you did create an instance (by using the new keyword), but you've commented that line out.

    Think about it this way: you're creating an instance of Scanner, and you're storing that instance in the keyboard variable. You then use that variable to call the non-static nextInt() function. You have to say keyboard.nextInt()- you can't just call nextInt(), because you need to use the instance of Scanner.

    Your Average class is similar. You have to create an instance of it, and then use that instance, in order to call the non-static methods of that class.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    wxboy911 is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Re: Just not getting it....

    So if I put line 96 back in and change lines 98 and 100 to include the calls AverageScore.selectinSort(): and AverageScore.calculateMean(); my code may work....I am at work now so I can't test anything.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Just not getting it....

    Quote Originally Posted by wxboy911 View Post
    Just not getting it....
    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Just not getting it....

    Quote Originally Posted by wxboy911 View Post
    So if I put line 96 back in and change lines 98 and 100 to include the calls AverageScore.selectinSort(): and AverageScore.calculateMean(); my code may work....I am at work now so I can't test anything.
    What happened when you tried that?

    (by the way, you might want to use standard naming conventions and start methods and variables with lower-case letters)
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  10. #10
    wxboy911 is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Re: Just not getting it....

    Thanks for all the help-The code now compiles! I still have an issue with the output. The program computes the average and displays the test scores after the first score is entered.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Just not getting it....

    Possibly you're running into the same problem this person had:

    http://www.java-forums.org/new-java/...-nextline.html
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •