Specific Requirements We will begin the development of a simple University regis
ID: 3768072 • 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.
Person Class
Instance Fields
// You need not use these names, but you must use these types
String firstName
String lastName
String ssn
Constructors
// Default - Sets last name, first name and ssn to "unknown"
Person()
// Three string parameters are first name, last name and ssn
// Be certain that the parameters are in that order
Person ( String, String, String )
Methods
// Sets the person's first name, last name and ssn
// Be certain that the parameters are in that order
setAttributes( String, String, String )
// Returns a String containing the person’s first name
getFirstName()
// Returns a String containing the person’s last name
getLastName()
// Returns a String containing the person’s ssn
getSSN()
// This method is a member of the Comparable interface.
// Briefly, it compares the parameter Object to this object
// and decides whether this object is less than, equal to or greater than
// the parameter Object. It then returns an integer indicating the
// outcome of that comparison. The possible values for the return value
// are a negative value, zero or a positive value.
//
// There are several things to consider here. First of all we need a policy.
// This method is going to be used to assist in getting persons into
// alphabetical order, so the policy will be that this method will
// look at both last name and first name when comparing persons. It
// will not look at ssn. The first names need be
// compared only if the last names are the same. Remember that case
// matters. If ASCII codes are used to compare Strings, all uppercase
// characters come before all lowercase characters.
// ASCII --- 'A' = 65, 'Z' = 90, 'a' = 97
// (ex. Zac comes before alice alphabetically)
//
// One option would be to convert all names to the same case for
// purposes of comparison. In this project we will simply assume that
// all names are supplied correctly (always dangerous) and compare them
// as is. (Let "Zac" come before "al" )
// Finally, remember to indicate that Person implements the
// comparable interface in your class declaration statement and
// be sure that your parameter type is Object and not Student
compareTo( Object )
// Returns a boolean indicating whether the parameter Object is equal to
// this object
// Note: the policy will be that two persons with the same ssn are duplicates
// even if the names are different
equals( Object )
// Returns a String containing the person’s information
// Use roughly the format shown below. You need not count spaces and make
// everything line up perfectly. You might use a tab character to
// separate the first name and the ssn.
toString()
// Here is the desired format
// Smith, Julie 123-45-6789
Student Class (a subclass of Person)
Instance Fields
// You need not use these names, but you must use these types
String yearInSchool
// Note: The student id should be unique for each student
// The student id is not their ssn
int id
// Note: One way to assign a unique ID for each student is to declare a
// static variable in the Student class and use it to keep track of the
// next ID number to be assigned and increment it each time a new student
// is created.
// The policy in this case will be that the first student is assigned
// ID number 1000, the next student ID number 1001, etc.
static int nextID
Constructors
// Default - Sets last name, first name and ssn to "unknown"
// assigns the next sequential ID number and sets year in school
// to "unknown
Student()
// Four string parameters are first name, last name, ssn and year in school
// Be certain that the parameters are in that order
// This constructor should also assign the next sequential ID number
Student( String, String, String, String )
Methods
// Sets the student’s first name, last name, ssn and year in school
// Be certain that the parameters are in that order
setAttributes( String, String, String, String )
// Returns an integer containing the student’s ID number
getID()
// Returns a boolean indicating whether the parameter Object is equal to
// this object
// Note: the policy will be that two students with the same ID are duplicates
// even if the names are different
equals( Object )
// Returns a String containing the student’s information
// Use roughly the format shown below. Use the tab character for spacing
toString()
// here is the desired format
// Smith, Julie 123-45-6789 Sophomore 1001
Instructor Class (a subclass of Person)
Instance Fields
// You need not use these names, but you must use these types
// rank can be one of four values: instructor, assistant, associate or professor
String rank
// the salary will depend on the rank of the instructor
double salary
// Note: The instructor id should be unique for each instructor and starts at 10000
// The instructor id is not their ssn
// Look at the definition for student for ideas on unique IDs.
// But remember, this is a different id than the student id.
int id
Constructors
// Default - sets the salary to zero and sets rank to "unknown"
// - Sets last name, first name and ssn to "unknown"
Instructor()
// Four string parameters are first name, last name, ssn and rank
// Be certain that the parameters are in that order
// This constructor should also assign salary based on rank
// if the rank is the salary is
// instructor 20,000
// assistant 40,000
// associate 60,000
// professor 80,000
// creates an unique, consecutive id for the instructor
Instructor( String, String, String, String )
Methods
// Sets the instructor's first name, last name, ssn, rank and salary
// Be certain that the parameters are in that order
setAttributes( String, String, String, String )
// Returns an double containing the instructor's salary
getSalary()
// Returns a boolean indicating whether the parameter Object is equal to
// this object
// Note: the policy will be that two instructors with the same ID are duplicates
// even if the names are different
equals( Object )
// Returns a String containing the student information
// Use roughly the format shown below. You need not count spaces
// and make everything line up perfectly, use tabs instead.
toString()
// here is the desired format
// Smith, Julie 123-45-6789 Instructor 20000.00
The fourth class we will write is a class that will contain an Instructor and a group of students. This Course will also have a name and a room number. The course can be used in further developing our registration system. It contains an array of type Student to hold the student list. The array will not be full, so you have to keep track of the number of students actually inserted into the array. We will insert a group of students that may be in any order. After we insert all the students, then we need to sort them in alphabetical order. Remember that the person has a compareTo method that you can use to do the comparison while sorting the students.
Course Class
Instance Fields
// You need not use these names, but you must use these types
String courseName
int room
Instructor instructor
int numberStudents
Student [] studentList
Constructors
// Default - set the name to "unknown", the room number to 0,
// the number of students to zero and the instructor will be set
// to "unknown"
Course()
// sets the name, the room number for the course. Number of students will
// be set to zero and the instructor will be set to "unknown"
Course( String, int )
Methods
// Sets the course name, room number and number of students. Be certain
// that the parameters are in that order. Initializes the size of the array
// to hold 30 students. Note that it is empty at this point.
setAttributes( String, int, int )
// Returns the instructor assigned to this course
getInstructor()
// Assigns an instructor to the course
setInstructor( Instructor )
// Returns the name of this course
getCourseName()
// Assigns new name to the course
setCourseName( String )
// Returns the room number assigned to this course
getRoomNumber()
// Assigns a room number to the course
setRoomNumber( int )
// Adds a student to the course
void addStudent(Student)
// Sorts all the students in the course
void sortStudents()
// Returns a boolean indicating whether the parameter Object is equal to
// this object
// Note: the policy will be that courses with the same name and the same
// instructor are duplicates even if the student in the course are different
equals( Object )
// Returns a String containing the studentís information
// Use roughly the format shown below. You need not count spaces
// and make everything line up perfectly
toString()
// here is the desired string format
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Course: CIS 3020 Room: 333
// Instructor: Kerr, Lorn 123-456-7890 Instructor 20000.0
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Students:
// Jones, Paul 123-45-6789 Senior 1005
// Smith, Julie 123-45-6789 Sophomore 1001
// ... etc.
Inheritance
Here are a few reminders about inheritance. All methods and instance fields of the superclass are inherited by the subclass. Private instance fields in the superclass can be accessed only through accessors. If a method in the superclass does not perform exact functionality required by the subclass, it can be overridden in the subclass (remember the signatures must match). New methods may be written for the subclass that do not appear in the superclass. Finally, constructors are not inherited, but the constructors in the superclass can be called explicitly from the subclass. Be careful about accidentally shadowing variables in the superclass by re-declaring them in the subclass.
Explanation / Answer
Person class:
public class Person implements Comparable {
public String firstName;
public String lastName;
public String ssn;
public Person() {
this("unknown", "unknown", "unknown");
}
public Person(String fName, String lName, String ssn) {
this.setAttributes(fName, lName, ssn);
}
public void setAttributes(String fName, String lName, String ssn) {
this.firstName = fName;
this.lastName = lName;
this.ssn = ssn;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public String getSSN() {
return this.ssn;
}
public int compareTo(Object obj) {
if (obj instanceof Person) {
Person person = (Person) obj;
int last = this.getLastName().compareTo(person.getLastName());
if (last == 0) {
return this.getFirstName().compareTo(person.getFirstName());
} else
return last;
}
return 0;
}
public boolean equals(Object person) {
if (person instanceof Person) {
Person newPerson = (Person) person;
return this.getSSN().equals(newPerson.getSSN());
}
return false;
}
public String toString() {
return this.lastName + ", " + this.firstName + " " + this.ssn;
}
}
Student class:
public class Student extends Person
{
private String yearInSchool;
private int id;
private static int nextID = 1000;
public Student()
{
this("unknown","unknown","unknown","unknown");
}
public Student( String fName, String lName, String ssn, String year)
{
this.setAttributes(fName, lName, ssn,year);
this.id = Student.nextID;
Student.nextID++;
}
public void setAttributes( String fName, String lName, String ssn, String year)
{
super.setAttributes(fName, lName, ssn);
this.yearInSchool = year;
}
public int getID()
{
return this.id;
}
public boolean equals(Object obj)
{
if (obj instanceof Student) {
Student student = (Student) obj;
return this.getID() == student.getID();
}
return false;
}
public String toString()
{
return super.toString()+" "+this.yearInSchool+" "+this.getID();
}
}
Instructor class:
public class Instructor extends Person {
private String rank;
private double salary;
private int id;
private static int nextID = 1000;
public Instructor() {
this("unknown", "unknown", "unknown", "unknown");
}
public Instructor(String fName, String lName, String ssn, String rank) {
this.setAttributes(fName, lName, ssn, rank);
this.id = Instructor.nextID;
Instructor.nextID++;
}
public int getID() {
return this.id;
}
public void setAttributes(String fName, String lName, String ssn, String rank) {
super.setAttributes(fName, lName, ssn);
this.rank = rank;
switch (rank.toLowerCase()) {
case "instructor":
this.salary = 20000;
break;
case "assistant":
this.salary = 40000;
break;
case "associate":
this.salary = 60000;
break;
case "professor":
this.salary = 80000;
break;
default:
this.salary = 0;
break;
}
}
public double getSalary() {
return this.salary;
}
public boolean equals(Object obj) {
if (obj instanceof Instructor) {
Instructor instructor = (Instructor) obj;
return this.getID() == instructor.getID();
}
return false;
}
public String toString() {
return super.toString() + " " + this.rank + " " + this.getSalary();
}
}
TOO MANY SUB_PARTS IN ONE QUESTION!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.