Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package carloans;
- import java.util.*;
- public class CarLoans {
- public static void main(String[] args) {
- // Scanner object for user inputs
- Scanner inputs;
- inputs = new Scanner(System.in);
- // Asks user for amount of loan
- System.out.println("Please input Loan Amount in Dollars (ex: 4500.00):");
- double LoanAmount = inputs.nextDouble();
- // Asks user for amount of interest rate
- System.out.println("Please input Interest Rate in Decimals (ex: 7.5% = 0.075):");
- double Rate = inputs.nextDouble();
- // Asks user for number of months
- System.out.println("Please input Loan Duration in Months:");
- int Months = (int)inputs.nextDouble();
- // Closes the scanner object
- inputs.close();
- // Stores calculations made
- double MonthlyInt = MonthlyInterest(Rate);
- double MonthlyPayment = MonthlyPay(LoanAmount, MonthlyInt, Months);
- // Prints loan information
- PrintInfo(LoanAmount, Rate, Months, MonthlyPayment);
- }
- // Calculates the Monthly interest given the anual interest
- public static double MonthlyInterest(double AnnualInterest) {
- return AnnualInterest / 12;
- }
- // Calculates the Monthly Payment given amount, monthly interest, and number of months
- public static double MonthlyPay(double Amount, double MonthlyInterest, int Months) {
- return (MonthlyInterest * Amount) / (1 - (Math.pow(1 + MonthlyInterest, Months * -1)));
- }
- // Displays the loan information including the calculated values
- public static void PrintInfo(double Borrowed, double Annual, int Months, double MonthlyPay) {
- System.out.printf("Amount Borrowed Annual Interest Rate Length of Loan Monthly Payment\n" +
- "%15.2f%23.3f%17d%17.2f", Borrowed, Annual, Months, MonthlyPay);
- }
- }
Add Comment
Please, Sign In to add comment