Skip to content

Commit dc82e76

Browse files
adding more chapter 5
1 parent 354a680 commit dc82e76

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @author : Jonathan Birkey
3+
* @mailto : jonathan.birkey@gmail.com
4+
* @created : 24Dec2021
5+
*
6+
* Exercise 5.21
7+
* (Financial application: compare loans with various interest rates) Write a program
8+
* that lets the user enter the loan amount and loan period in number of years, and
9+
* displays the monthly and total payments for each interest rate starting from 5% to
10+
* 8%, with an increment of 1/8. Here is a sample run:
11+
*
12+
* Loan Amount: 10000
13+
* Number of Years: 5
14+
*
15+
* Interest Rate Monthly Payment Total Payment
16+
* 5.000% 188.71 11322.74
17+
* 5.125% 189.29 11357.13
18+
* 5.250% 189.86 11391.59
19+
* ...
20+
* 7.875% 202.17 12129.97
21+
* 8.000% 202.76 12165.84
22+
**/
23+
package com.github.jonathanbirkey.chapter05;
24+
25+
import java.util.Scanner;
26+
27+
public class Exercise21 {
28+
public static void main(String[] args) {
29+
Scanner input = new Scanner(System.in);
30+
System.out.print("Loan Amount: ");
31+
double loanAmount = input.nextDouble();
32+
System.out.print("Number of Years: ");
33+
int numOfYears = input.nextInt();
34+
input.close();
35+
36+
double annualInterestRate = 5;
37+
double totalPeriods = numOfYears * 12;
38+
39+
40+
System.out.printf("%-20s%-20s%s", "Interest Rate", "Monthly Payment", "Total Payment\n");
41+
while (annualInterestRate <= 8.0){
42+
double monthlyInterestRate = (annualInterestRate / 100) / 12;
43+
double monthlyPayment = loanAmount * (monthlyInterestRate * Math.pow(1+monthlyInterestRate, totalPeriods))
44+
/ (Math.pow(1+monthlyInterestRate, totalPeriods) - 1);
45+
double totalPayment = monthlyPayment * totalPeriods;
46+
System.out.printf("%-5.3f%-15s%-20.2f%.2f\n", annualInterestRate, "%", monthlyPayment, totalPayment);
47+
annualInterestRate += 0.125;
48+
}
49+
50+
51+
52+
}
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @author : Jonathan Birkey
3+
* @mailto : jonathan.birkey@gmail.com
4+
* @created : 27Dec2021
5+
*
6+
* Exercise 5.22
7+
* (Financial application: loan amortization schedule) The monthly payment for a given
8+
* loan pays the principal and the interest. The monthly interest is computed by multiplying
9+
* the monthly interest rate and the balance (the remaining principal). The principal paid
10+
* for the month is therefore the monthly payment minus the monthly interest. Write a program
11+
* that lets the user enter the loan amount, number of years, and interest rate then displays
12+
* the amortization schedule for the loan. Here is a sample run:
13+
*
14+
* Loan Amount: 10000
15+
* Number of Years: 1
16+
* Annual Interest Rate: 7
17+
*
18+
* Payment# Interest Principal Balance
19+
* 1 58.33 806.93 9193.07
20+
* 2 53.62 811.64 8381.43
21+
* ...
22+
* 11 10.00 855.26 860.27
23+
* 12 5.01 860.25 0.01
24+
**/
25+
package com.github.jonathanbirkey.chapter05;
26+
27+
import java.util.Scanner;
28+
29+
public class Exercise22 {
30+
public static void main(String[] args) {
31+
Scanner input = new Scanner(System.in);
32+
System.out.print("Loan Amount: ");
33+
double principal = input.nextDouble();
34+
System.out.print("Number of Years: ");
35+
int numberOfYears = input.nextInt();
36+
System.out.print("Annual Interest Rate: ");
37+
double interest = input.nextDouble();
38+
input.close();
39+
double totalPeriods = numberOfYears * 12;
40+
double monthlyInterestRate = (interest / 100) / 12;
41+
42+
double monthlyPayment = principal * (monthlyInterestRate * Math.pow(1+monthlyInterestRate, totalPeriods))
43+
/ (Math.pow(1+monthlyInterestRate, totalPeriods) - 1);
44+
double balance = monthlyPayment * totalPeriods;
45+
46+
System.out.printf("\nMonthly Payment: %.2f\nTotal Payment: %.2f\n\n",monthlyPayment, balance);
47+
System.out.printf("%-15s%-15s%-15s%s\n", "Payment#", "Interest", "Principal", "Balance");
48+
for (int i = 1; i <= numberOfYears * 12; i++) {
49+
interest = monthlyInterestRate * balance;
50+
principal = monthlyPayment - interest;
51+
balance = balance - principal;
52+
System.out.printf("%-15d%-15.2f%-15.2f%.2f\n", i, interest, principal, balance);
53+
}
54+
}
55+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @author : Jonathan Birkey
3+
* @mailto : jonathan.birkey@gmail.com
4+
* @created : 27Dec2021
5+
*
6+
* Exercise 5.23
7+
* (Demonstrate cancellation errors) A cancellation error occurs when you are
8+
* manipulating a very large number with a very small number. The large number
9+
* may cancel out the smaller number. For example, the result of 100000000.0
10+
* + 0.000000001 is equal to 100000000.0. To avoid cancellation errors and
11+
* obtain more accurate results, carefully select the order of computation. For
12+
* example, in computing the following summation, you will obtain more accurate
13+
* results by computing from right to left rather than from left to right:
14+
*
15+
* 1 + 1/2 + 1/3 + ... + 1/n
16+
*
17+
* Write a program that compares the results of the summation of the preceding
18+
* series, computing from left to right and from right to left with n = 50000.
19+
**/
20+
package com.github.jonathanbirkey.chapter05;
21+
22+
public class Exercise23 {
23+
public static void main(String[] args) {
24+
double n = 50000;
25+
double leftToRight = 1;
26+
for(double i = 2; i <= n; i++){
27+
leftToRight += 1/i;
28+
}
29+
System.out.printf("Summation from left to right: %f\n", leftToRight);
30+
31+
double rightToLeft = 0;
32+
for(double j = n; j >= 2; j--){
33+
rightToLeft += 1/j;
34+
}
35+
rightToLeft += 1;
36+
System.out.printf("Summation from right to left: %f\n", rightToLeft);
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @author : Jonathan Birkey
3+
* @mailto : jonathan.birkey@gmail.com
4+
* @created : 27Dec2021
5+
*
6+
* Exercise 5.24
7+
* (Sum a series) Write a program to compute the following summation:
8+
*
9+
* 1/3 + 3/5 + 5/7 + 7/9 + 9/11 + 11/13 + ... + 95/97 + 97/99
10+
**/
11+
package com.github.jonathanbirkey.chapter05;
12+
13+
public class Exercise24 {
14+
public static void main(String[] args) {
15+
double summation = 0;
16+
for(int i = 1; i < 100; i += 2) {
17+
summation += i/(i + 2.0);
18+
}
19+
System.out.printf("The summation is %f", summation);
20+
}
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @author : Jonathan Birkey
3+
* @mailto : jonathan.birkey@gmail.com
4+
* @created : 27Dec2021
5+
*
6+
* Exercise 5.25
7+
* (Compute Pi) You can approximate p by using the following summation:
8+
*
9+
* Pi - 4(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + -^i+1/2i-1)
10+
* Write a program that displays the Pi value for i = 10000,20000,...,and100000.
11+
**/
12+
package com.github.jonathanbirkey.chapter05;
13+
14+
public class Exercise25 {
15+
public static void main(String[] args) {
16+
double sum1 = 0;
17+
for(int i = 2; i <= 10000; i++){
18+
sum1 += (Math.pow(-1, i + 1)) / (2.0 * i - 1.0);
19+
}
20+
double Pi1 = 4 * (1.0 - sum1);
21+
System.out.printf("Pi value when i = 10000 is %f\n", Pi1);
22+
23+
double sum2 = 0;
24+
for(int j = 2; j <= 20000; j++){
25+
sum2 += (Math.pow(-1, j + 1)) / (2.0 * j - 1.0);
26+
}
27+
double Pi2 = 4 * (1.0 - sum2);
28+
System.out.printf("Pi value when i = 20000 is %f\n", Pi2);
29+
30+
double sum3 = 0;
31+
for(int k = 2; k <= 100000; k++){
32+
sum3 += (Math.pow(-1, k + 1)) / (2.0 * k - 1.0);
33+
}
34+
double Pi3 = 4 * (1.0 - sum3);
35+
System.out.printf("Pi value when i = 100000 is %f\n", Pi3);
36+
}
37+
}

0 commit comments

Comments
 (0)