Skip to content

Commit 7f26ed4

Browse files
committed
Update solutions
1 parent a2da6f8 commit 7f26ed4

5 files changed

+232
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* (Financial application: compute commissions) Write a method that computes the
3+
* commission, using the scheme in Programming Exercise 5.39. The header of
4+
* the method is as follows:
5+
* public static double computeCommission(double salesAmount)
6+
* Write a test program that displays the following table:
7+
* Sales Amount Commission
8+
* 10000 900.0
9+
* 15000 1500.0
10+
* ...
11+
* 95000 11100.0
12+
* 100000 11700.0
13+
*
14+
* Created by Sven on 07/07/19.
15+
*/
16+
package Chapter06;
17+
18+
public class Exercise0611_Financial_application_compute_commissions {
19+
public static void main(String[] args) {
20+
System.out.println("Sales Amount Commission");
21+
System.out.println("--------------------------");
22+
for (int i = 10000; i <= 100000; i += 5000) {
23+
System.out.printf("%-16d%10.1f\n", i, computeCommission(i));
24+
}
25+
}
26+
27+
public static double computeCommission(double salesAmount) {
28+
double commission = 0.0;
29+
if (salesAmount > 10000.0) {
30+
commission = 700.0 + (salesAmount - 10000.0) * 0.1;
31+
} else if (salesAmount > 5000.0) {
32+
commission = 300.0 + (salesAmount - 5000.0) * 0.08;
33+
} else {
34+
commission = salesAmount * 0.06;
35+
}
36+
return commission;
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* (Display numbers) Write a method that prints numbers using the following header:
3+
* public static void printNumbers(int num1, int num2, int
4+
* numberPerLine)
5+
* This method prints the characters between num1 and num2 with the specified
6+
* numbers per line. Write a test program that prints ten characters per line from 1
7+
* to 100. Numbers are separated by exactly one space.
8+
*
9+
* Created by Sven on 07/07/19.
10+
*/
11+
package Chapter06;
12+
13+
public class Exercise0612_Display_numbers {
14+
public static void main(String[] args) {
15+
printNumbers(1, 100, 10);
16+
}
17+
18+
public static void printNumbers(int num1, int num2, int numberPerLine) {
19+
int count = 0;
20+
for (int i = num1; i <= num2; i++) {
21+
System.out.print(i + " ");
22+
count++;
23+
if (count % numberPerLine == 0) {
24+
System.out.println();
25+
}
26+
}
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* (Sum series) Write a method to compute the following series:
3+
* m(i) = 1/3 +2/4 + ... + i/i+2
4+
* Write a test program that displays the following table:
5+
* I m(i)
6+
* 1 0.3333
7+
* 2 0.8333
8+
* ...
9+
* 19 14.7093
10+
* 20 15.6184
11+
*
12+
* Created by Sven on 07/07/19.
13+
*/
14+
package Chapter06;
15+
16+
public class Exercise0613_Sum_series {
17+
public static void main(String[] args) {
18+
System.out.println("i m(i)");
19+
System.out.println("---------");
20+
for (int i = 1; i <= 20; i++) {
21+
System.out.printf("%-5d%.4f\n", i, computeSum(i));
22+
}
23+
}
24+
25+
public static double computeSum(int i) {
26+
double sum = 0.0;
27+
for (int j = 1; j <= i; j++) {
28+
sum += (double) j / (double) (j + 2);
29+
}
30+
return sum;
31+
}
32+
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* (Estimate p) p can be computed using the following summation:
3+
* m (i) = 4 (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + ((-1)^(i+1))/(2i-1))
4+
* Write a method that returns m(i) for a given i and write a test program that
5+
* displays the following table:
6+
* i m(i)
7+
* 1 4.0000
8+
* 101 3.1515
9+
* 201 3.1466
10+
* 301 3.1449
11+
* 401 3.1441
12+
* 501 3.1436
13+
* 601 3.1433
14+
* 701 3.1430
15+
* 801 3.1428
16+
* 901 3.1427
17+
*
18+
* Created by Sven on 07/07/19.
19+
*/
20+
package Chapter06;
21+
22+
public class Exercise0614_Estimate_PI {
23+
public static void main(String[] args) {
24+
System.out.println("i m(i)");
25+
System.out.println("---------");
26+
for (int i = 1; i <= 901; i += 100) {
27+
System.out.printf("%-5d%.4f\n", i, computePI(i));
28+
}
29+
}
30+
31+
public static double computePI(int i) {
32+
double sum = 0.0;
33+
for (int j = 1; j <= i; j++) {
34+
sum += 4 * (Math.pow(-1, j + 1) / (2 * j - 1));
35+
}
36+
return sum;
37+
}
38+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* (Financial application: print a tax table) Listing 3.5 gives a program to compute
3+
* tax. Write a method for computing tax using the following header:
4+
* public static double computeTax(int status, double taxableIncome)
5+
* Use this method to write a program that prints a tax table for taxable income from
6+
* $50,000 to $70,000 with intervals of $100 for all the following statuses:
7+
* Taxable Single Married Joint Married Head of
8+
* Income or Qualifying Separate a House
9+
* Widow(er)
10+
* 50000 8688 6665 8688 7353
11+
* 50100 8713 6680 8713 7378
12+
* ...
13+
* 69900 13663 9850 12328 9840
14+
* 70000 13688 9875 12353 9853
15+
* Hint: round the tax into integers using Math.round (i.e., Math
16+
* .round(computeTax(status, taxableIncome))).
17+
*
18+
* Created by Sven on 07/07/19.
19+
*/
20+
package Chapter06;
21+
22+
public class Exercise0615_Financial_application_print_a_tax_table {
23+
public static void main(String[] args) {
24+
System.out.println("Taxable Income Single Married Joint or Qualifying Widow(er) Married Separate Head of a house");
25+
System.out.println("--------------------------------------------------------------------------------------------------------");
26+
for (int i = 50000; i <= 70000; i += 100) {
27+
System.out.printf("%-18d%-10d%-41d%-20d%d\n",
28+
i,
29+
Math.round(computeTax(0, i)),
30+
Math.round(computeTax(1, i)),
31+
Math.round(computeTax(2, i)),
32+
Math.round(computeTax(3, i)));
33+
}
34+
}
35+
36+
public static double computeTax(int status, double taxableIncome) {
37+
double tax = 0;
38+
39+
if (status == 0) { // Compute tax for single filers
40+
if (taxableIncome <= 8350)
41+
tax = taxableIncome * 0.10;
42+
else if (taxableIncome <= 33950)
43+
tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
44+
else if (taxableIncome <= 82250)
45+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (taxableIncome - 33950) * 0.25;
46+
else if (taxableIncome <= 171550)
47+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (taxableIncome - 82250) * 0.28;
48+
else if (taxableIncome <= 372950)
49+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (taxableIncome - 171550) * 0.33;
50+
else
51+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (taxableIncome - 372950) * 0.35;
52+
} else if (status == 1) { // Compute tax for married file jointly
53+
if (taxableIncome <= 16700)
54+
tax = taxableIncome * 0.10;
55+
else if (taxableIncome <= 67900)
56+
tax = 16700 * 0.10 + (taxableIncome - 16700) * 0.15;
57+
else if (taxableIncome <= 137050)
58+
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (taxableIncome - 67900) * 0.25;
59+
else if (taxableIncome <= 208850)
60+
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (taxableIncome - 137050) * 0.28;
61+
else if (taxableIncome <= 372950)
62+
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (taxableIncome - 208850) * 0.33;
63+
else
64+
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (taxableIncome - 372950) * 0.35;
65+
} else if (status == 2) { // Compute tax for married separately
66+
if (taxableIncome <= 8350)
67+
tax = taxableIncome * 0.10;
68+
else if (taxableIncome <= 33950)
69+
tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
70+
else if (taxableIncome <= 68525)
71+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (taxableIncome - 33950) * 0.25;
72+
else if (taxableIncome <= 104425)
73+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (taxableIncome - 68525) * 0.28;
74+
else if (taxableIncome <= 186475)
75+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (taxableIncome - 104425) * 0.33;
76+
else
77+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (taxableIncome - 186475) * 0.35;
78+
} else if (status == 3) { // Compute tax for head of household
79+
if (taxableIncome <= 11950)
80+
tax = taxableIncome * 0.10;
81+
else if (taxableIncome <= 45500)
82+
tax = 11950 * 0.10 + (taxableIncome - 11950) * 0.15;
83+
else if (taxableIncome <= 117450)
84+
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (taxableIncome - 45500) * 0.25;
85+
else if (taxableIncome <= 190200)
86+
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (taxableIncome - 117450) * 0.28;
87+
else if (taxableIncome <= 372950)
88+
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (taxableIncome - 190200) * 0.33;
89+
else
90+
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (taxableIncome - 372950) * 0.35;
91+
}
92+
93+
return tax;
94+
}
95+
}

0 commit comments

Comments
 (0)