Question

In: Computer Science

In Angel, you will find a class called Employee. This class should include a constructor which...

In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method).

You will create a new class called Manager that inherits from the class Employee. Add a field named department of type String. Supply a method toStringthat prints the manager's name, department, and salary. Remember, you may not change anything in the Employee class.

You will then create a test class that uses the Manager class. The test class should prompt the user to enter name, department and salary. This information should be stored in an array. Upon entry of 3 employee records, the program should output the information you entered.

Your program should be able to handle a maximum of 3 entries.

You may write the program as a console application or using dialog boxes.

Please remember to include the needed comments at the top of your .java file which will identify this

Hand in:

1. A typed copy of the algorithm

2. The printed copy of your UML Class Diagram

3. The printed copy of your working source code with comments

Solutions

Expert Solution

1. ALGORITHM:

Algorithm to create 3 Manager objects which inherits the employee class:

Start:

Step 1:

1. Create class Employee

2. Add instance variable name (string) and salary(number)

3. Create constructor to set name to blank and salary = 0

4. Create another constructor to set name and salary same as passed parameter values

5. Create setters for name and salary which sets the value of name and salary same as passed variables as parameters.

6. Create getters for name and salary which returns name and salary.

7. Create a toString method which displays employee name and employee salary in formatted output

Step 2:

8. Create a new class Manager which extends Employee

9. Add instance variable department(string)

10. Create a getter for department that returns the department value

11. Create a setter for department that sets the department value same as passed in parameter.

12. Create a toString method which calls Employee’s toString() and add deprtament details in it.

Step 3:

13. Create last class TestManager

14. Create array of Manager class object having size 3.

15. Create name, salary and department for all objects and put in array.

16. Display the manager details for all 3 manager objects using toString method.

2. CLASSDIAGRAM:

The given problem has two classes Employee and Manager where the manager extends the Employee class. All the data members and member functions are shown in the below diagram:

3. PROGRAM:

The solution will have 3 classes Employee, Manager and TestManager where TestManager is driver class to call the other classes by instantiating their objects.

Employee.java:

public class Employee {

                private String name;

                private int salary;

                //default constructor

                public Employee(){

                                name = "";//set name to blank

                                salary = 0;//set salary to 0

                }

               

                public Employee(String name, int salary){//parameterised constructor

                                this.name=name;//set name same as passed name

                                this.salary=salary;//set salary same as passed salary

                }

                //getters for the instance variables

                public String getName() {

                                return name;//return name

                }

               

                public int getSalary() {

                                return salary; //return salary

                }

                //setter for setting the values

                public void setName(String name) {

                                this.name = name;//set name as passed variable

                }

                public void setSalary(int salary) {

                                this.salary = salary;//set salary as passed variable

                }

               

                public String toString() {//return formatted details about employee

                                return "\n\n================\n\nEmployee details:\n================\nEmployee Name: " + name + "\nSalary: " + salary ;

                }             

}

Manager.java:

public class Manager extends Employee{

                private String department;         

                //getter for department

                public String getDepartment() {

                                return department; //return department

                }

                //setter for department

                public void setDepartment(String department) {

                                this.department = department;//set department same as passed value

                }

                @Override

                public String toString() {//print formatted details of manager employee

                                return super.toString()+"\nType: Manager\nDepartment: " + department;//super.toString will print the employee other details

                }             

}

TestManager.java:

import java.util.Scanner;//to use scanner object while reading inputs

public class TestManager {

                public static void main(String[] args) {

                                Scanner scObject = new Scanner(System.in);//create object of Scanner to read inputs

                                //local variables to read the values from user

                                String nm, dept;

                                int salary;

                               

                                Manager[] managerArray = new Manager[3];//and array of employees which holds 3 objects

                                managerArray[0] = new Manager();//put first object of manager at index 0

                                managerArray[1] = new Manager();//put second object of manager at index 1

                                managerArray[2] = new Manager();//put third object of manager at index 2

                               

                                for(int i = 0; i < 3; i++) {

                                                System.out.println("Enter details for manager: "+ (i+1)+"\n=============\n");

                                                System.out.print("Enter Name: ");

                                                nm = scObject.nextLine();

                                                System.out.print("Enter Salary: ");

                                                salary = scObject.nextInt();

                                                scObject.nextLine();//remove \n after reading an integer

                                                System.out.print("Enter Department: ");

                                                dept = scObject.nextLine();

                                               

                                                //set the read out values to the manager object at index i

                                                managerArray[i].setName(nm);

                                                managerArray[i].setSalary(salary);

                                                managerArray[i].setDepartment(dept);

                                                }//end for when objects are set for all employees

                               

                                for(int i = 0; i < 3; i++) {//this loop will print all 3 objects of manager stores in array

                                                System.out.println(managerArray[i]);

                                }//end for

                }//end main

}//end class

4. OUTPUT:


Related Solutions

In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor overloading) for initializing objects with different parameters. The employee class should have different functions for saving and updating bio-data, educational background, and current status like experience, Salary, position & travel history. The employee class should have a function which asks user what do he likes to see about employee (.i.e., Bio-data, current status....) and only show the respective data. Create two objects of interns...
in JAVA PLEASE SHOW OUTPUT! PriorityQueueUserDefinedObjectExample (20) Create an Employee class which implements Comparable<Employee> The constructor...
in JAVA PLEASE SHOW OUTPUT! PriorityQueueUserDefinedObjectExample (20) Create an Employee class which implements Comparable<Employee> The constructor consists of an employee’s first name and an employee’s salary, both of which are instance variables. Create accessor and mutator methods for both of these variables Write an equals method that returns true if the salaries are equal with one cent and the names are exactly equal Write a compareTo method that returns 1 if the salary of this employee is greater than the...
Part I: The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class.
  Part I:   The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class. * Update the function display() to be a virtual function     * Add function, double weeklyEarning() to be a pure virtual function. This will make Employee an abstract class, so your main will not be able to create any Employee objects. Part II: The Derived Classes Add an weeklyEarning() function to each...
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee...
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee to represent when they joined the company. Use the Joda-Time library class LocalDate as the type for this variable. See http://www.joda.org/joda-time/index.html for details end examples of how to use this library. You may also have to download the Joda-Time library (JAR File) and include this in your CLASSPATH or IDE Project Libraries. Use a static variable in the Employee class to help automatically assign...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
1. A constructor is a special Class member method. It is automatically called when an object...
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented...
Create a class called employee which has the following instance variables: Employee ID number Salary Years...
Create a class called employee which has the following instance variables: Employee ID number Salary Years at the company Your class should have the following methods: New employee which reads in an employee’s ID number, salary and years of service Anniversary which will up the years of service by 1 You got a raise which will read in how much the raise was (a percent) and then calculate the new salary You get a bonus which gives a yearly bonus...
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT