Assignment 1
Question 1 : Write a student class to represent students. Each student has a name, a unique ID, and a
phone number. Students can choose to add courses or drop courses. No student has access to the research
lab, but a student has access to the library if he/she is taking at least one course.
The Student class should have the following public interface:
- public Student(String name, String phoneNumber) : A constructor to set up student’s name and
the phone number. Hint: Consider using a static variable to help assign a unique ID to each student
object. - public String getStudentName() : An accessor to return the student’s name.
- public String getStudentPhone() : An accessor to return the student’s phone number.
- public int getStudentId() : An accessor to return the student’s ID.
- public boolean addCourse(String courseName) : A method to add courses. The max course load
of each student is five courses. Any attempt to add more than five courses will fail and this method
will return false. Return true when the course is added successfully. - public boolean dropCourse(String courseName) : A method to drop a course. This method will
remove the course from the student’s course load. If there is no such course in the student’s course
load or the student has not enrolled in any course, the method will return false. - public int getCourseCount() : A method to return the number of courses that student has enrolled.
- public boolean hasAccessToLibrary() : A method that indicates whether the student has access to
the library. If the number of courses enrolled is greater than zero, the student will have the access
to library. - public boolean hasAccessToResearchLab() : A method that indicates whether the student has
access to the research lab. Return false.
Write a tester program Task1Tester to test the class you have written. Follow the examples given in
class (e.g., BankAccountTester), and create objects from the class Student above. Test the public
interface of your class thoroughly (each public method must be tested at least once). For accessor
methods, print out the expected return value and the return value of your method (see the
BankAccountTester example) to compare. This tester class is not to be submitted for marking, but it is
beneficial for you to make sure your Student class works as intended.
Question 2 : Now we would like to have an additional class Instructor to represent instructors. All
instructors have access to the library. Assume that each instructor is assigned to teach one course and
this assignment will not change. Each instructor may also have a research project, and only instructors
having a research project can access the research lab.
Since now we have two types of people (and imagine down the road we might have more), you should
implement a hierarchy of classes to represent the different types of people. So the first step of this
question is to write, at the top of the class hierarchy, an abstract class called Person with methods
common to different types of people. Person has the following public interface:
- public Person(String name, String phoneNumber, String type) : A constructor to initialize the
person’s name and phone number. Consider using a static variable to assign a unique Id to each
person object. - public String getName() : An accessor to return the person’s name.
- public String getPhoneNumber() : An accessor to return the person’s phone number.
- public int getId() : An accessor to return the person’s id.
- public String getType() : An accessor to return a string that represents the type of person.
- public abstract boolean hasAccessToLibrary() : A method that indicates whether the person has
access to the library. This should be override by the subclass. - public abstract boolean hasAccessToResearchLab() : A method that indicates whether the person
has access to the research lab. This should be overridden by the subclass.
Once Person is defined, you must then rewrite the Student class (with a new class name Student 2 )
and define a new class Instructor , both of which inherit from Person :
- Student 2 should be implemented to keep the properties and behaviors specified in Question 1 (of
course, the constructor for this class should be named Student2 instead of Student ). - The class Instructor should have the following public interface:
- public Instructor(String name, String phoneNumber, String courseId) : A constructor to
set up the instructor name, phone number and course. - public boolean setReserachTopic(String topic) : A method to set the research topic for the
instructor. If the instructor has already had a topic, the old topic will be replaced by the given
topic. - public String getTopic() : return the research topic of the instructor.
- public boolean hasAccessToLibrary() : A method that indicates whether the instructor has
access to the library. Always return true. - public boolean hasAccessToResearchLab() : A method that indicates whether the instructor
has access to the research lab. Return true if the research topic is set.
- public Instructor(String name, String phoneNumber, String courseId) : A constructor to
Write a tester program Task2Tester to test the classes you have written in Question 2. Create objects
from the classes Student 2 and Instructor above. This tester class is no t to be submitted.
What to submit:
Please complete the Questions 1 and 2 according to the requirements, documenting them properly using
JavaDoc. Please define the classes and methods using the exact names and signatures given here.
Compile and test your code under the command line environment (e.g., by executing Terminal under
macos or cmd under Windows).
- Submit all 4 source files: Student.java , Person. java, Student 2 .java, and Instructor.java.
Nothing else (including the tester programs) should be submitted. - DO NOT submit .class files! Submit .java files ONLY.
Important to Remember:
1. Your assignment will be given a zero mark if only the compiled files (.class files) are submitted,
as we have no way to look at your code. Please make sure to submit the source files (.java files).
No regrading requests will be entertained if this rule is not followed. No exceptions.
2. Please make sure your code compiles under the command line (i.e., without any IDE, such as
Eclipse, IntelliJ IDEA, and BlueJ). Do not put any package statement at the beginning of your
source file. If you are using an IDE, this is especially important because some IDEs put your code
under a particular package for your project. Any code that does not compile under the command
line can only receive 20/100. Regrading requests like “…but the code works on my
computer/in my IDE” will not be entertained. No exceptions.
Marking Scheme:
Style (variable naming, indentation, & layout) _/
JavaDoc Comments _/
Code Compiles? _(yes/no)
Successful Execution of Test Cases _/
Total _/
According to this marking scheme the maximum mark you can get for code that does not compile is
20/100.