Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor in class cannot be applied to given types

Tags:

java

I ve got the following code using arrays to find some prim numbers. However, when trying to compile my user class PalindromeArrayUser it says - "Constructor in class cannot be applied to given types"

required: int. found: no arguments. reason: actual and formal arguments lists differ in length.

However, I have passed to the constructer an int value (the same way it was designed in my blueprint). I don't quite get where the problem comes from. Thanks.

Here are my two classes

 public class PalindromeArray 
 {

int arrLength;

public PalindromeArray(int InputValue) 
{
    arrLength = InputValue;
}


int arr[] = new int[arrLength];
boolean check[] = new boolean [arrLength];


public void InitializeArray()  
{

    for (int k = 2; k < arr.length; k++)
    {
        arr[k] = k;
        check[k] = true;

    }   
}

public void primeCheck()  
{

    for (int i = 2; i < Math.sqrt(arr.length - 1); i++ )
    {
        if (check[i] == true)
        {
        for (int j = 2; j < arr.length; j++)
          {
            if (j % i == 0)
                {
                     check[j] = false;
                     check[i] = true;
                }
          }
        }   

    }   
}

public void PrintArray() 
{
    for (int k = 2; k < arr.length; k++)
    {
        if ((!check[k]) == false)
            System.out.println(arr[k]);

    }
}

   }

And this is my User class where the problem comes from. The class above compiles fine.

 import java.io.*;

 public class PalindromeArrayUser extends PalindromeArray
 {
public static void main(String argv[]) throws IOException 
{
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please enter the upper bound.");

    String line = input.readLine();

    int InputUser = Integer.parseInt(line);
                                     // this is where I pass the same int type as I  
                                                  // constructed it
    PalindromeArray palindrome = new PalindromeArray(InputUser);
    palindrome.InitializeArray();
    palindrome.primeCheck();
    palindrome.PrintArray();


}

 }
like image 517
user2901128 Avatar asked Nov 05 '13 12:11

user2901128


People also ask

Which classes Cannot declare constructors?

The child class inherits all the members of the superclass except the constructors. In other words, constructors cannot be inherited in Java therefore you cannot override constructors. So, writing final before constructors makes no sense. Therefore, java does not allow final keyword before a constructor.

Which type of class Cannot be used for creating an object?

An abstract class is one that cannot be used to create objects.

Which constructor is not possible in Java?

A static constructor is not allowed in Java programming. It is illegal and against the Java standards to use a static constructor. So, the Java program will not be compiled and throw a compile-time error.

Which one is the wrong constructor type?

What is false about constructor? Explanation: The constructor cannot have a return type. It should create and return new objects. Hence it would give a compilation error.


1 Answers

when you create a constructor for a class, there won't be any default constructor created for that class. so if you extend that class and if the subclass tries to call the no-arg constructor of its super class then there will be an compile-time error.

to demonstrate:

class Parent {
  int i;
  public Parent(int i) {
    this.i=i;
  }
}

class Child extends Parent {
  int j;
  public Child(int i, int j) {
    super(i);
    this.j=j;
  }
  public Child(int j) {
    // here a call to super() is made, but since there is no no-arg constructor
    // for class Parent there will be a compile time error
    this.j=j;
  }
}

EDIT:

to answer your question do this, don't assign the value arrLength to arr[] and check[] as arrLength would be 0 at that time.

so just declare them like this

int arr[];
boolean check[];

and in the constructor after you assign the input to arrLength put these statements.

arr = new int[arrLength];
check = new boolean [arrLength];
like image 185
Thirumalai Parthasarathi Avatar answered Oct 02 '22 14:10

Thirumalai Parthasarathi