Robert Montz
Robert Montz

Reputation: 213

Average of Int[]; Method not Returning

My task is to calculate the average of the Integers a user defines in an Array and print back to main using the method in my class file.

Currently I have both files to compile but can't seem to figure out why nothing is being printed. Please help, much appreciated!

Snippet of code in Test Program to examine:

// Print average of integers in array.
     System.out.println("\nAverage of values in array =");
     myArray.avgArray();

Snippet of code in Class Program to examine:

// Method to calculate average of integers in array.
  public double avgArray()
  { 
     int sum = 0;

     for(int ctr = 0; ctr < limit; ctr++)
     {
       sum = sum + nrs[ctr];
     }

     double avg = sum /(double)limit;

     return avg;
  }

This code is my program for testing purposes:

import java.util.Scanner;


public class P5test
{
  public static void main(String[] args)
  {
    // Set up Scanner object for keyboard input.
     Scanner keyboard = new Scanner(System.in);

     // Input size of array desired.
     System.out.print("Size of array:   ");
     int size = keyboard.nextInt();

     // Set up object.
     P5class myArray = new P5class(size);

     // Allow user to fill array.
     System.out.println("\nEnter data for array:");
     myArray.fillArray();

     // Print contents of array.
     System.out.println("\nContents of array:");
     myArray.printArray();

     // Print average of integers in array.
     System.out.println("\nAverage of values in array =");
     myArray.avgArray();

     // Print postive values in array.
     System.out.println("\nPositive values in array:");
     myArray.pvaluesArray();

     // Sort contents of array in ascending order.
     System.out.println("\nSorted Array:");
     myArray.sortArray();
     myArray.printArray();
  }
}

This code contains my class with the various methods I'll be implementing to main:

import java.util.Scanner;

public class P5class
{
  // Constructor setting up empty array of size specified by user.
  public P5class(int size)
  {
    limit = size;
     nrs = new int[limit];
  }

  // Method to fill array.
  public void fillArray()
  {
    // Set up Scanner object for keyboard input.
     Scanner keyboard = new Scanner(System.in);

     for(int ctr = 0; ctr < limit; ctr++)
     {
       System.out.print("Integer #" + (ctr+1) + ":  ");
        nrs[ctr] = keyboard.nextInt();
     }
  }

  // Method to display contents of array.
  public void printArray()
  {
    for(int ctr = 0; ctr < limit; ctr++)
     {
       System.out.println("Nrs[" + ctr + "] = " + nrs[ctr]);
     }
  }

  // Method to calculate average of integers in array.
  public double avgArray()
  { 
     int sum = 0;

     for(int ctr = 0; ctr < limit; ctr++)
     {
       sum = sum + nrs[ctr];
     }

     double avg = sum /(double)limit;

     return avg;
  }

  // Method to examine if any positive values are present in array.
  public void pvaluesArray()
  {
    int largest = nrs[0];

    // Check to see if any other number in array is larger than first.
    for(int ctr = 1; ctr < limit; ctr++)
    {
      if(nrs[ctr] > largest)
         largest = nrs[ctr];
    }

    int sum = 0;

    for(int ctr = 0; ctr < limit; ctr++)
    {
      sum = sum + nrs[ctr];
    }

    double average = (double)sum/limit;


    for(int ctr = 0; ctr < limit; ctr++)
    {
      if(largest <= 0)
      {
        System.out.println("\nArray contains no positive integers.");
          break;
      }
      else if (nrs[ctr] > 0)
      {
        System.out.println("Nrs[" + ctr + "] = " + nrs[ctr]);
      }
    }
  }

  // Method to sort array into ascending order.
  public void sortArray()
  {
    for(int ctr0 = 0; ctr0 < limit-1; ctr0++)
     {
       // Make one pass of Bubble Sort.
       for(int ctr = 0; ctr < limit-1; ctr++)
       {
         if(nrs[ctr] > nrs[ctr+1])
          {
            int temp = nrs[ctr];
            nrs[ctr] = nrs[ctr+1];
            nrs[ctr+1] = temp;
          } 
       }
     }
  }

  // Instance variables.
  private int limit;
  private int[] nrs;
}

Here is an example output of the program when Run:

 ----jGRASP exec: java P5test

Size of array:   5

Enter data for array:
Integer #1:  0
Integer #2:  -9
Integer #3:  4
Integer #4:  7
Integer #5:  2

Contents of array:
Nrs[0] = 0
Nrs[1] = -9
Nrs[2] = 4
Nrs[3] = 7
Nrs[4] = 2

Average of values in array =

Positive values in array:
Nrs[2] = 4
Nrs[3] = 7
Nrs[4] = 2

Sorted Array:
Nrs[0] = -9
Nrs[1] = 0
Nrs[2] = 2
Nrs[3] = 4
Nrs[4] = 7

 ----jGRASP: operation complete.

Upvotes: 0

Views: 1749

Answers (1)

Bernhard Barker
Bernhard Barker

Reputation: 55609

It's returning a value, but you're not doing anything with it, try:

System.out.println("\nAverage of values in array = " + myArray.avgArray());

Or:

double avg = myArray.avgArray();
System.out.println("\nAverage of values in array = " + avg);

Or one of many other ways.

Or, instead of return avg;, you can simply print it out in the method with System.out.println(avg); and change the return type to void.

Upvotes: 3

Related Questions