Monday, 13 July 2015

Java Programs[Important]

  1. 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.
  2.  
  3. Program :
  4.  
  5. import   .awt.*; import   .awt.event.*; import   .applet.*;
  6.  
  7. /* <applet code = "LoanPayment" width=500 height=300 > <param name = monthlyRate value=true> </applet> */
  8.  
  9. public class LoanPayment extends Applet implements ActionListener { TextField amt_t, rate_t, period_t; Button compute = new Button("Compute"); boolean monthlyRate; 
  10. 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);
  11.  
  12. amt_t = new TextField(10); rate_t = new TextField(10); period_t = new TextField(10);
  13.  
  14. add(amt_l); add(amt_t); add(rate_l); add(rate_t); add(period_l); add(period_t); add(compute);
  15.  
  16. amt_t.setText("0"); rate_t.setText("0"); period_t.setText("0"); monthlyRate = Boolean.valueOf(getParameter("monthlyRate"));
  17.  
  18. amt_t.addActionListener(this); rate_t.addActionListener(this); period_t.addActionListener(this); compute.addActionListener(this); }
  19.  
  20. public void paint(Graphics g) { double amt=0, rate=0, period=0, payment=0; String amt_s, rate_s, period_s, payment_s;
  21.  
  22. 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();
  23.  
  24. period = Double.parseDouble(period_s); } catch (Exception e) { }
  25.  
  26. if (monthlyRate) payment = amt * period * rate * 12 / 100; else payment = amt * period * rate / 100;
  27.  
  28. payment_s = String.valueOf(payment);
  29.  
  30. g.drawString("The LOAN PAYMENT amount is: ", 50, 150); g.drawString(payment_s, 250, 150); }
  31.  
  32. public void actionPerformed(ActionEvent ae) { repaint(); }
  33.