Student Solution Exercises 5
1
2
3
4
5
6
1
1 2 3
0 1 2
0 1 2 3
1 4 5 6
7.10 (Sales Commissions) Use a one-dimensional array to solve the following problem: A compa-
ny pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of
their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week re-
ceives $200 plus 9% of $5000, or a total of $650. Write an application (using an array of counters)
that determines how many of the salespeople earned salaries in each of the following ranges (assume
that each salesperson’s salary is truncated to an integer amount):
a) $200–299
b) $300–399
c) $400–499
d) $500–599
e) $600–699
f) $700–799
g) $800–899
h) $900–999
i) $1000 and over
Summarize the results in tabular format.
ANS:
1 // Exercise 7.10 Solution: [Link]
2 // Program calculates the amount of pay for a salesperson and counts the
3 // number of salespeople that earned salaries in given ranges.
4 import [Link];
5
6 public class Sales
7 {
8 // counts the number of people in given salary ranges
9 public void countRanges()
10 {
11 Scanner input = new Scanner( [Link] );
12
13 int total[] = new int[ 9 ]; // totals for the various salaries
14
15 // initialize the values in the array to zero
16 for ( int counter = 0; counter < [Link]; counter++ )
17 total[ counter ] = 0;
18
19 // read in values and assign them to the appropriate range
20 [Link]( "Enter sales amount (negative to end): " );
6 Chapter 7 Arrays
21 double dollars = [Link]();
22
23 while ( dollars >= 0 )
24 {
25 double salary = dollars * 0.09 + 200;
26 int range = ( int ) ( salary / 100 );
27
28 if ( range > 10 )
29 range = 10;
30
31 ++total[ range - 2 ];
32
33 [Link]( "Enter sales amount (negative to end): " );
34 dollars = [Link]();
35 } // end while
36
37 // print chart
38 [Link]( "Range\t\tNumber" );
39
40 for ( int range = 0; range < [Link] - 1; range++ )
41 [Link]( "$%d-$%d\t%d\n",
42 (200 + 100 * range), (299 + 100 * range), total[ range ] );
43
44 // special case for the last range
45 [Link]( "$1000 and over\t%d\n",
46 total[ [Link] - 1 ] );
47 } // end method countRanges
48 } // end class Sales
1 // Exercise 7.10 Solution: [Link]
2 // Test application for class Sales
3 public class SalesTest
4 {
5 public static void main( String args[] )
6 {
7 Sales application = new Sales();
8 [Link]();
9 } // end main
10 } // end class SalesTest
Enter sales amount (negative to end): 5000
Enter sales amount (negative to end): -1
Range Number
$200-$299 0
$300-$399 0
$400-$499 0
$500-$599 0
$600-$699 1
$700-$799 0
$800-$899 0
$900-$999 0
$1000 and over 0