In: Computer Science
I am to complete the following for Java but getting compiler errors...Implement a base class called Student that contains a name and major. Implement the subclass GraduateStudent, which adds a property called stipend.
Here is what I have for code with the compiler error following.
public class GraduateStudent extends Student {
private double stipend;
public GraduateStudent(String name, String major,
double stipend) {
super(name);
super(major);
this.stipend = stipend;
}
public void setStipend(double stipend) {
this.stipend = stipend;
}
public double getStipend() {
return stipend;
}
}
public class Student {
protected String name;
protected String major;
public Student(String name, String major) {
this.name = name;
this.major = major;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setMajor(String major) {
this.major = major;
}
public String getMajor() {
return major;
}
}
GraduateStudent.java:7: error: constructor Student in class Student cannot be applied to given types; super(name); ^ required: String,String found: String reason: actual and formal argument lists differ in length GraduateStudent.java:8: error: call to super must be first statement in constructor super(major); ^ 2 errors [ERROR] did not compile; check the compiler stack trace field for more info
PROGRAM EXECUTION STACK TRACE
[ERROR] did not compile; check the compiler stack trace field for more info
YOUR CODE'S OUTPUT
1 | [ERROR] did not compile; check the compiler stack trace field for more info |
2 | |
3 | false |
Program:
class Student { protected String name; protected String major; public Student(String name, String major) { this.name = name; this.major = major; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setMajor(String major) { this.major = major; } public String getMajor() { return major; } } class GraduateStudent extends Student { private double stipend; public GraduateStudent(String name, String major, double stipend) { /* This is the part where you made mistake */ /* Super class constructor should be called like this */ super(name, major); this.stipend = stipend; } public void setStipend(double stipend) { this.stipend = stipend; } public double getStipend() { return stipend; } } /* Main class for creating object of GraduateStudent class to test the code*/ public class Main { public static void main(String[] args) { GraduateStudent g = new GraduateStudent("John", "Computer", 1500); // calling GraduateStudent methods System.out.println(g.getName()); System.out.println(g.getMajor()); System.out.println(g.getStipend()); } }
Program Snippet:
class Student
{
protected String name;
protected String major;
public Student(String name, String major)
{
this.name = name;
this.major = major;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setMajor(String major)
{
this.major = major;
}
public String getMajor()
{
return major;
}
}
class GraduateStudent extends Student
{
private double stipend;
public GraduateStudent(String name, String major, double stipend)
{
/* This is the part where you made mistake */
/* Super class constructor should be called like this */
super(name, major);
this.stipend = stipend;
}
public void setStipend(double stipend)
{
this.stipend = stipend;
}
public double getStipend()
{
return stipend;
}
}
/* Main class for creating object of GraduateStudent class to test the code*/
public class Main
{
public static void main(String[] args)
{
GraduateStudent g = new GraduateStudent("John", "Computer", 1500);
// calling GraduateStudent methods
System.out.println(g.getName());
System.out.println(g.getMajor());
System.out.println(g.getStipend());
}
}
Output:
I hope you got the provided solution.
Thank You.