Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the Java hierarchy you posted BELOW . Add a user-defined exception that can be thrown by one of the methods as part of the

Use the Java hierarchy you posted BELOW . 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 you include a screen shot in IDE eclipse enviroment.

//CREATING CLASS HIERACHY

//Superclass: UEmployee

//Subclasses: Faculty, Staff

import java.util.*;

class UEmployee {

//declaration of name and salary

String empName;

double salary;

//constructor of UEmployee

UEmployee(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 UEmployee {

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;

}

//overriding toString() method to include department name

public String toString() {

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

}

}

class Staff extends UEmployee {

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;

}

//overloading toString() method to include job title

public String toString(String prefix) {

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

}

}

//test or driver class

class Test {

public static void main(String args[]) {

//accepting the required values from user using Scanner class

String nameF, nameS, dname, title;

double salF, salS;

Scanner sc = new Scanner(System.in);

System.out.print(" Enter the faculty name: ");

nameF = sc.nextLine();

System.out.print("Enter the salary: ");

salF = sc.nextDouble();

System.out.print("Enter the department name: ");

dname = sc.next();

sc.nextLine();

System.out.print(" Enter the staff name: ");

nameS = sc.nextLine();

System.out.print("Enter the salary: ");

salS = sc.nextDouble();

System.out.print("Enter the Job title: ");

title = sc.next();

//Instantiation of Faculty class

Faculty fac = new Faculty(nameF, salF, dname);

System.out.println(" Details of Faculty:");

System.out.println("----------------------------------");

System.out.println(fac);

//Instantiation of staff class

Staff st = new Staff(nameS, salS, title);

System.out.println(" Details of Staff:");

System.out.println("----------------------------------");

System.out.println(st.toString("Staff Details: "));

System.out.println(" ");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions