Explanation
Your error message is pretty straightforward, carefully read it:
Error on line 12: constructor Game in class Game cannot be applied to given types;
return r.toString() + "\n" + super.play(new Game()) + "\n"; ^
required:
java.lang.String
found: no argumentsreason: actual and formal argument lists differ in length
So you are calling new Game()
, without any arguments. But the constructor in that class requires you to call it with a String
, like new Game("foo")
.
If you lookup the code for the class Game
, you will see something like:
public class Game {
...
// Constructor that requires a String as argument
public Game(String foo) {
...
}
...
}
Check out the class to see what exactly the purpose of that String
is.
Example
To give you a better feeling for what you did wrong, let me show you another example. Suppose you have a class Person
and you want that each person has a String name
and an int age
. You can achieve this by letting the constructor require both. For example:
public class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public String getAge() { return age; }
}
Now you can create instance of this class by calling the constructor and providing both, a name and an age:
Person person = new Person("John", 20);
But what you are trying to do is just calling it like new Person()
, without supplying any name or age, despite the constructor requiring it.
4
solved Sports team model – constructor in class cannot be applied to given types