Java17 Assignment Instructions
Java17 Assignment Instructions
Note: As this is not a UML course, the above diagram is an overview. Also, for those of you for which English is not you first language and/or you are not
familiar with Universities, a Faculty contains several Departments.
Page 1 of 6
The following instructions will help in coding the assignment:
Page 2 of 6
LecturerRecord
o final by default (insert final anyway)
o the record contains 4 instance variables in this order:
a String name
an Integer age
a Faculty reference called faculty
a Department reference called dept
o insert a custom compact constructor:
if the name passed in is blank (hint: use the isBlank() method) or the age passed in
negative we will throw an IllegalArgumentException with a custom message. The
custom message is built as follows:
using a text block we can use the String method formatted to insert both the
name and age parameters into the custom error message; this can be done as
follows:
o hasPhd() method:
we need to cater for someone using “Dr.” at the start of their name or “PhD” at the
end of their name.
figure out the prefix (first 3 characters) and suffix (last 3 characters) in the name.
Hint: one option is to use the substring() method from String
using nested switch expressions, return true if the lecturers name begins with “Dr.”
or ends with “PhD”; return false otherwise.
o whichFaculty() method:
switch on the faculty:
using a switch expression pattern matching:
o if it’s the EngineeringFaculty then in a code block do the following:
call toString() on the reference, prepended with
“Faculty of: “ i.e. assuming eng is the reference, code
System.out.println(“Faculty of: “+eng);
call the custom method engineering()
o if it’s the HumanitiesFaculty then in a code block do the following:
call toString() on the reference prepended with “Faculty of: “
call the custom method humanities()
o if it’s the BusinessFaculty then in a code block do the following:
call toString() on the reference prepended with “Faculty of: “
call the custom method business()
o otherwise, throw an IllegalArgumentException, outputting the faculty
that is causing the error in the error message.
Page 3 of 6
o whichDept() method:
switch on the department:
using a switch expression pattern matching:
o if it’s the ComputerEngineeringDept then in a code block do the
following:
call toString() on the reference prepended with “Dept of: “
call the custom method compEng()
o if it’s the SoftwareEngineeringDept then in a code block do the
following:
call toString() on the reference prepended with “Dept of: “
call the custom method swEng()
o if it’s the SocialCareDept then in a code block do the following:
call toString() on the reference prepended with “Dept of: “
call the custom method socialCare()
o if it’s the AccountingDept then in a code block do the following:
call toString() on the reference prepended with “Dept of: “
call the custom method accounting()
o otherwise, throw an IllegalArgumentException, outputting the
department that is causing the error in the error message.
University
o in the main() method do the following:
Force an exception by creating a LecturerRecord that has either a blank name and/or a
negative age. Test both scenarios. Sample output:
Page 4 of 6
Create a LecturerRecord with the following details: the lecturers name is “Jane
Bloggs”; she is 24; she works in the Engineering faculty and is in the Software
Engineering Department. Output the details by calling toString(). Sample output:
Now, rather than using toString(), we will output Jane’s details individually by calling
each accessor method in turn. Sample output:
Create a record for “Dr. Anne Bloggs”; she is 35 and works in the Accounting
department in the faculty of Business. Output her details (toString()) and whether or
not she has a PhD. In this case we will decorate the output i.e. rather than simply
returning true/false, we will use a ternary operator to output “Ann has a PhD” or
“Anne has not a PhD”, depending on true/false respectively. Sample output:
Page 5 of 6
“Joe Bloggs PhD” is 54 and is a member of staff in the Social Care department in the
faculty of Humanities. Create a record representing him. Output his details
(toString()) and whether or not he has a PhD (again, decorate the output). Sample
output:
Page 6 of 6