Employees at a store are paid daily wages according to the following rules.


Each employee is paid the same fixed amount per day.

Each employee is paid an additional amount for each item they sold on that day.

Daily Bonus:

If the number of items sold that day by an employee is greater than a computed threshold, then the employee also receives a bonus equal to 10 percent of the employee’s daily wages.

You will write two methods in the Payroll class below.


public class Payroll


{


private int[] itemsSold; // number of items sold by each employee


private double[] wages; // wages to be computed in part (b)




/** Returns the bonus threshold as described in part (a).


*/


public double computeBonusThreshold()


{


/* To be implemented in part (a) */


}




/** Computes employee wages as described in part (b)


* and stores them in wages.


* The parameter fixedWage represents the fixed amount each employee


* is paid per day.


* The parameter perItemWage represents the amount each employee


* is paid per item sold.


*/


public void computeWages(double fixedWage, double perItemWage)


{


/* To be implemented in part (b) */


}




// Other instance variables, constructors, and methods not shown.


}


The bonus threshold is calculated based on the number of items sold by all employees on a given day. The employee with the greatest number of sales and the employee with the least number of sales on that day are ignored in the calculation. The average number of items sold by the remaining employees on that day is computed, and this value is used as the bonus threshold.


For a given day, the number of items sold by each employee is stored in the array itemsSold. The example below shows the contents of itemsSold for a day in which there were ten employees. Each array index represents an individual employee. For example, itemsSold[3] represents the number of items sold by employee 3.


The figure presents an array titled “itemsSold.” The array has 10 indices, each representing an employee, numbered 0 through 9. The data are as follows.


Employee 0, 48 items sold. Employee 1, 50 items sold. Employee 2, 37 items sold. Employee 3, 62 items sold. Employee 4, 38 items sold. Employee 5, 70 items sold. Employee 6, 55 items sold. Employee 7, 37 items sold. Employee 8, 64 items sold. Employee 9, 60 items sold.

Based on the information in the table, the bonus threshold is calculated as follows.


(48+50+37+62+38+70+55+37+64+60)−37−708=51.75


(a) Complete the method computeBonusThreshold below, which is intended to return the bonus threshold based on the contents of the itemsSold array. Assume that itemsSold has been filled appropriately, and that the array contains at least three employees.


/** Returns the bonus threshold as described in part (a).


*/


public double computeBonusThreshold()


The computeWages method is intended to calculate the wages for each employee and to assign them to the appropriate element of the array wages. For example, wages[3] should be assigned the wages for employee

3

. An employee’s wages consist of their daily wages plus a possible bonus and are calculated as follows.


Each employee’s wages are equal to the fixed wage plus the number of items sold times the amount paid per item sold.

If the employee sold more items than the bonus threshold, the employee also receives a 10 percent bonus added to their wages.

As described in part (a), computeBonusThreshold() returns 51.75 for the example array below.


The figure presents an array titled “itemsSold.” The array has 10 indices, each representing an employee, numbered 0 through 9. The data are as follows. Employee 0, 48 items sold. Employee 1, 50 items sold. Employee 2, 37 items sold. Employee 3, 62 items sold. Employee 4, 38 items sold. Employee 5, 70 items sold. Employee 6, 55 items sold. Employee 7, 37 items sold. Employee 8, 64 items sold. Employee 9, 60 items sold.

Suppose that fixedWage is 10.0 and perItemWage is 1.5.


Employee 0 did not sell more items than the bonus threshold, so employee 0’s wages are equal to 10.0 + 1.5 * 48, which evaluates to 82.0. This value will be assigned to wages[0].

Employee 9 sold more items than the bonus threshold, so employee 9 receives a 10 percent bonus. Employee 9’s wages are equal to (10.0 + 1.5 * 60) * 1.1, or 110.0. This value will be assigned to wages[9].


(b) Write the method computeWages. Assume that itemsSold has been filled appropriately, and there are at least three employees in the array. Assume also that the wages array and the itemsSold array have the same length. Your solution must call computeBonusThreshold appropriately to receive full credit.


/** Computes employee wages as described in part (b)


* and stores them in wages.


* The parameter fixedWage represents the fixed amount each employee


* is paid per day.


* The parameter perItemWage represents the amount each employee


* is paid per item sold.


*/


public void computeWages(double fixedWage, double perItemWage)

Respuesta :

Answer:

See explaination

Explanation:

public class Payroll

{

private int[] itemsSold;

private double[] wages;

//constructor is just kept for demonstrating the code output

public Payroll()

{

itemsSold = new int[] {48,50,37,62,38,70,55,37,64,60};

wages = new double[10];

}

/*

to compute Bonus Threshold, you have to find sum Of Items Sold, highest and lowest

deduct highest and lowest from sum Of Items Sold and then divide by number of items -2

to find the bonus Threshold

*/

public double computeBonusThreshold()

{

int highest = itemsSold[0];

int lowest = itemsSold[0];

int sumOfItemsSold = itemsSold[0];

double bonusThreshold =0;

//iterate over items , find sum of items sold and highest and lowest

for (int i = 1; i < itemsSold.length ; i++)

{

sumOfItemsSold += itemsSold[i];

if(itemsSold[i] > highest)

highest = itemsSold[i];

if(itemsSold[i] < lowest )

lowest = itemsSold[i];

}

bonusThreshold = (sumOfItemsSold - lowest - highest ) / (itemsSold.length -2.0);

return bonusThreshold;

}

/*

Compute wages as asked, wage for an employee is fixed wage added with itemSold * perItemWage

When an employee sold items more than bonus Threshold Items then he will get extra 10%

*/

public void computeWages(double fixedWage, double perItemWage)

{

double bonusThresholdForWages = computeBonusThreshold();

for (int i = 0; i < wages.length ; i++)

{

wages[i] = fixedWage + ( itemsSold[i] * perItemWage);

if(itemsSold[i] > bonusThresholdForWages)

{

wages[i] = wages[i] * 1.1 ; //which is 10 % extra of current wage of employee

}

}

}

/*

Method to show itemsold, wages for the employee

*/

public void printWages()

{

for (int i = 0; i < wages.length ; i++)

{

System.out.printf("Employee =%d , ItemsSold = %d, Wage = %.2f\n",i,itemsSold[i], wages[i]);

}

}

}

=========main.java======

public class Main

{

public static void main(String[] args) {

//create an object of payroll class, where itemsSolds are hard coded for demo

Payroll payroll = new Payroll();

System.out.println("Bonus threshold items are : " + payroll.computeBonusThreshold());

//call computeWages which will internally class bonus threshold as well

payroll.computeWages(10.0,1.5);

payroll.printWages();

}

}

See attachment for screenshot of codes for indentation.

Ver imagen kendrich
Ver imagen kendrich