Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q3 [15 points] . Implement the following UML class diagram in Java. Implement a user defined exception class called InvalidSalaryException. The class must have two

Q3 [15 points]. Implement the following UML class diagram in Java.

image text in transcribed

  1. Implement a user defined exception class called InvalidSalaryException. The class must have two constructors, a constructor that takes no parameters, and a constructor that takes a string message. These constructors should call the corresponding constructors from the super class java.lang.Exception.

  1. Make the class Employee as a subclass (i.e., extends) of the class Person.

  1. Implement the methods of the class Person, as follows:

  1. Constructor public void Person(String name, double age)

This constructor initializes the protected fields this.name and this.age appropriately using the formal parameters of the constructor.

  1. public void setName(String name)

This method sets the value for the protected field this.name.

  1. public void setAge(double age)

This method sets the field this.age to age.

  1. Implement the methods of the class Employee, as follows:

  1. public Employee(String name, double age, double salary)

This constructor initializes the class attributes this.name and this.age, and, this.salary appropriately. Note that some of these attributes are inherited from the Person class.

  1. public void setSalary(double salary)

This method throws an InvalidSalaryException if the value of the parameter salary is less than zero. Otherwise, it sets the this.salary attribute to the parameter salary. You should change the method signature to include the corresponding throws expression.

  1. public void setName(String name)

This method MUST call the method setName() of the super class Person to set the filed this.name to the parameter name.

  1. Implement a main method in the class Empoyee, as follows:
    1. Add a try-catch blocks. The try block must include the following statements:
      1. Create and instantiate an object of type Employee with the values Ahmed, 20, 300.
      2. Call the method setAge() on the object, where the actual parameter is 20.
      3. Call the method setSalary() on the object, where the actual parameter is 400.
    2. The catch block should catch InvalidSalaryException exception, and it should call the method printStackTrace().
Per son protected name: String protected age: double public Person(String name, double age) public setName(String name) public setAge(double): void A Employee private salary: double public Employee(String, double, double) public setSalary(double): void public setName(String name) InvalidSalaryException public Invalid SalayException public InvalidSalavException(String)

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

More Books

Students also viewed these Databases questions

Question

3. Job rotation is used for all levels and types of employees.

Answered: 1 week ago