Question: Use the Java hierarchy you posted in week 5 (corrected based on any feedback you may have received). Add a user-defined exception that can be

Use the Java hierarchy you posted in week 5 (corrected based on any feedback you may have received). Add a user-defined exception that can be thrown by one of the methods as part of the validation or error checking. The main method should then create an instance of the class and call the method in such a way that the exception is thrown (e.g. invalid input or state of the system). Submit your program as an attached .java file and post a screenshot to show that you have been able to successfully run that program. Make sure your submission adheres to the Submission Requirements document.

public class Employee {

String empName;

double salary;

//constructor of Employee

Employee(String empName, double salary) {

this.empName = empName;

this.salary = salary;

}

//function to return the employee name

String getName() {

return empName;

}

//function to return the salary

double getSalary() {

return salary;

}

public String toString() {

String output = "";

output = output + "Name: " + getName() + " ";

output = output + "Salary: $" + getSalary() + " ";

return output;

}

}

class Faculty extends Employee {

String deptName;

//constructor of the Faculty

Faculty(String nm, double sal, String dnm) {

//passing the name and salary to superclass constructor

super(nm, sal);

deptName = dnm;

}

//function to return department name

String getDeptName() {

return deptName;

}

//override toString method to include department name

@Override

public String toString() {

return super.toString() + "Department Name: " + getDeptName() + " ";

}

}

class Staff extends Employee {

String jobTitle;

//constructor of Staff class

Staff(String nm, double sal, String title) {

//passing the name and salary to superclass constructor

super(nm, sal);

jobTitle = title;

}

//function to return job title

String getJobTitle() {

return jobTitle;

}

//overload the toString method to include a boolean parameter

public String toString(boolean includeSalary) {

if (includeSalary) {

return super.toString() + "Job Title: " + getJobTitle() + " ";

} else {

return "Job Title: " + getJobTitle() + " ";

}

}

}

public class Main {

public static void main(String[] args) {

Faculty faculty = new Faculty("John ", 60000, "Mathematics");

System.out.println(faculty.toString());

Staff staff = new Staff("Smith", 40000, "Administrative Assistant");

System.out.println(staff.toString());

System.out.println(staff.toString(false));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!