Starting from:

$25

COSC117 - Homework05 -Solved

   Programming Exercises
1.    Write a program that will take as input a length (such as 23.4 in) and a conversion unit (such as ft) and output the converted length. Units are to be input by their abbreviations without a . at the end, for example, 23.4 in or 17.98 mi or 56.9 km . The units the program is to convert from and to are in, ft, yd, mi, mm, cm, m, km. A run of the program is below. Have the program print out brief instructions, as below. Also have the program print out an error if either the input units or the conversion units are invalid. The final output should be a simple conversion equation with 5 decimal place accuracy for both the input and output. In the run below, the user typed in 26.2 mi after the program asked for the length and the km after the program asked for new units. This was the only input from the user.

Input a length as <length> <units>, for example, 23.4 ft The <units> can be: in, ft, yd, mi, mm, cm, m, or km.

Do not use a . at the end of the units, so 23.4 feet is 23.4 ft

Length: 26.2 mi

Input the units to be converted to: in, ft, yd, mi, mm, cm, m, or km.

New Units: km

26.20000 mi = 42.16481 km

Hints:

(a)    Your first input line contains two inputs, the first is a double and the second is a string.

(b)    You may want to trim the input strings in case the user types in some extra spaces.

(c)     It may make the program logic easier if you take the input length and convert it to one unit (inches for example) and then convert that to the desired output unit.

2.    This exercise is to implement a method to find a root, or a zero, of a function. In your mathematics classes you have, for many different reasons, needed to solve f(x) = 0 for x. For example, to solve x2 − 3x + 1 = 0 you would have used the quadratic formula. Not all functions are as easy to solve as a quadratic, in fact many are so difficult that they cannot be solved exactly but need to be approximated. For example, what about solving sin(x) + cos(x) = 0?

1

One method to finding a solution to f(x) = 0 is as follows. In this method you start with a function to find the root of, f(x), a lower bound a and upper bound b values. We choose a and b so that we know there is a solution between them, that is we choose a and b so that f(a) and f(b) have different signs, i.e. one is positive and one is negative. First, make sure that f(a) and f(b) have different signs. If not then print out that there is not a root between these two values and end the program. If they do differ in sign then find the midpoint between a and b, call it m, that is  . Evaluate the function at the midpoint, f(m). If f(a) and f(m) have different signs then reset the upper bound b to m. If f(m) and f(b) have different signs then reset the lower bound a to m. Then continue the process with finding the midpoint of the new values of a and b. Keep going until the difference between a and b is less than the input tolerance. Once that happens return the midpoint value. Three runs of the program are below.

Input Initial Lower Bound: 1

Input Initial Upper Bound: 4

Input Tolerance on Approximation: 0.0000001

Approximation to a root at: 2.3561945259571075

Input Initial Lower Bound: 1

Input Initial Upper Bound: 2

Input Tolerance on Approximation: 0.0001 There is no root between 1.0 and 2.0.

Input Initial Lower Bound: 7

Input Initial Upper Bound: 9

Input Tolerance on Approximation: 0.000001

Approximation to a root at: 8.63938045501709

For this one we are going to introduce a simple “method” so that you can evaluate f(x) more easily throughout the program. Below is a start to the program, notice on line 21 we have the code double lbval = f(a). This of course declares the variable lbval and initializes it to the value of the right hand side of the expression. The right hand side of the expression is f(a). What happens here is that the value of a is sent to the value of x in the code on line 31. Then val is given the value of sin(x)+cos(x), that is sin(a)+cos(a). This f(a) value is then returned to line 21 and stored in lbval. The same thing happens but with the value of b on line 22.

1 import java.util.Scanner;

2

3 public class HW05_Prob1 {

4

5                                  public static void main(String[] args) {

6                                  Scanner keyboard = new Scanner(System.in);

7

8                                  System.out.print("Input Initial Lower Bound: ");

9                                  double a = keyboard.nextDouble();

10

11                                  System.out.print("Input Initial Upper Bound: ");

12                                  double b = keyboard.nextDouble();

13

14                                  System.out.print("Input Tolerance on Approximation: ");

15                                  double tolerance = keyboard.nextDouble();

16

17                                  // This next block is just to show how you will get

2

18                                  // function values using the f method below. It can be removed 19         // when you submit the program.

20

21                                  double lbval = f(a);

22                                  double ubval = f(b);

23

24
 
System.out.println("f(a) = " + lbval);
25

26
 
System.out.println("f(b) = " + ubval);
27

28
 
// Complete the program.
29

30
}
 
31                                  public static double f(double x) {

32                                  double val = Math.sin(x) + Math.cos(x);

33                                  return val;

34                                  }

35                                  }

3. In this exercise you will write a program that will construct a loan payment chart. Unless you are independently wealthy you will be taking out a loan for something in your lifetime. It may be to buy a car or a house. You may already have one for college. When you take out a loan the bank gives you the money in one lump sum and you pay it back with monthly payments. The bank, of course, charges you interest on the loan each month as well.

The way the bank determines you monthly payment is a follows. They take the loan amount A, the annual interest rate R in decimal form (i.e. 5% = 0.05), and the number of years the loan is to be paid back Y . They first calculate the term T by

 

Using this they calculate your monthly payment (call it P) by

 

If we let B be the balance of the loan (the amount you still owe the bank), then the amount you owe, that is the new balance, after a monthly payment is .

That is, the bank adds the interest on the loan, which is the balance times the interest rate divided by 12,  . You divide by 12 since it is interest on one month instead of on an entire year. The bank then subtracts the payment you have made.

For example, say that your balance (B) at the beginning of the current month is $100,000, you are paying $1,250 a month (P) and your annual interest rate is 4.5%

(R = 0.045). Then your new balance for the next month is

 

and the interest for the month was

 

3

An example run of the program is below. Your output should look the same with the decimal points lining up, a column for months, balance, monthly interest and total interest for each month.

Input the amount of the loan: 25000

Input the number of years of the loan: 4

Input the interest rate (5% = 0.05): 0.045

Your monthly payment is 570.09

              Month                     Balance                      Interest                    Total Int.

0                                            25000.00              0.00         0.00

1                                            24523.66              93.75       93.75

2                                            24045.54              91.96       185.71

3                                            23565.62              90.17       275.88

4                                            23083.91              88.37       364.26

5                                            22600.38              86.56       450.82

6                                            22115.05              84.75       535.57

7                                            21627.89              82.93       618.50

8                                            21138.91              81.10       699.61

9                                            20648.09              79.27       778.88

10                                         20155.44              77.43       856.31

11                                         19660.93              75.58       931.89

12                                         19164.57              73.73       1005.62

13                                         18666.35              71.87       1077.49

14                                         18166.27              70.00       1147.49

15                                         17664.30              68.12       1215.61

16                                         17160.46              66.24       1281.85

17                                         16654.72              64.35       1346.20

18                                         16147.09              62.46       1408.66

19                                         15637.55              60.55       1469.21

20                                         15126.11              58.64       1527.85

21                                         14612.74              56.72       1584.57

22                                         14097.45              54.80       1639.37

23                                         13580.23              52.87       1692.24

24                                         13061.07              50.93       1743.16

25                                         12539.96              48.98       1792.14

26                                         12016.90              47.02       1839.17

27                                         11491.88              45.06       1884.23

28                                         10964.88              43.09       1927.32

29                                         10435.92              41.12       1968.44

30                                         9904.96                39.13       2007.58

31                                         9372.02                37.14       2044.72

32                                         8837.08                35.15       2079.87

33                                         8300.13                33.14       2113.00

34                                         7761.17                31.13       2144.13

35                                         7220.18                29.10       2173.23

36                                         6677.17                27.08       2200.31

37                                         6132.13                25.04       2225.35

38                                         5585.03                23.00       2248.35

39                                         5035.89                20.94       2269.29

40                                         4484.69                18.88       2288.17

41                                         3931.42                16.82       2304.99

42                                         3376.07                14.74       2319.73

43                                         2818.65                12.66       2332.39

44                                         2259.13                10.57       2342.96

45                                         1697.51                8.47         2351.44

46                                         1133.79                6.37         2357.80

47                                         567.96  4.25         2362.05

48                                         0.00      2.13         2364.18

Your loan amount was 25000.00.

Your loan duration was 48.00 months.

4

Your loan monthly payment was 570.09.

Your total interest was 2364.18. Your total cost is 27364.18.

Once your program is complete, use it to answer the following questions. Include the answers with your program runs.

(a)     Say you are buying a new car and you take out a loan for $20,000. The best interest rate you can get is 4.7% and you decide to take the loan out for 5 years. What is your monthly payment, the total interest on the loan, and how much did the car actually cost you?

(b)    Say you are buying the same car above but do a 4 year loan. What is your monthly payment, the total interest on the loan, and how much did the car actually cost you? What was the difference in the monthly payment? How much did you save in interest by going with a 4 year loan?

(c)     Let’s consider buying a house, say you take out a loan for $200,000. The bank is giving you an interest rate of 4% and you decide to go with the standard 30 year mortgage. What is your monthly payment, the total interest on the loan, and how much did the house actually cost you?

(d)    Say you took out the same house loan but went with a 20 year mortgage. What is your monthly payment, the total interest on the loan, and how much did the house actually cost you? What was the difference in the monthly payment? How much did you save in interest by going with a 20 year loan instead of a 30 year loan?

5

More products