Results 1 to 3 of 3
  1. #1
    msaggs is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default java loan calculator applet help

    Hi, I'm new to using java and I need some serious help with fixing the code. I have to write a Java applet program (together with its html test file) to calculate loan payments. The user will provide the interest rate, the number of years, and loan amount.

    here's what I have so far

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.Label;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.TitledBorder;
     
    public class LoanCalculator extends JApplet
    implements ActionListener
    {
    private JTextField jtfInterestRate = new JTextField(10);
    private JTextField jtfYear = new JTextField(10);
    private JTextField jtfLoan = new JTextField(10);
    private JTextField jtfMonthlyPay = new JTextField(10);
    private JTextField jtfTotalPay = new JTextField(10);
     
    private JButton jbtCompute = new JButton("Compute Payment");
     
    public LoanCalculator()
    {
    this.jtfMonthlyPay.setEditable(false);
    this.jtfTotalPay.setEditable(false);
     
    this.jtfInterestRate.setHorizontalAlignment(4);
    this.jtfYear.setHorizontalAlignment(4);
    this.jtfLoan.setHorizontalAlignment(4);
    this.jtfMonthlyPay.setHorizontalAlignment(4);
    this.jtfTotalPay.setHorizontalAlignment(4);
     
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(5, 2));
    p1.add(new Label("Interest Rate (e.g., 5.5 for 5.5%)"));
    p1.add(this.jtfInterestRate);
    p1.add(new Label("Years "));
    p1.add(this.jtfYear);
    p1.add(new Label("Loan Amount"));
    p1.add(this.jtfLoan);
    p1.add(new Label("Monthly Payment"));
    p1.add(this.jtfMonthlyPay);
    p1.add(new Label("Total Payment"));
    p1.add(this.jtfTotalPay);
    p1.setBorder(new TitledBorder( 
    "Enter interest rate, year and loan amount"));
     
    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout(2));
    p2.add(this.jbtCompute);
     
    add(p1, "Center");
    add(p2, "South");
     
    this.jbtCompute.addActionListener(this);
    }
     
    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() == this.jbtCompute)
    {
    double interest =
    Double.valueOf(this.jtfInterestRate.getText()).doubleValue();
    int year =
    Integer.valueOf(this.jtfYear.getText()).intValue();
    double loanAmount =
    Double.valueOf(this.jtfLoan.getText()).doubleValue();
     
    Loan loan = new Loan(interest, year, loanAmount);
     
    this.jtfMonthlyPay.setText(String.format("%.2f", new Object[] { Double.valueOf(loan.getMonthlyPayment()) }));
    this.jtfTotalPay.setText(String.format("%.2f", new Object[] { Double.valueOf(loan.getTotalPayment()) }));
    }
    }
     
    public static void main(String[] args)
    {
    JFrame frame = new JFrame("Loan Calulator");
     
    LoanCalculator applet = new LoanCalculator();
     
    frame.add(applet, "Center");
     
    applet.init();
    applet.start();
     
    frame.setSize(350, 250);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }
    }
    There are 2 errors at Line 71 and both them are cannot find symbol.

    more detail of the error is

    Symbol: class Loan
    Location: class LoanCalculator

    I've tried adding in a NoClassDefErrorException import and that didn't work. I tried changing the name of the code and that didn't work either. I've even tried adding a public class loan in the main method but I realized then that a class can't be declared twice in the main method.

    I know that nothing in the above code indicates that I actually have a Loan class and Loan.java and LoanCalculator.java should be in the same directory. I even tried changing line 24 to public Loan() and got an error message saying that invalid method declaration; return type required. How can I fix the error to get the code to compile. I would really appreciate it if anyone can help. thanks

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: java loan calculator applet help

    do you need to define the class in the "header"

    so

    private Loan loan; ?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Calculate the remaining balance on a loan
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 9
    Last Post: 03-30-2010, 03:38 AM
  2. [SOLVED] Simple Calculator Applet Weird Error
    By sari in forum New To Java
    Replies: 5
    Last Post: 01-28-2009, 04:57 AM
  3. Formula on Maximum loan..
    By esh21 in forum NetBeans
    Replies: 4
    Last Post: 08-14-2008, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •