Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object Oriented Programming Java Classes

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", )
        };

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading