Assignment 04: Modeling students and departments
Info
Due Fr 26 April 23.59
- Download the starter code and extract the .zip file's contents.
For this assignment you will work various other .js
files in the project folder. All of these files are imported by index.js
. I have included index.js
in the provided HTML.
Your next task is to implement two JavaScript classes used to model students and departments in our app.
Task 1: Student class
Implement the Student class in student.js
, and export it as the default (and only) export. An instance of Student will have the following properties (instance variables), which are all "public":
userId:
The student's userId
givenName:
The student's given name
surname:
The student's surname
dept:
The name of the department the student is declared in (a string), or null if the student has not declared yet
ectsCompleted:
The number of ECTS the student has completed (a number)
isAlum:
true if the student has graduated, false if not
An instance has the following methods:
Deliverables
constructor(userID, givenName, surname):
Construct a new Student. The student's userId, given name, and surname are as passed in. Students start undeclared, ungraduated, and with zero ects completed.fullName():
Return the student's full name, which is their given name, followed by a space, followed by their surname.
addEcts(ects):` Increase the number of ECTS the student has completed by the parameter ects.toString():
Return a string representing the student, which should be their given name, a space, their surname, another space, and their userId in parentheses. (For example, "Michael Chang (mchang91)")canGraduate():
Return a boolean indicating whether the student can graduate. A student can graduate if they have declared and have completed at least 180 ECTS. If the student has already graduated, this method should throw an Error with a descriptive message.
Task 2: Department class
Now write the Department class and export it as the default export in dept.js. An instance of Department has the following "public" properties:
name:
The full name of the department
code:
The department code
students:
An array of the current (non-alum) students who have declared under this department
Implement the following methods:
Deliverables
constructor(name, code):
Construct a new department with the name and code passed in. Departments start with no current students.toString():
Return the string representation of the department, which is just the department's name.declare(student):
Declare the passed-in Student instance under this department, updating the student's and department's instance variables accordingly. If the student is already declared under this department, this method should return without doing anything. But if the student is already declared under a different department, this method should throw an Error with a descriptive error message.graduate():
Check if each current student in the department can graduate; if they can, mark them as an alum and remove them from the list of current students. Return an array of the just-graduated Student instances.
Some notes on these tasks:
- You may add additional instance variables and methods to these classes if you wish, but you are not required to.
- It is a good practice to reuse existing functionality where possible. For example, notice how a Student's toString contains their full name.
- You may assume that a client of these classes will not modify the instance variables in a way that makes the instance inconsistent. For example, a client will not directly set a student's dept without going through declare.
- You aren't required to do any error checking beyond what is described here.
Review the slides of lecture 04 for the syntax for throwing Errors. Your errors should generate exceptions that can be caught and handled by the client of your class (which you will do in the next part).
Task 3: The App class
Finally, you'll put this app together by implementing the App class (in app.js).
An instance of the App class is constructed by index.js when the page loads. App has the following instance variables:
students:
An Object mapping students' userIDs (keys) to corresponding Student instances.
depts:
An Object mapping department codes (keys) to corresponding Department instances.
The constructor has been started for you; you will add to it in the next task. First, implement the following methods:
Deliverables
loadData(data):
Takes an object with the three keys from the sample data (students, depts, and ects) and populates the app's instance variables:- First, reset the students and depts instance variables to be empty. This allows loadData to "reset" the app.
- Populate the students map with new Student instances, using the students data.
- Update the number of ects each student has completed via the ects data.
Populate the depts map with new Department instances, based on the passed-in data.
declare(userId, deptCode):
Takes an userId and department code (both strings) and tries to declare that student under that department, then returns the (updated) Student instance. If the userId or department code isn't known to the app, throw an Error with a descriptive message. (If calling declare on the Department causes an error, don't handle it here.)
graduate(deptCode):
Make the department specified by the passed-in code try to graduate its students, returning the list of graduates (Student instances). If the department code doesn't match a known department, throw an Error with a descriptive message.
You will also need to add imports for Student and Department.
Testing Task 1-3
After completing these methods, you will be able to test your app from end to end:
- The app instance is exposed to the console. You can inspect its instance variables and call its methods.
- For example, you could call app.declare(“rschraml", "CS") or app.graduate(“CS"). (Note that loadData is already called for you in index.js.)
- The function testApp, exposed to the console, will attempt to test a variety of methods of Task 1-3. If anything doesn't match, it will print out an error message. These tests aren't comprehensive.
- Important: If you passed all tests add yourself to data.js. At the end of the present tests in index.js declare your department check if worked same as shown in the already present tests.
Submitting
Upload your assignment04_js folder to your public_html on your meise. Finally, provide a link named Assignment 04 on your personal page which refers to assignment04_js/index.html
. I will visit your site and check if testApp()
works as expected.