- Constructor in class cannot be applied to given types
- 3 Answers 3
- Error: Constructor Room in class Room cannot be applied to given types
- Constructor cannot be applied to given types
- java constructor in class cannot be applied to given types
- 8 Answers 8
- Java error: constructor in class cannot be applied to given types
Constructor in class cannot be applied to given types
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]); >> >
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(); >>
3 Answers 3
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.
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; >>
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
and in the constructor after you assign the input to arrLength put these statements.
arr = new int[arrLength]; check = new boolean [arrLength];
Error: Constructor Room in class Room cannot be applied to given types
I am completely new to java. I have searched for hours upon hours for the solution to this problem but every answer involves passing args or using a void which I do not do in this situation. I have two java files, one for Room class, and one for TourHouse class. I am trying to create a new Room in the TourHouse class. Here is my error, it’s driving me nuts, I’ve tried everything I am capable of understanding. Thank you in advance.
HouseTour.java:15: error: constructor Room in class Room cannot be applied to given types; < ^ required: String, String found: no arguments reason: actual and formal arguments differ in length
// Room.java import java.util.*; public class Room < // Define Instance Variables private String name; private String description; // Define Constructor public Room(String theName, String theDescription) < name = theName; description = theDescription; >public String toString( ) < return "The " + name + "\n" + description + "\n"; >>
import java.util.*; public class HouseTour extends Room < // Define Variables public Room[ ] rooms = new Room[7]; //Define Constructor public HouseTour( ) < rooms[0] = new Room("Living Room", "Mayonnaise and Brill Grates, Michaelsoft"); rooms[1] = new Room("Basement", "Hopefully no dead bodies down here. "); >// this is horrible and not right public String rooms( ) < for (int i = 0; i > >
// this is horrible and not right public String rooms( ) < output = "House Rooms included in tour\n"; for (int i = 0; i return output.toString(); // do I do this? > >
What I am doing is trying to learn java by converting the ruby projects I have created. So in ruby you say:
def rooms output = "House Rooms included in tour\n" @rooms.each do |r| output += r.to_s + "\n" end return output end
Edit: Still trying, any ideas? added public String s; and public String output; to declarations
// this is horrible and not right public String rooms( ) < s = "" output = "House Rooms included in tour\n"; for (int i = 0; i s.toString() // I don't know return output + s; // do I do this? > >
Constructor cannot be applied to given types
been baning my head against the wall in what is probably, a simple problem that I just don't quite understand. I have these three classes, tring to pass object/methods between them. Heres the first class
public class LargeMapDriver < public static void main(String[] args) < Scanner keyboard = new Scanner(System.in); int value1; ThyPoint p1 = new ThyPoint(132, 734); ThyPoint p2 = new ThyPoint(56, 998); ThyPoint p3 = new ThyPoint(100, 105); System.out.println("Enter value: "); order = keyboard.nextInt(); LargeMap myMap = new LargeMap(value1, p1, p2, p3);
public class ThyPoint < private int a; private int b; //constructor public ThyPoint(int x, int y) < a = x; b = y; >//. //set and get methods for a and b. not shown //. public String toString() < return "a: " + getValueA() + " b: " + getValueA(); >>
**constructor LargeMap in class LargeMap cannot be applied to given types; LargeMap myMap = new LargeMap(value1, p1, p2, p3); ^ required: no arguments found int,ThyPoint,ThyPoint,ThyPoint reason: actual and formal arguments differ in length**
So, I'm trying to create a constructor for the LargeMap class, but failing, I'm trying to pass those values in the p1, p2, p3 objects into the constructor to accept. And to initialize the values in them, how do I do this? The values I want to initialize in them are:
ThyPoint p1 = new ThyPoint(132, 734); ThyPoint p2 = new ThyPoint(56, 998); ThyPoint p3 = new ThyPoint(100, 105);
java constructor in class cannot be applied to given types
I have 2 subclasses: Staff, Student they belong to superclass Person. Here is the code(tasks) which is given by my teacher:
class Student extends Person < private String SID; // student ID number /** * Create a student with no parameters. */ Student() < //task. >>
public class Staff extends Person < private String roomNumber; /** * Construct a staff member with field values and no pamaeters. */ public Staff() < //task >>
I don't know what to do. I an a starter in learning BlueJ. Hope anyone can help me. Thank you very much.
Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.
8 Answers 8
Since your superclass Person doesn't have a default constructor, in your subclasses ( Student and Staff ), you must call the superclass constructor as the first statement.
You should define your sub-class constructors like this:
Right, But why java compiler behave this way? When it is creating empty constructor when no provided, Why it isn't creating when having parameter?
the first thing a constructor will do, is call the constructor (with same arguments) of the super class. Person does not have a no-argument constructor, so, you must change your code in one of next two ways:
Student(String name, int yearOfBirth) < //task. >
and the same goes for Staff
Add super(NAME_IN_STRING_TYPE,YEAR_OF_BIRTH_IN_INT_TYPE); as a first statement in your subclasse's constructor like
This is needed since there is no default no-arg constructor in the base class. You have to explicitly define a no-arg constructor in base class or you need to instruct the compiler to call the custom constructor of the base class.
Note : Compiler will not add default no-arg constructor in a class if it has a user defined constructor. It will add the default no-arg constructor only when there is no constructor defined in the class.
Java error: constructor in class cannot be applied to given types
I just added the constructor Building and I thought everything would work fine, but I'm getting an error on line 43. When I create the object, Building b = new Building(); , it says I need to have a double and int in the argument, so I did as it said, but I just keep getting more errors. What am I doing wrong?
// This program lets the user design the area and stories of a building multiple times // Author: Noah Davidson // Date: February 20, 2014 import java.util.*; public class Building // Class begins < static Scanner console = new Scanner(System.in); double area; // Attributes of a building int floors; public Building(double squarefootage, int stories) < area = squarefootage; floors = stories; >void get_squarefootage() // User enters the area of floor < System.out.println("Please enter the square footage of the floor."); area = console.nextDouble(); >void get_stories() // The user enters the amount of floors in the building < System.out.println("Please enter the number of floors in the building."); floors = console.nextInt(); >void get_info() // This function prints outs the variables of the building < System.out.println("The area is: " + area + " feet squared"); System.out.println("The number of stories in the building: " + floors + " levels"); >public static void main(String[] args) // Main starts < char ans; // Allows for char do< // 'do/while' loop starts so user can reiterate // the program as many times as they desire Building b = new Building(); // Creates the object b b.get_squarefootage(); // Calls the user to enter the area b.get_stories(); // Calls the user to enter the floors System.out.println("---------------"); b.get_info(); // Displays the variables System.out.println("Would you like to repeat this program? (Y/N)"); ans = console.next().charAt(0); // The user enters either Y or y until // they wish to exit the program >while(ans == 'Y' || ans == 'y'); // Test of do/while loop > >