In: Computer Science
For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble()
----- .
If an amount of A dollars is borrowed at an interest rate (expressed as a decimal) r for y years, and n is the number of annual payments, then the amount of each individual payment is given by :
payment = ( r * A/n ) / ( 1 - (1 + (r/n) )-ny )
Write a java ” Payment program” (application or applet) that uses this formula to find the payments corresponding to different sets of input data A, R, y, n. Test thoroughly the program with different data sets of your own.
To solve this question first define a Scanner that will take the input of all the variables and After that define all the variable which will store the variables. The Input will be taken in the console. The given formula gives payment information in the year.
The necessary comments that will help you understand the solution.
import java.util.*;
class interest{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Taking input
System.out.print("Enter the Amount: ");
double amount = scan.nextDouble();
System.out.print("Enter the Interest Rate(%): ");
double rate = scan.nextDouble();
System.out.print("Enter the years: ");
int y = scan.nextInt();
System.out.print("Enter the number of annual payments: ");
int n = scan.nextInt();
// Payment formula
double payment = (rate * (amount/n)) / (1 - (1/ (Math.pow((1 + (rate / n)), n * y))));
payment = Math.round(payment * 100.0) / 100.0;
System.out.println("The payment is(yearly): " + payment);
}
}
Hoping you have understood the solution