Object-Oriented Design Refactoring

Verified

Added on  2019/09/22

|29
|2910
|327
Practical Assignment
AI Summary
This practical assignment presents a Java-based university system with an initial flawed design. The assignment is divided into several parts. Part (a) shows the initial class diagram of the system, highlighting classes like School, Student, Faculty, OfficeStaff, and TechnicalSupportStaff. Part (b) analyzes the drawbacks of this design, pointing out issues like code duplication (violating the DRY principle), lack of proper abstraction, and missing getter/setter methods. The solution proposes refactoring the code by introducing a base class 'Person' to inherit common attributes and methods, improving data and method abstraction, and enhancing information hiding. Part (c) illustrates the improved class diagram after refactoring, showcasing the use of inheritance and polymorphism. Finally, part (d) provides the refactored Java code, including the 'Person' base class and derived classes with appropriate getter and setter methods. The refactored code demonstrates improved organization, reduced redundancy, and better adherence to object-oriented principles.
Document Page
a) The class diagram for the current implementation of the university system
String schoolName
ArrayList <Student> students
ArrayList <Faculty> faculty
ArrayList <OfficeStaff> o_staff
ArrayList <TechnicalSupportStaff> ts_staff
School
School(String name)
addstudent(Student s)
addfaculty(Faculty f)
addofficeStaff(OfficeStaff os)
addTSS(TechnicalSupportStaff tss)
displaySchoolInfo()
Student
String firstName;
String lastName;
String major;
double gpa;
String academicLevel;
Student(String fName,
String lName, String al,
String m, double gp)
viewStudent()
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
String firstName
String lastName
String department
String researchArea
double monthly_salary
boolean hourlyPaid
double hourlyRate
Faculty(String fName, String lName, String
dep, String res, boolean hp, double ms,
double hr)
viewFaculty()
Faculty
OfficeStaff
String firstName;
String lastName;
boolean hourlyPaid;
double hourlyRate;
double monthly_salary;
OfficeStaff(String fName,
String lName, boolean hp,
double ms, double hr)
viewOfficeStaff()
TechnicalSupportStaff
String firstName;
String lastName;
String technicalArea;
boolean hourlyPaid;
double hourlyRate;
double monthly_salary;
TechnicalSupportStaff(String fName, String
lName, String ta, boolean hp, double ms,
double hr)
viewTechnicalSupportStaff()
Document Page
b) Current implementation violates some of the OO design issues discussed in the
class.
What are the drawbacks of the design given? How can you refactor it? Here are
some of the factors you need to consider Proper data and method abstraction
Information hiding Using inheritance and polymorphism as needed Removing
duplicate code Other “smelly code” symptoms discussed in the class as
appropriate
Ans :
The drawbacks of the given design are:
There is no proper formatting of the code
It violates the DRY (don't repeat yourself) means don't write duplicate code,
instead use Abstraction and inheritance to abstract common things in one place.
The benefit of this Object oriented design principle is in maintenance. It's
important not to abuse it, duplication is not for code, but for functionality.
We can remove unused imports
Like we can remove
import java.util.*; from university.java
There are no getter and setter for the private fields in every class.
Document Page
We can refactor it by
Proper data and method abstraction
Information hiding
proper formatting of the code
Using inheritance and polymorphism as needed
Removing duplicate code
We can also write a base class Person.java
package university;
public class Person {
protected String firstName;
protected String lastName;
protected boolean hourlyPaid;
protected double hourlyRate;
protected double monthly_salary;
public String getFirstName() {
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isHourlyPaid() {
return hourlyPaid;
}
public void setHourlyPaid(boolean hourlyPaid) {
this.hourlyPaid = hourlyPaid;
}
Document Page
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public double getMonthly_salary() {
return monthly_salary;
}
public void setMonthly_salary(double monthly_salary) {
this.monthly_salary = monthly_salary;
}
}
Document Page
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Part c
Draw the class diagram for the refactored design (your refactored design should have inheritance and
polymorphism included)
Base class Person
Person
protected String firstName;
protected String lastName;
protected boolean hourlyPaid;
protected double hourlyRate;
protected double monthly_salary;
String getFirstName()
setFirstName(String firstName)
String getLastName()
setLastName(String lastName)
boolean isHourlyPaid()
setHourlyPaid(boolean hourlyPaid)
double getHourlyRate()
setHourlyRate(double hourlyRate)
double getMonthly_salary()
setMonthly_salary(double monthly_salary)
Document Page
Student, Faculty, OfficeStaff, TechnicalSupportStaff extends Person class
String schoolName
ArrayList <Student> students
ArrayList <Faculty> faculty
ArrayList <OfficeStaff> o_staff
ArrayList <TechnicalSupportStaff> ts_staff
School(String name)
addstudent(Student s)
addfaculty(Faculty f)
addofficeStaff(OfficeStaff os)
addTSS(TechnicalSupportStaff tss)
displaySchoolInfo()
getSchoolName():String
setSchoolName(String schoolName)
ArrayList<Student> getStudents()
setStudents(ArrayList<Student> students)
ArrayList<Faculty> getFaculty()
setFaculty(ArrayList<Faculty> faculty)
ArrayList<OfficeStaff> getO_staff()
setO_staff(ArrayList<OfficeStaff> o_staff)
ArrayList<TechnicalSupportStaff>
getTs_staff()
s
etTs_staff(ArrayList<TechnicalSupportStaff
> ts_staff)
Student extends Perso
String major;
double gpa;
String academicLevel;
Student(String fName,
String lName, String al,
String m, double gp)
viewStudent()
String getMajor()
setMajor(String major)
double getGpa()
setGpa(double gpa)
String
getAcademicLevel()
setAcademicLevel(Strin
g academicLevel)
School
Document Page
String department
String researchArea
Faculty(String fName, String lName, String
dep, String res, boolean hp, double ms,
double hr)
viewFaculty()
String getDepartment()
setDepartment(String department)
String getResearchArea()
setResearchArea(String researchArea)
Faculty extends Person
OfficeStaff extends Person
OfficeStaff(String fName,
String lName, boolean hp,
double ms, double hr)
viewOfficeStaff()
TechnicalSupportStaff extends Person
String technicalArea;
TechnicalSupportStaff(String fName, String
lName, String ta, boolean hp, double ms,
double hr)
viewTechnicalSupportStaff()
String getTechnicalArea()
setTechnicalArea(String technicalArea)
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Document Page
Part d
Person.java
package university;
public class Person {
protected String firstName;
protected String lastName;
protected boolean hourlyPaid;
protected double hourlyRate;
protected double monthly_salary;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
chevron_up_icon
1 out of 29
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]