Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// I WANT TO USE DIALOG BOX // I NEED HELP PLEASE 1 import java.text.DecimalForm

ID: 3718116 • Letter: #

Question

// I WANT TO USE DIALOG BOX

// I NEED HELP PLEASE

1 import java.text.DecimalFormat;
2 import java.util.Scanner;
3 public class BuyingCar
4 {
5 public static void main(String[] args)
6 {
7 //Scanner class object
8 Scanner scanner = new Scanner(System.in);
9 //prompt loan amount
10 System.out.print("Loan Amount: ");
11 double loanAmount = scanner.nextDouble();
12 //prompt number of years
13 System.out.print("Number of Years: ");
14 int numYears = scanner.nextInt();
15 //prompt annual interest rate
16 System.out.print("Annual Interest Rate (in %): ");
17 double annualInterestRate = scanner.nextDouble();
18 System.out.println();
19 //call method printTable
20 printTable(loanAmount, annualInterestRate, numYears);
21 }
22 /**The method printTable that takes principal, annual interest rate and years
23 * and generates a table of monthly payment on console.*/
24 public static void printTable(double principal,
25 double annualInterestRate,
26 int numYears)
27 {
28
29 double amount=principal;
30 double interestPaid, principalPaid, newBalance;
31 double monthlyInterestRate, monthlyPayment;
32 int month;
33 int numMonths = numYears * 12;
34 double totalInterest=0;
35
36 // calculate monthly rate
37 monthlyInterestRate = annualInterestRate / 12;
38 monthlyPayment = monthlyPayment(principal, monthlyInterestRate, numYears);
39
40 // Print the table header
41 System.out.printf("%10s%10s%10s%10s%15s ","","","", "Unpaid","Total Interest");
42 System.out.printf("%10s%10s%10s%10s%15s ","Payment","Principal","Interest", "Balance","to Date");
43
44 //generate a tabel of values for payment, principal, interest ,unpaid balance
45 //interest to date
46 for (month = 1; month <= numMonths; month++)
47 {
48 // calculate interest paid
49 interestPaid = principal * (monthlyInterestRate / 100);
50 //calculate the principal paid
51 principalPaid = monthlyPayment - interestPaid;
52 //calculate the new balance
53 newBalance = principal - principalPaid;
54
55 totalInterest+=interestPaid;
56 if(newBalance<=0)
57 System.out.format("%8d%10.2f%10.2f%12.2f%12.2f ",
58 month,principalPaid, interestPaid, 0.00,totalInterest);
59 else
60 System.out.format("%8d%10.2f%10.2f%12.2f%12.2f ",
61 month,principalPaid, interestPaid, newBalance,totalInterest);
62
63 // set newbalance to principal
64 principal = newBalance;
65
66 if(month%12==0 && month!=numMonths)
67 System.out.println(" Continuing on Next Page ");
68
69 }
70 DecimalFormat df=new DecimalFormat("#,###.00");
71 //print total amount paid to the loan with interest
72 System.out.printf("Total amount paid = "+df.format(amount+totalInterest));
73 }
74
75 /**The method monthlyPayment that returns the monthly payment of the loan amount,
76 * monthly interest rate and number of years*/
77 static double monthlyPayment(double loanAmount, double monthlyInterestRate, int numberOfYears) {
78 //convert interest rate to decimal value
79 monthlyInterestRate =monthlyInterestRate/ 100.0;
80 return loanAmount * monthlyInterestRate /
81 ( 1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12) );
82 }
83 }//end of class

Explanation / Answer

I have added the JoptionPane dialog box for taking input from user. If you want output alsoto be in a message box then please comment, I'll do that for you ... Now in this program we are not using any Scanner class.

public class BuyingCar {

public static void main(String[] args) {

// prompt loan amount

double loanAmount = Double.parseDouble(JOptionPane.showInputDialog("Loan Amount: "));

// prompt number of years

int numYears = Integer.parseInt(JOptionPane.showInputDialog("Number of Years: "));

// prompt annual interest rate

double annualInterestRate = Double.parseDouble(JOptionPane.showInputDialog("Annual Interest Rate (in %): "));

// call method printTable

printTable(loanAmount, annualInterestRate, numYears);

}

/**

* The method printTable that takes principal, annual interest rate and

* years and generates a table of monthly payment on console.

*/

public static void printTable(double principal, double annualInterestRate, int numYears) {

double amount = principal;

double interestPaid, principalPaid, newBalance;

double monthlyInterestRate, monthlyPayment;

int month;

int numMonths = numYears * 12;

double totalInterest = 0;

// calculate monthly rate

monthlyInterestRate = annualInterestRate / 12;

monthlyPayment = monthlyPayment(principal, monthlyInterestRate, numYears);

// Print the table header

System.out.printf("%10s%10s%10s%10s%15s ", "", "", "", "Unpaid", "Total Interest");

System.out.printf("%10s%10s%10s%10s%15s ", "Payment", "Principal", "Interest", "Balance", "to Date");

// generate a tabel of values for payment, principal, interest ,unpaid

// balance

// interest to date

for (month = 1; month <= numMonths; month++) {

// calculate interest paid

interestPaid = principal * (monthlyInterestRate / 100);

// calculate the principal paid

principalPaid = monthlyPayment - interestPaid;

// calculate the new balance

newBalance = principal - principalPaid;

totalInterest += interestPaid;

if (newBalance <= 0)

System.out.format("%8d%10.2f%10.2f%12.2f%12.2f ", month, principalPaid, interestPaid, 0.00,

totalInterest);

else

System.out.format("%8d%10.2f%10.2f%12.2f%12.2f ", month, principalPaid, interestPaid, newBalance,

totalInterest);

// set newbalance to principal

principal = newBalance;

if (month % 12 == 0 && month != numMonths)

System.out.println(" Continuing on Next Page ");

}

DecimalFormat df = new DecimalFormat("#,###.00");

// print total amount paid to the loan with interest

System.out.printf("Total amount paid = " + df.format(amount + totalInterest));

}

/**

* The method monthlyPayment that returns the monthly payment of the loan

* amount, monthly interest rate and number of years

*/

static double monthlyPayment(double loanAmount, double monthlyInterestRate, int numberOfYears) {

// convert interest rate to decimal value

monthlyInterestRate = monthlyInterestRate / 100.0;

return loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));

}

}// end of class

UPDATED CODE WITH OUTPUT IN DIALOG BOX

public class BuyingCar {

public static void main(String[] args) {

// prompt loan amount

double loanAmount = Double.parseDouble(JOptionPane.showInputDialog("Loan Amount: "));

// prompt number of years

int numYears = Integer.parseInt(JOptionPane.showInputDialog("Number of Years: "));

// prompt annual interest rate

double annualInterestRate = Double.parseDouble(JOptionPane.showInputDialog("Annual Interest Rate (in %): "));

// call method printTable

printTable(loanAmount, annualInterestRate, numYears);

}

/**

* The method printTable that takes principal, annual interest rate and

* years and generates a table of monthly payment on console.

*/

public static void printTable(double principal, double annualInterestRate, int numYears) {

double amount = principal;

double interestPaid, principalPaid, newBalance;

double monthlyInterestRate, monthlyPayment;

int month;

int numMonths = numYears * 12;

double totalInterest = 0;

// calculate monthly rate

monthlyInterestRate = annualInterestRate / 12;

monthlyPayment = monthlyPayment(principal, monthlyInterestRate, numYears);

String output = "";

// Print the table header

output = output + String.format("%10s%10s%10s%10s%15s ", "", "", "", "Unpaid", "Total Interest");

output = output + String.format("%10s%10s%10s%10s%15s ", "Payment", "Principal", "Interest", "Balance", "to Date");

// generate a tabel of values for payment, principal, interest ,unpaid

// balance

// interest to date

for (month = 1; month <= numMonths; month++) {

// calculate interest paid

interestPaid = principal * (monthlyInterestRate / 100);

// calculate the principal paid

principalPaid = monthlyPayment - interestPaid;

// calculate the new balance

newBalance = principal - principalPaid;

totalInterest += interestPaid;

if (newBalance <= 0)

output = output + String.format("%8d%10.2f%10.2f%12.2f%12.2f ", month, principalPaid, interestPaid, 0.00,

totalInterest);

else

output = output + String.format("%8d%10.2f%10.2f%12.2f%12.2f ", month, principalPaid, interestPaid, newBalance,

totalInterest);

// set newbalance to principal

principal = newBalance;

if (month % 12 == 0 && month != numMonths)

output = output + String.format(" Continuing on Next Page ");

}

DecimalFormat df = new DecimalFormat("#,###.00");

// print total amount paid to the loan with interest

output = output + String.format("Total amount paid = " + df.format(amount + totalInterest));

JTextArea textArea = new JTextArea(output);

JScrollPane scrollPane = new JScrollPane(textArea);  

textArea.setLineWrap(true);  

textArea.setWrapStyleWord(true);

scrollPane.setPreferredSize( new Dimension( 500, 500 ) );

JOptionPane.showMessageDialog(null, scrollPane, "Unpaid Amount",  

JOptionPane.INFORMATION_MESSAGE);

}

/**

* The method monthlyPayment that returns the monthly payment of the loan

* amount, monthly interest rate and number of years

*/

static double monthlyPayment(double loanAmount, double monthlyInterestRate, int numberOfYears) {

// convert interest rate to decimal value

monthlyInterestRate = monthlyInterestRate / 100.0;

return loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));

}

}// end of class