Can you help with Mortgage Calculator in Java?


One Response to “Can you help with Mortgage Calculator in Java?”

  1. Penumbra says:

    There was a lot wrong with this. You had duplicate main methods, you tried to import while making a variable declaration (which would be a handy feature I suppose, but is invalid in Java), and forgot to extend the JFrame class and implement ActionListener. I fixed these and some other issues so that it would compile. Getting the logic to work is up to you though.

    /** @author alesiab
    */

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;

    //Declaration of class
    public class MortCalc2 extends JFrame implements ActionListener {

    // Declaration of labels
    JPanel row1 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel principal_label1 = new JLabel( “Net Amount $ :”, JLabel.LEFT );// Sets label text and alignment
    JTextField principal_txt = new JTextField( 10 );
    JPanel row2 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel term_label2 = new JLabel( “Term Yrs :”, JLabel.LEFT );
    JTextField term_txt = new JTextField( 10 );
    JPanel row3 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel rate_label3 = new JLabel( “Interest Rate %:”, JLabel.LEFT );
    JTextField rate_txt = new JTextField( 10 );
    JPanel row4 = new JPanel( new GridLayout( 1, 3 ) );
    JLabel pay_label4 = new JLabel( “Monthly Payment $:”, JLabel.LEFT );
    JTextField pay_txt = new JTextField( 10 );
    JPanel button = new JPanel( new FlowLayout( FlowLayout.CENTER ) );

    // Declaration of buttons and text shown on them
    JButton rstButton = new JButton( “Reset” );
    JButton extButton = new JButton( “Exit” );
    JButton calcButton = new JButton( “Calculate” );

    // Declaration for area to display user input
    JTextArea displayArea = new JTextArea( 10, 45 );

    // Declaration of number formats that will be output after calculation
    DecimalFormat twodigits = new DecimalFormat( “#,###.00″ );

    // Declaration of variables
    double rate = 0;// Sets rate as a double set to zero
    double mpay = 0;// Sets monthly payment as a double set to zero
    double principal = 0;// Sets principal as a double set to zero
    int term = 0;// Sets interest as a integer set to zero
    double interest = 0;// Sets interest as a double set to zero

    // Declaration of constructor
    public MortCalc2() {

    super( “Mortgage Calculator: SR-mf-003 #4″ );// Syntax used for calling constructor
    setSize( 400, 200 );// sets size of the constructor
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    Container pane = getContentPane();// returns the value for content
    Border bdr = new EmptyBorder( 2, 5, 2, 5 );// provides borders for label

    // Declaration of windows that will hold users input
    pane.add( row1 );
    row1.add( principal_label1 );
    row1.add( principal_txt );
    row1.setMaximumSize( new Dimension( 300, 25 ) );
    row1.setBorder( bdr );

    pane.add( row2 );
    row2.add( term_label2 );
    row2.add( term_txt );
    row2.setMaximumSize( new Dimension( 300, row2.getMinimumSize().height ) );
    row2.setBorder( bdr );

    pane.add( row3 );
    row3.add( rate_label3 );
    row3.add( rate_txt );
    row3.setMaximumSize( new Dimension( 300, row3.getMinimumSize().height ) );
    row3.setBorder( bdr );

    pane.add( row4 );
    row4.add( pay_label4 );
    row4.add( pay_txt );
    row4.setMaximumSize( new Dimension( 300, row4.getMinimumSize().height ) );
    row4.setBorder( bdr );

    button.add( calcButton );
    button.add( rstButton );
    button.add( extButton );
    pane.add( button );

    pane.setLayout( new BoxLayout( pane, BoxLayout.Y_AXIS ) );
    setVisible( true );
    setContentPane( pane );

    button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height ) );

    // Adds specified action listener to get action events from buttons
    rstButton.addActionListener( this );
    extButton.addActionListener( this );
    calcButton.addActionListener( this );
    }

    // Declaration of calculation actions to take place

    public void actionPerformed( ActionEvent event ) {
    Object source = event.getSource();
    // Begin “if” function
    if( source == calcButton ) // calculates on hit
    {
    validateUserInput( principal_txt, rate_txt, term_txt ); // validates input
    // formula for calculation output
    mpay = principal * ( rate / ( 12 * 100 ) ) / ( 1 – Math.pow( 1 + ( rate / ( 12 * 100 ) ), -1 * ( term * 12 ) ) );
    pay_txt.setText( “” + twodigits.format( mpay ) );
    }

    if( source == rstButton ) // Clears on hit
    {
    term_txt.setText( ” ” );
    rate_txt.setText( ” ” );
    principal_txt.setText( ” ” );
    pay_txt.setText( ” ” );
    displayArea.setText( ” ” );
    }
    if( source == extButton ) // Exits on hit
    {
    System.exit( 0 );
    }
    }// end action

    // Declaration of the main method
    public static void main( String[] arguments ) {
    MortCalc2 mtg = new MortCalc2();
    }

    // Declaration of user input for each variable
    public void validateUserInput( JTextField principal_txt, JTextField rate_txt, JTextField term_txt ) {
    // Begin “try/catch” function
    try {
    principal = Double.parseDouble( principal_txt.getText() );
    } catch( NumberFormatException e )// Action listener
    {
    }
    }
    }

Leave a Reply

*


Celebrity Sex Tapes | Kim Kardashian Sex Tape