Reputation: 21
This is what I am working with. It is a monthly payment loan calculator. I keep getting an error "The method monthlyPayment (double, int, int) is undefined for the type Assignment 8." This error shows up in the main method. The error is on line 27.
CLASS
public class LoanCalc {
public static double monthlyPayment(double amountBorrowed, int loanLength, int intRate) {
double principal;
double interestRate;
double monthlyPayment;
principal = amountBorrowed;
interestRate = intRate / 100 / 12;
monthlyPayment = (interestRate * principal) /
(1- Math.pow((1 + interestRate) , - loanLength * 12 ));
return monthlyPayment;
}
}
MAIN METHOD
1 import java.util.Scanner;
2
3 public class Assignment8 {
4
5 public static void main(String[] args) {
6
7 Scanner kbd = new Scanner(System.in);
8
9 System.out.println("Enter the amount borrowed: ");
10 double amountBorrowed = kbd.nextDouble();
11
12 System.out.println("Enter the interest rate: ");
13 int intRate = kbd.nextInt();
14
15 System.out.println("Enter the minimum length of loan: ");
16 int minLength = kbd.nextInt();
17
18 System.out.println("Enter the maximum length of loan: ");
19 int loanLength = kbd.nextInt();
20 while (loanLength < minLength) {
21 System.out.println("Invalid input: Input must be greater than 22 minimum length of loan");
23 System.out.println("Enter the maximum length of loan: ");
24 loanLength = kbd.nextInt();
25 }
26
27 double payment = monthlyPayment(amountBorrowed, loanLength, intRate);
28 System.out.println(payment);
29
30 }
}
Upvotes: 0
Views: 78
Reputation: 25096
You have to call the function using
LoanCalc.monthlyPayment( ... )
since it is a static method belonging to another class.
Upvotes: 1
Reputation: 3896
Change it to
double payment = LoanCalc.monthlyPayment(amountBorrowed, loanLength, intRate);
This is because monthlyPayment()
belongs to LoanCalc
, not Assignment8
, so you need to explicitly state where to find monthlyPayment()
.
Upvotes: 3