Please help me fix this error:
constructor CollegeMember in class C10h1.CollegeMember cannot be applied to given types; required: java.lang.String,java.lang.String; found: no arguments; reasons: actual and formal arguments differ in length
Also I tried adding a default constructor to call super but it just gave me another error!
Code:
class C10h1
{
public static void main(String[] args)
{
Student s = new Student("Bert", 2, "555-5555");
System.out.println("name = " + s.getName());
System.out.println("year = " + s.getYear());
System.out.println("telephone = " + s.getTelNumber());
Professor p = new Professor("Jane", 1, "555-9999");
System.out.println("name = " + p.getName());
System.out.println("rank = " + p.getRank());
System.out.println("telephone = " + p.getTelNumber());
}
}
class CollegeMember
{
String name, telNumber;
public CollegeMember(String a, String b)
{
name = a;
telNumber = b;
}
public String getName()
{
return name;
}
public String getTelNumber()
{
return telNumber;
}
}
class Student extends CollegeMember
{
int year;
public Student(String name, int year, String telNumber)
{
name = super.getname();
year = year;
telNumber = super.getTelNumber();
}
public int getYear()
{
return year;
}
}
class Professor extends CollegeMember
{
int rank;
public Professor(String name, int rank, String telNumber)
{
name = super.getname();
rank = rank;
telNumber = super.getTelNumber();
}
public int getRank()
{
return rank;
}
}
Question Prompt:
Create 3 public classes: CollegeMember, Student, and Professor. Student and Professor are subclasses of CollegeMember. CollegeMember has a String name field and a string telNumber field. Its constructor has 2 parameters that provide the initial values for the name and telNumber fields. CollegeMember also has accessor methods getName and getTelNumber that return name and telNumber, respectively. Student has an int year field(1=first year, 2=second year, 3=third year, 4=fourth year).Its constructor has 3 parameters: name, year, and telNumber that provide initial values for the name, year, and telNumber fields. It also has an accessor method getYear that returns year. Professor has an int rank field(1=assistant, 2=associate, 3=full). Its constructor has 3 parameters: name, rank, and telNumber that provide initial values for the name, rank, and telNumber fields. It also has an accessor method getRank that returns rank. Create a C10h1 class with a main method. Include javadoc comments in your class files. Use javadoc to create documentation files.
The problem is your Student
constructor. It needs to chain to the CollegeMember
constructor, which it should do like this:
public Student(String name, int year, String telNumber) {
super(name, telNumber);
this.year = year;
}
Likewise for Professor
, just with rank
instead of year
Note that you don't need to set name
and telNumber
in those constructors - that's the job of the CollegeMember
constructor you're chaining to. It would also be better if you made all the fields private - then you wouldn't even be able to try to set those fields in the subclasses... which makes sense, as they're not the responsibility of the subclasses.
See more on this question at Stackoverflow