-4

When i am trying to create an Course object in main method, eclipse says that "The constructor Course() is undefined" i dont know why it occurs.

Here is my Course class and its constructions;

public class Course{
    
private String CourseCode;

private String day;

private int StudentCount;

private int Capacity;

private double averageGrade;


public Course(String CourseCode, String day, int StudentCount, int Capacity, double averageGrade){
this.CourseCode=CourseCode;

this.day = day;

this.StudentCount =StudentCount;

this.Capacity = Capacity;

this.averageGrade =averageGrade; 
}

And the main method that trying to create an object called Course;

Course c = new Course();
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Course c = new Course() // The constructor Course() is undefined – sleepyshaman Oct 14 '20 at 19:14
  • 2
    You defined a constructor that takes a course code, day, student count, capacity, and average grade as parameters. Just like with any method call, you now need to pass some values to the constructor to match the signature you defined. – nanofarad Oct 14 '20 at 19:15
  • 2
    Java only generates a default constructor for you if you don't define any others. See: https://softwareengineering.stackexchange.com/questions/257938/why-is-there-no-default-constructor-generated-if-you-define-an-explicit-construc – azurefrog Oct 14 '20 at 19:16

3 Answers3

1

Now that you've defined a constructor, you need to call it with all the arguments it specifies.

Course c = new Course(
  "CMSC-101",
  "Wednesday",
  /* student count */ 20,
  /* capacity */ 25,
  /* average grade */ 4.0);
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0
Course c = new Course();

by writing above line in main method you're invoking a default constructor i.e. no params passed as an argument, in this case the fields are initialized of there default values.

whereas in your code

public Course(String CourseCode, String day, int StudentCount, int Capacity, double averageGrade){
this.CourseCode=CourseCode;

this.day = day;

this.StudentCount =StudentCount;

this.Capacity = Capacity;

this.averageGrade =averageGrade; 
}

is a Parametrized contsructor i.e. your passing some values (Parameter) and your setting some state of your variables using that.

in main method encounter compilation issues because it won't find a default constructor on your class

solution

either pass Param like

Course c = Course("ABC", "Monday", 23, 40, 2.30);

or create a default constructor (Both the constructors can exist in your class)

user
  • 7,435
  • 3
  • 14
  • 44
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37
0

Because you are creating Parameterized constructor and it's replacing default constructor of your class, and while you are creating object you are not passing any argument.

There are two way to solve this problem.

  1. Create one non parameterized constructor in your class along with your parameterized constructor.
    public Course{
        public Course(){}
    }
  1. While creating object pass argument within constructor.
    public Course{
       Course c = new Course("CourseCode", "Monday", 10, 100, 90.5);
    }
Vivek Jain
  • 317
  • 3
  • 12