John Francis Saquido Capili John Francis Saquido Capili - 17 days ago 12
Java Question

Can you help me with a java code

This code i made them i don't know if that is correct.

Person.Java

public class Person {
private String fname;
private String lname;
public Person(){
this.fname="";
this.lname="";

}
public Person(String fname,String lname){

this.fname = fname;
this.lname = lname;
}
public void setfirstname(String fname){
this.fname = fname;

}
public void setlastname(String lname)
{
this.lname = lname;

}
public String getfirstname(){
return fname;
}
public String getlastname(){
return lname;
}
}


Student.java

public class Student extends Person{
private String studentid;
private String studenttype;
public Student(){
this.studentid = "";
this.studenttype = "";

}
public Student(String studentid,String studenttype)
{
this.studentid = studentid;
this.studenttype = studenttype;
}
public void setStudentId(String studentid)
{
this.studentid = studentid;
}
public void setStudentType(String studenttype){
this.studenttype = studenttype;
}
public String getStudentID(){
return studentid;
}
public String getStudentType()
{
return studenttype;
}
}


Course.java


Course.java


i think i am not finish at this point i have some problem on the subject and list can you help me in this code

public class Course extends Student{
private String courseid;
private String coursedesc;
private Subject subject[];
public Course()
{
this.courseid = "";
this.coursedesc = "";
}
public Course(String courseid,String coursedesc)
{
this.courseid = courseid;
this.coursedesc = coursedesc;
}
public void setCourseId(String courseid)
{
this.courseid = courseid;
}
public void setCourseDesc(String coursedesc)
{
this.coursedesc = coursedesc;
}
public String getCourseId()
{
return courseid;
}
public String getCourseDesc()
{
return coursedesc;
}
public void addSubject(Subject subject)
{

}
}


Subject.java


**Can you guys help me at this ? **


Answer

Use ArrayList instead of Subject[]. Like that:

public class Course extends Student {
    private String courseid;
    private String coursedesc;
    private List<Subject> subjects;

    public Course()
    {
        this.courseid = "";
        this.coursedesc = "";
        subjects = new ArrayList<Subject>();
    }

    public Course(String courseid, String coursedesc)
    {
        this();
        this.courseid = courseid;
        this.coursedesc = coursedesc;
    }

    public void setCourseId(String courseid)
    {
        this.courseid = courseid;
    }

    public void setCourseDesc(String coursedesc)
    {
        this.coursedesc = coursedesc;
    }

    public String getCourseId()
    {
        return courseid;   
    }

    public String getCourseDesc()
    {
        return coursedesc;   
    }

    public void addSubject(Subject subject)
    {
        subjects.add(subject);
    }

    public List<Subject> getSubjects()
    {
        return subjects;
    }
}