Worker class and average salary
HardClasses20 min
Write a class Worker with the fields name (string), age (number) and salary (number).
- Constructor:
new Worker(name, age, salary). - A getter and a setter for every field:
worker.salaryreturns the salary,worker.salary = 2000changes it. Usegetandset, not plain properties. toString()returns a string exactly likeWorker{name='Ann', age=30, salary=1500}. Numbers are printed the default JavaScript way:1500.5, not1500.50.- The function
averageSalary(workers)takes an array ofWorkerobjects and returns the average salary, not rounded. For an empty array it returns0. - Create an array
workerswith at least threeWorkerobjects.
Examples
new Worker('Ann', 30, 1500).toString()Returns "Worker{name='Ann', age=30, salary=1500}"
averageSalary([new Worker('Ann', 30, 1000), new Worker('Bob', 41, 2000)])Returns 1500
averageSalary([])Returns 0
Asked at EPAM
Loading the editor...
- The constructor stores the fields, getters return them
- Setters change the values
- name, age and salary are get/set accessors
- toString format
- Average salary of two workers
- Empty list: 0
- The workers array holds Worker objects
Hidden tests run on Submit