I create this class ‘Courses’ where I have some parameters, one is of type List<‘String’>. At the time of creating the object in the main section, I am missing the list of prerequisites. I don’t know how to declare them correctly. If anyone knows how to do it, thanks for the help.
public class Courses{
private String code;
private String title;
private int numCred;
private String level;
private List<String> prerequisites;
public Courses(String code, String title, int numCred, String level, List<String> prerequisites ){
this.code= code;
this.title= title;
this.numCred= numCred;
this.level= level;
this.prerequisites = prerequisites;
}
In the main I have something like this:
Courses[] students = {
new Courses("211564", "Program", 3, "Pre", ),
new Courses("221451", "Debate", 3, "Pre", )
};
>Solution :
Constructor of Courses class expect five arguments but you are passing only four arguments while creating the objects.
If you do not have the prerequisites parameter everytime, you can make it optional in the constructor. Like this:
public Courses(String code, String title, int numCred, String level, List<String> prerequisites = new ArrayList<String>()){
this.code= code;
this.title= title;
this.numCred= numCred;
this.level= level;
this.prerequisites = prerequisites;
}
Then this will work in the main section:
Courses[] students = {
new Courses("211564", "Program", 3, "Pre"),
new Courses("221451", "Debate", 3, "Pre")
};
There are other ways to handle this like you can create another constructor in the class that expects four arguments, or you could pass the empty list as the fifth argument while creating the object.