Results 1 to 6 of 6
  1. #1
    Pedram. is offline Member
    Join Date
    Sep 2016
    Posts
    3
    Rep Power
    0

    Default Average calculation in a separated class

    Hi,
    I want to calculate the average of 4 inputs, I've created "Average" class but there is something wrong with that :


    Java Code:
    package averageinclass;
     
    import java.util.*;
     
    public class AverageInClass {
     
     
     
        public static void main(String[] args) {
     
            Scanner input= new Scanner(System.in);
     
          Average ave= new Average();  
     
            double []x = new double[4];
     
            for (int i = 0; i < 4; i++) {
     
                 x[i]=input.nextDouble();
     
            }
     
        System.out.print("The average of the given numbers is" +Math.ceil(+ave.Average() ) );
     
        } }




    Java Code:
    public class Average {
     
     
     
        public double Average(double []x) {
     
     
     
     
     
            double sum=0;
     
            for (int i = 0; i < x.length; i++) {
     
                sum=sum+x[i];
     
            }
            double average = sum/4;
            return  average;
        }
     
    }
    Last edited by Pedram.; 09-13-2016 at 01:32 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Average calculation in a separated class

    there is something wrong with that :
    Please explain. If there are errors, copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Pedram. is offline Member
    Join Date
    Sep 2016
    Posts
    3
    Rep Power
    0

    Default Re: Average calculation in a separated class

    here is the error :

    Average calculation in a separated class-error.png

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Average calculation in a separated class

    The method called on line 23 requires an argument of an array of doubles.

    Note: The computation of the average on line 18 should use the .length of the array like on line 13, not a hardcoded 4. That would allow the method to compute the average of any sized array.
    Last edited by Norm; 09-13-2016 at 02:28 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Pedram. is offline Member
    Join Date
    Sep 2016
    Posts
    3
    Rep Power
    0

    Default Re: Average calculation in a separated class

    So the problem was the lack of the x argument in the line 23 :
    Java Code:
     System.out.print("The average of the given numbers is" +Math.ceil(+ave.Average(x) ) )
    ;

    I did some modifications and the altered code is :
    Java Code:
    package averageinclass;
      
    import java.util.*;
      
    public class AverageinClass {
      
      
      
        public static void main(String[] args) {
      
            Scanner input= new Scanner(System.in);
      
          Average ave= new Average(); 
          
          System.out.println("Enter 0 as the last number");
          
         int n=1000;
         int j=0;
            double []x = new double[n];
            
            System.out.print("Enter your numbers ");
      
            for (int i = 0; i < x.length; i++) {
      
                 x[i]=input.nextDouble();
                  j++;
                if (x[i]==0) {
                    break;
                }
            }
      
        System.out.println("The average of the given numbers is " +ave.Average(x,j)  );
      
        } }


    Java Code:
    package averageinclass;
    
    public class Average {
      
      
      
        public double Average(double []x, int j) {
      
      
      
      
      
            double sum=0;
      
            for (int i = 0; i <j; i++) {
      
                sum=sum+x[i];
      
            }
            double average = sum/j;
            return  average;
        }
      
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Average calculation in a separated class

    Have you solved your problem now? If not, please explain.

    What is the variable j for? The name of a variable should describe what the variable contains. For example: numberUsed would be a good name for a variable that contained the number of elements in an array that were used to hold data.
    Last edited by Norm; 09-14-2016 at 04:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 14
    Last Post: 03-25-2015, 09:20 AM
  2. Replies: 6
    Last Post: 10-08-2014, 08:47 PM
  3. Replies: 1
    Last Post: 02-17-2012, 03:27 AM
  4. Urgent/ Help with average method in created class
    By johnjacob in forum New To Java
    Replies: 12
    Last Post: 12-07-2010, 02:47 AM
  5. Replies: 0
    Last Post: 04-16-2008, 10:58 PM

Tags for this Thread

Posting Permissions

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