Java Programs[Important]
- Write an Applet
that computes the payment of a loan based on the amount of the loan, the
interest rate and the number of months. It takes one parameter from the
browser: Monthly rate; if true, the interest rate is per month, otherwise the
interest rate is annual.
-
- Program :
-
- import .awt.*; import .awt.event.*; import .applet.*;
-
- /* <applet
code = "LoanPayment" width=500 height=300 > <param name =
monthlyRate value=true> </applet> */
-
- public class
LoanPayment extends Applet implements ActionListener { TextField amt_t, rate_t,
period_t; Button compute = new Button("Compute"); boolean
monthlyRate;
- public void
init() { Label amt_l = new Label("Amount: "); Label rate_l = new
Label("Rate: ", Label.CENTER); Label period_l = new
Label("Period: ", Label.RIGHT);
-
- amt_t = new
TextField(10); rate_t = new TextField(10); period_t = new TextField(10);
-
- add(amt_l);
add(amt_t); add(rate_l); add(rate_t); add(period_l); add(period_t);
add(compute);
-
- amt_t.setText("0");
rate_t.setText("0"); period_t.setText("0"); monthlyRate =
Boolean.valueOf(getParameter("monthlyRate"));
-
- amt_t.addActionListener(this);
rate_t.addActionListener(this); period_t.addActionListener(this);
compute.addActionListener(this); }
-
- public void
paint(Graphics g) { double amt=0, rate=0, period=0, payment=0; String amt_s,
rate_s, period_s, payment_s;
-
- g.drawString("Input
the Loan Amt, Rate and Period in each box and press Compute", 50,100); try
{ amt_s = amt_t.getText(); amt = Double.parseDouble(amt_s); rate_s =
rate_t.getText(); rate = Double.parseDouble(rate_s); period_s =
period_t.getText();
-
- period =
Double.parseDouble(period_s); } catch (Exception e) { }
-
- if (monthlyRate)
payment = amt * period * rate * 12 / 100; else payment = amt * period * rate /
100;
-
- payment_s =
String.valueOf(payment);
-
- g.drawString("The
LOAN PAYMENT amount is: ", 50, 150); g.drawString(payment_s, 250, 150); }
-
- public void
actionPerformed(ActionEvent ae) { repaint(); }
-