Specific Requirements We will begin the development of a simple University regis
ID: 3766305 • Letter: S
Question
Specific Requirements We will begin the development of a simple University registration system that keeps track of students, instructors, and courses. We will have students and instructors that share many common traits such as names and social security numbers. By using the concept of inheritance, we can separate out the common entities that both Students and Instructors have and place these common elements in a separate Person class. Then we can inherit from this Person class and avoid writing the same code several times. Correspondingly, in our classes, if we have code that appears several times, we can abstract it out and place it in a separate method and have the method called when we need this particular functionality. In Instructor and Student, we will abstract out the common code in the two constructors (for each class) and place it in a method called setAttributes. This way we have the code in a single place and, if we find errors, we only have to change it in one place to fix it. In this project you will write five classes. The first will be a class that represents a person. The second will be a class that represents a college student. The third will an instructor. The fourth class will simulate a class membership list. The fifth will be a test class to verify exhaustively the correctness of the other four classes; you should test at least once for each method. Notice that both the Student Class and the Instructor Class inherit from the Person Class. A student is a special Person. An Instructor is also a special person, but is a different kind of special person than is a Student. It is important that you take advantage of the functionality of the super-class when you write these subclasses. The primary advantage of inheritance is to be able to reuse code.
Explanation / Answer
Complete Program:
// File: Person.java
public class Person
{
private String name;
private int ssn;
public Person()
{
this.name = "";
this.ssn = 0;
}
public Person(String name, int ssn)
{
setAttributes(name, ssn);
}
void setAttributes(String name, int ssn)
{
this.name = name;
this.ssn = ssn;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getSSn()
{
return ssn;
}
public void setSSN(int ssn)
{
this.ssn = ssn;
}
public String toString()
{
return "Name: " + getName() + " " + "SSN: " + getSSn() + " ";
}
}
// File: Student.java
public class Student extends Person
{
private String majorSubject;
public Student()
{
super();
majorSubject = "";
}
public Student(String name, int ssn, String majorSubject)
{
super(name, ssn);
this.majorSubject = majorSubject;
}
public String getMajorSubject()
{
return majorSubject;
}
public void setMajorSubject(String majorSubject)
{
this.majorSubject = majorSubject;
}
public String toString()
{
return "Student " + super.toString() + "MajorSubject: " + getMajorSubject() + " ";
}
}
// File: Instructor.java
public class Instructor extends Person
{
private double salary;
public Instructor()
{
super();
salary = 0;
}
public Instructor(String name, int ssn, double salary)
{
super(name, ssn);
this.salary = salary;
}
public double getSalary()
{
return salary;
}
public void setSalary(double salary)
{
this.salary = salary;
}
public String toString()
{
return "Instructor " + super.toString() + "Salary: " + getSalary() + " ";
}
}
// File: Membership.java
import java.util.*;
public class Membership
{
private ArrayList<Object> list;
public Membership()
{
list = new ArrayList<Object>();
}
public void addStudent(Student student)
{
list.add(student);
}
public void addInstructor(Instructor instructor)
{
list.add(instructor);
}
public ArrayList<Student> getStudents()
{
ArrayList<Student> students = new ArrayList<Student>();
for(int i = 0; i < list.size(); i++)
{
if(list.get(i) instanceof Student)
students.add((Student)list.get(i));
}
return students;
}
public ArrayList<Instructor> getInstructors()
{
ArrayList<Instructor> instructors = new ArrayList<Instructor>();
for(int i = 0; i < list.size(); i++)
{
if(list.get(i) instanceof Instructor)
instructors.add((Instructor)list.get(i));
}
return instructors;
}
public int getNumberOfStudents()
{
int count = 0;
for(int i = 0; i < list.size(); i++)
{
if(list.get(i) instanceof Student)
count++;
}
return count;
}
public int getNumberOfInstructors()
{
int count = 0;
for(int i = 0; i < list.size(); i++)
{
if(list.get(i) instanceof Instructor)
count++;
}
return count;
}
public String toString()
{
String result = "";
for(int i = 0; i < list.size(); i++)
{
result += list.get(i) + " ";
}
return result;
}
}
// File: Driver.java
public class Driver
{
public static void main(String[] args)
{
Student s1 = new Student("John Smith", 1001, "Physics");
Instructor i1 = new Instructor("Mike Roy", 1002, 20000);
Membership members = new Membership();
members.addStudent(s1);
members.addInstructor(i1);
System.out.println(members);
}
}
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.