CS 171 - Fall 1999
Lab 8

Handed out:  Mar 21

Due Date:   April 10


1. Write a Java program that will read a decimal number from the command line, and computes its equivalent binary representation. Follow the method described in the class on Tuesday. That is: Suppose the decimal number is D, then first find K, the highest power of 2 that is smaller than D. The first bit of the binary number is obtained by computing the quotient when D is divided by K. Next set D to D mod K, and set K to K/2, and repeat this process to find the next bit. Continue this iteration until all the bits have been calculated. The equivalent binary number should be output on the screen.


2. Write a Java program using a while loop to compute the gcd of two given integers. Your program should ask the user to supply two integers.

Hint: The following ML function (discussed in class 14) computes the gcd (greatest common divisor) of two given integers a and b.

fun gcd(a,b) =
    if b=0 then a
    else gcd(b,a mod b);
Use the same logic as given in the ML program. Instead of using recursion, you rewrite it as a while loop.


3. You are interested in buying a new car, and need to take out a loan for the car.  When you take out a loan for a car, you not only pay what the car costs (the principal), but you also pay interest on the amount you owe (which is how banks make money). You would like to calculate how much interest you will pay over the whole life of the loan, and furthermore, how much you can save if you pay extra on your monthly payment (thus paying the loan off early). You decide to write a Java program to help you compute this.

The formula to calculate the monthly payment is

Interest Rate Equation

where R is the monthly interest rate, A is the amount of the loan, and N is the number of months that the loan is for. P is the resulting monthly (or, more accurately, the current month's) payment.  This formula can be used once, with the total amount of the loan, to figure out the monthly payment, or it can be applied at each month over the life of the loan, by using the outstanding loan amount for A and the number of remaining months for N (the interest rate R does not change). For this assignment, you just need to use this formula once, at the beginning of the loan, to figure out the monthly payment.

When this monthly payment is sent in, part of the payment goes to paying the current interest due, and whatever is left over goes to paying off part of the balance of the loan (how much you still owe). These portions change every month, and need to be calculated for each month.

The amount of a monthly payment P that pays interest is simply the monthly interest rate times the amount of the loan (or current outstanding amount), and the amount of P that is paid to the principal (i.e., the current amount) is whatever is left after paying the interest. The amount paid to the principal is subtracted from the current amount of the loan, resulting in a new balance A for next month's application of the above formula.

You are to write a program that computes and prints out each monthly payment on the auto loan, the portion that is paid as interest, and the portion that actually goes to the principal. At the end, the program should print out the total interest paid and the total principal paid.

If you have extra money, you are allowed to send in an extra amount over and above what you actually owe for that month (i.e., the monthly payment P that is due). All of this extra amount goes directly to paying off some of the principal (since the interest for that month was already covered). In your program, you will need to allow for adding an extra amount to each monthly payment that is the amount you want to pay extra on the loan.

The inputs to your program should be command-line arguments that are: the amount of the loan, the yearly (note this) interest rate as a percent (you need to convert it to a monthly rate), and the number of years that the loan is taken out for. Additionally, you should allow another input, an amount per month that the person wants to pay extra on the loan. This extra amount should go directly to the principal.

Your assignment should do the following:

  1. use double's for all relevant variables,
  2. use the math library function Math.pow(double val, double exp) for exponentiation,
  3. have a function double monthly_payment(double A, double N, double R) written by you that takes A, N, and R, as above, and returns the monthly payment, using the formula given above.
  4. you can write other functions as well, if it helps you.
A sample of how you can print two-decimal-digit real values is shown below in a sample program:
import java.text.*;

class lab8 
{

 public static void main (String Args[]) 
 {
   double V = 1234.5673;
   DecimalFormat twodec = new DecimalFormat();
   twodec.setMaximumFractionDigits(2);
   twodec.setMinimumFractionDigits(2);
   System.out.println("Formatted printing for V is: " +
       twodec.format(V));
 }

}
This program prints out the double value V with only two decimal places, and commas to separate thousands, millions, etc. Note that it does not round the number; in this example, it prints "1,234.56". You can print any double value in your program with the "twodec.format()" function once you include the statement beginning with "DecimalFormat" and the two statements beginning with "twodec". Note that you also have to have the line "import java.text.*;" at the top of your program.

A sample run of your program might look like

% java loancalc 14699.35 8.5 5 25
----------------------------------------------------
Your loan is for $14,699.35, for 60 months and a
yearly interest rate of 8.50 percent.
Your base monthly payment is $301.58 and you have 
chosen to pay $25.00 extra each month.
----------------------------------------------------
Month  Payment  Interest  Principal  Extra   Balance
----------------------------------------------------
1      326.58   104.12    197.46     25.00   14,476.89
2      326.58   102.54    199.04     25.00   14,252.86
3      326.58   100.96    200.62     25.00   14,027.23
4      326.58   99.36    202.22     25.00   13,800.01
...
55      128.82   0.91    127.91     0.00   0.00
----------------------------------------------------
Total interest : 3,064.77
Total principal: 14,699.35
In this example the loan was for $14,699.35, the yearly interest rate was 8.5%, the number of years the loan was for was 5, and the person was paying $25 extra a month. Because they were paying extra, they actually finished paying the loan in 55 months, rather than the full 60 months that the loan was for. Not only that, but they saved money by not paying as much interest. Try running this example with no extra amount and see how much more interest is paid.