Having trouble with my program, can someone please help?
//Imports the scanner utility
import java.util.Scanner;
public class ComputeLoan1
{
//Calls the method for variables to be calculated
public static double calculations(double loanAmount, double monthlyInterestRate, int numOfYears)
{
//Declares calculations as monthlyPayment
double monthlyPayment = (loanAmount * monthlyInterestRate / (1
- 1 / Math.pow(1 + monthlyInterestRate, numOfYears * 12)));
//Value that is returned to the main method
return monthlyPayment;
}//Closes the calculations method
//Final method used to output users calculations
public static void finalMethod(String[] args)
{
//Displays results
System.out.println("The monthly payment is " + (int)(monthlyPayment * 100) /100.0);
}//Closes the finalMethod
//Main method
public static void main(String[] args) {
//Declarations
double loanAmount = 0;
double annualInterestRate = 0;
double monthlyInterestRate = 0;
int numOfYears = 0;
double calculations;
//Creates a scanner for input from keyboard
Scanner input = new Scanner(System.in);
//Greeting to the user
System.out.println("Welcome to Kentucky Credit Union Loan Calculator!");
System.out.println("This program is used to help calculate the monthly loan amount.");
System.out.println("");
//Prompts the user for input
System.out.print("Please enter the loan amount: $");
loanAmount = input.nextDouble(); //Accepts input from user and stores it in a variable
//Enter yearly interest rate
System.out.print("Enter yearly interest rate, for example 7.25: ");
annualInterestRate = input.nextDouble();
//Obtain monthly interest rate
monthlyInterestRate = annualInterestRate / 1200;
//Prompts the user for input
System.out.print("Please enter length of the loan in years as an integer, for example 5: ");
numOfYears = input.nextInt();
} //Closes the main method
}//Closes the class
Re: Having trouble with my program, can someone please help?
Please correctly format your code by using highlight tags:
[highlight=Java]//your code goes here[/highlight]
What exactly is your program? Does your code compile, or are there errors (if so, post them)?
Quote:
monthlyInterestRate = annualInterestRate / 1200;
I haven't dealt with loan formulas very much, but this strikes me as odd. Wouldn't you want to divide the annual rate by just 12 to get the monthly rate?
Re: Having trouble with my program, can someone please help?
This is the error I get:
Documents\JCreator LE\MyProjects\ComputeLoan1\src\ComputeLoan1.java:2 5: cannot find symbol
symbol : variable monthlyPayment
location: class ComputeLoan1
System.out.println("The monthly payment is " + (int)(monthlyPayment * 100) /100.0);
^
1 error
the monthlyInterestRate = annualInterestRate / 1200; is correct. I used to know why you do it this way, but I forgot. It's how it is in my java programming book too.
this is what the program is suppose to do:
Your application will calculate the monthly payments due on a loan. The user will input the loan amount, the interest rate, and the loan's term (time to repay).
You will accept input in the main method. You will use a separate method (other than main) to calculate the payment. You will use a third method to output the result. The third method (output) can be called from either the main method or the calculation method. Here is the formula you will use to calculate the payment:
Payment Formula
Re: Having trouble with my program, can someone please help?
Please format your code! :O
monthlyPayment is declared in your calculations() method in your ComputeLoan1 class; you can't use it anywhere else in that class. The scope of a variable is when and where you can use it; scope is limited to the block of code in which you define a variable. This means that since you defined monthlyPayment in your calculations() method, you can ONLY use it within that method.
However, the calculations() method returns monthlyPayment's value, so why not just call calculations()?
Also, note that your method:
public static void finalMethod(String[] args)
Does not have to have args as an argument; only the main method must have a String[] as an argument. Your final method doesn't require any arguments, so you need not list any in its declaration.
Re: Having trouble with my program, can someone please help?
Ty very much for your help. I got it figured out. Ty