Recall the following scenario discussed during the class. Develop a code base to represent the scenario. Add a test class to invoke Lecturer and Student class by creating atleast one object from each.
Note: All the common attributes and behavior stored in the super class and only the specific fields and behavior stored in subclasses.
Student
- name
- id
- course
+ setName()/getName()
+ setID()/getID()
+ setCourse()/getCourse()
Lecturer
- name
- id
- programme
+ setName()/getName()
+ setID()/getID()
+ setProg()/getProg()
Person
Identify field and attributes to be stored in this class
package persons;
public class Persons {
private String name;
private int id;
void setName(String n){
name = n;
}
String getName(){
return name;
}
void setID(int i){
id = i;
}
int getID(){
return id;
}
public static void main(String[] args) {
Student st = new Student();
st.setDetails("John", 23, "Computer Science");
st.display();
Lecturer lec = new Lecturer();
lec.setData("Abraham", 343, "Economics");
lec.display();
}
}
class Student extends Persons{
private String course;
void setDetails(String n, int i, String c){
setName(n);
setID(i);
course = c;
}
void display(){
System.out.printf("Student Name:"
+ " %s\nStudent ID: %d\nStudent Course: %s",getName(), getID(), course);
}
}
class Lecturer extends Persons{
private String programme;
void setData(String n, int i, String p){
setName(n);
setID(i);
programme = p;
}
void display(){
System.out.printf("Lecturer Name: "
+ "%s\nLecturer ID: %d\nLecturer"
+ " programme: %s",getName(), getID(), programme);
}
}
Comments
Leave a comment