Can't instantiate custom class
I have a class CollegeCourse:
Code :
public class CollegeCourse
{
private String courseId;
private int creditHours;
private char grade;
public CollegeCourse(String i, int h, char g)
{
this.courseId = i;
this.creditHours = h;
this.grade = g;
}
public String courseId(){return this.courseId;}
public int creditHours(){return this.creditHours;}
public char grade(){return this.grade;}
public void setcourseId(String i){this.courseId = i;}
public void setcreditHours(int h){this.creditHours = h;}
public void setgrade(char g){this.grade = g;}
}
Then I have the second class Student:
Code :
public class Student
{
private int studentId;
private CollegeCourse[] courseArray = new CollegeCourse[5];
public Student(int s, CollegeCourse[] c)
{
this.studentId = s;
this.courseArray = c;
}
public int studentId(){return this.studentId;}
public CollegeCourse[] courseArray(){return this.courseArray;}
public void setstudentId(int s){this.studentId = s;}
public void setcourseArray(CollegeCourse[] c){this.courseArray = c;}
}
In my tester class I am trying to instantiate the Student class like:
Code :
Student stu = new Student();
The compiler complains:
Code :
InputGrades.java:8: error: constructor Student in class Student cannot be applied to given types;
Student stu = new Student(12, new CollegeCourse("CSI 2010", 4, 'A'));
^
Also when I want to take input can I use a setter like this?:
Code :
stu.courseId = console.next();
Re: Can't instantiate custom class
You have defined a constructor for Student that takes 2 parameters as an argument, if you wish to have an empty argument constructor - which is how you are trying to instantiate the class - explicitly define this constructor in your class, or use the two argument constructor.
Re: Can't instantiate custom class
I am obviously still wrong because of my compiler error, am I any closer. I am trying to understand this conceptually, where am I wrong and
why?
tester class:
Code :
import java.util.*;
public class InputGrades
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int sId, hours;
String cId;
char letterGrade;
System.out.println("Please enter the student id: ");
sId = console.nextInt();
System.out.println("Enter the course id: ");
cId = console.nextLine();
System.out.println("Enter the credit hours: ");
hours = console.nextInt();
System.out.println("Enter the letter grade: ");
letterGrade = console.next().charAt(0);
Student stu = new Student(sId, new CollegeCourse(cId, hours, letterGrade));
}
}
Thanks!
Re: Can't instantiate custom class
I forgot here is the compiler error:
Code :
InputGrades.java:26: error: constructor Student in class Student cannot be applied to given types;
Student stu = new Student(sId, new CollegeCourse(cId, hours, letterGrade));
^
required: int,CollegeCourse[]
found: int,CollegeCourse
reason: actual argument CollegeCourse cannot be converted to CollegeCourse[] by method invocation conversion
1 error
Re: Can't instantiate custom class
Have a look at the expected data types and the found data types, they must match:
Expected:
int
CollegeCourse[]
Found:
int
CollegeCourse
Error, reason
"reason: actual argument CollegeCourse cannot be converted to CollegeCourse[] by method invocation conversion"
Have a look at Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Can't instantiate custom class
Building on what Tsarin said above, when you instantiate your student object in your tester class, you need to give it arguments that match the types it it expecting. As Tsarin said, the student 2 argument constructor is expecting an INT for the first argument and an ARRAY of college courses as the second argument. You are attempting to pass a single college course as your second argument when it should be an array of college courses.