Meg
Meg

Reputation: 25

Error: constructor AverageFINAL in class AverageFINAL cannot be applied to given types;

Objective: Create an AverageDriver class. This class only contains the main method. The main method should declare and instantiate an Average object. The Average object information should then be printed to the console.

I'm having trouble with the creating the object part of the last code. I tried following along with my textbook but I'm still getting an error message.

import java.util.Scanner;

/**
 * The Average class calculates the average of the test 
 * scores entered and puts the scores into descending order.
 */

public class AverageFINAL
{
  private int[] data;


 public int calculateMean()
 {
   int total = 0;
   int mean;

    for (int index = 0; index < data.length; index++)
      total += data[index];
   mean = total / data.length;
   return mean;
 }

 public int selectionSort()
 {
   int startScan, index, minIndex, minValue;

    for (startScan = 0; startScan < (data.length-1); startScan++)
    {
      minIndex = startScan;
      minValue = data[startScan];
      for(index = startScan + 1; index < data.length; index++)
      {
        if (data[index] < minValue)
        {
          minValue = data[index];
          minIndex = index;
        }
      }
      data[minIndex] = data [startScan];
      data [startScan] = minValue;
    }
  return data[startScan];
 }
}

import java.util.Scanner;

public class AverageDriver
{
  public static int main(String[] args)
  {
   int[] data = new int[5];

   AverageFINAL object = new AverageFINAL(data);

   getValues(data);

   System.out.println("The test scores you entered, in descending order, are:");
   System.out.println(object.selectionSort());
   System.out.println("The average is" + object.calculateMean() +".");

  }

  private static int getValues(int[] data)
  {
  Scanner keyboard = new Scanner(System.in);
    for (int index = 0; index < data.length; index++) 
    {
      System.out.println("Enter score #" + (index + 1) + ": ");
      data[index] = keyboard.nextInt();
    }
  keyboard.close();
  }
}

The error message I'm getting is:

1 error found:
File: C:\Users\bryan\Downloads\AverageDriver.java  [line: 9]
Error: constructor AverageFINAL in class AverageFINAL cannot be applied to given types;
  required: no arguments
  found: int[]
  reason: actual and formal argument lists differ in length

Upvotes: 0

Views: 329

Answers (2)

Paco Abato
Paco Abato

Reputation: 4065

You need a constructor with a parameter as you use it in:

AverageFINAL object = new AverageFINAL(data);

Try this:

public class AverageFINAL
{
  private int[] data;
  public AverageFINAL(int[] dataParam) {
    data = dataParam;
  }
etc.

Upvotes: 0

rgettman
rgettman

Reputation: 178293

You don't have any constructor defined in AverageFINAL. In that case, Java creates a default, no-arg constructor.

It's as if this was defined:

public AverageFINAL() {}

However, you are attempting to pass an int[] in this line:

AverageFINAL object = new AverageFINAL(data);

If you want to pass the int[], then you must explicitly create a constructor that takes an int[] as its parameter, and presumably you want to assign that int[] to your instance variable data.

Upvotes: 1

Related Questions