Results 1 to 10 of 10
  1. #1
    jaire is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Unhappy problem with java assignment

    I am working on a mortgage calculator for my java class. I have the payment calculation correct, but I cannot figure out why the balance figure is working. I had this working at the system level without a gui and it showed the interest and balance after each payment. Now it only shows the last one and it doesn't look like it is a repeating statement. Plus the scroll box at the bottom is not scrolling the right way. Please help if you can.

    Java Code:
    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;
    
    public class jTaylor3 extends JFrame implements ActionListener {
    
    	// Define labels, fields and buttons	
    	JPanel mortgageAmountPanel = new JPanel( new GridLayout( 1, 5 ) );
    	JLabel mortgageAmountLabel = new JLabel( "Mortgage Amount", JLabel.LEFT );
    	JTextField mortgageAmountField = new JTextField( 10 );
    		
    	JPanel termYearsPanel = new JPanel( new GridLayout( 1, 5 ) );
    	JLabel termYearsLabel = new JLabel( "Terms in Years", JLabel.LEFT );
    	JTextField termYearsField = new JTextField( 10 );
    		
    	JPanel interestRatePanel = new JPanel( new GridLayout( 1, 5 ) );
    	JLabel interestRateLabel = new JLabel( "Interest Rate", JLabel.LEFT );
    	JTextField interestRateField = new JTextField( 10 );
    		
    	JPanel monthPaymentPanel = new JPanel( new GridLayout( 1, 5 ) );
    	JLabel monthPaymentLabel = new JLabel( "Monthly Payment $:", JLabel.LEFT );
    	JTextField monthPaymentField = new JTextField( 10 );
    	
    	JPanel payDetailPanel = new JPanel( new GridLayout( 15, 15 ) );
    	JLabel payDetailLabel = new JLabel( "Payment   Interest Paid       Balance", JLabel.LEFT );
    	JTextField payDetailField = new JTextField( 100 );
    	
    	JPanel button = new JPanel( new GridLayout( 3,3 ) );
    	
    	
    
    
    	// Create buttons
    	JButton calcButton1 = new JButton( "Calculate 7 years at 5.35%" );
    	JButton calcButton2 = new JButton( "Calculate 15 years at 5.5%" );
    	JButton calcButton3 = new JButton( "Calculate 30 years at 5.75%" );
    
    	// Create area for user input
    	JTextArea displayArea = new JTextArea( 10, 25 );
    
    	// Declare variables and text formatting
    	double rate = 0;
    	double monthPayment = 0;
    	double mortgageAmount = 200000;
    	int termYears = 30;
    	double interestRate = 3.75;
    	DecimalFormat valueFormat = new DecimalFormat( "#,###.00" );
    
    public jTaylor3() {
    	super( "MORTGAGE CALCULATOR 1.0" );
    	setSize( 300, 380 );// sets size of the constructor
    	setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	
    	// Add content to panels
    	Container pane = getContentPane();// return value of content
    	pane.add( mortgageAmountPanel );
    		mortgageAmountPanel.add( mortgageAmountLabel );
    		mortgageAmountPanel.add( mortgageAmountField );
    		mortgageAmountPanel.setMaximumSize( new Dimension( 250, 25 ) );
    		
    	button.add( calcButton1);
    	pane.add( button );
    		setVisible( true );
    		setContentPane( pane );
    		button.setMaximumSize( new Dimension( 250, button.getMinimumSize().height ) );
    		calcButton1.addActionListener( this );
    	
    	button.add( calcButton2);
    	pane.add( button );
    		pane.setLayout( new BoxLayout( pane, BoxLayout.Y_AXIS ) );
    		setVisible( true );
    		setContentPane( pane );
    		button.setMaximumSize( new Dimension( 250, button.getMinimumSize().height ) );
    		calcButton2.addActionListener( this );
    	
    	button.add( calcButton3);
    	pane.add( button );
    		pane.setLayout( new BoxLayout( pane, BoxLayout.Y_AXIS ) );
    		setVisible( true );
    		setContentPane( pane );
    		button.setMaximumSize( new Dimension( 250, button.getMinimumSize().height ) );
    		calcButton3.addActionListener( this );
    	
    	pane.add( monthPaymentPanel );
    		monthPaymentPanel.add( monthPaymentLabel );
    		monthPaymentPanel.add( monthPaymentField );
    		monthPaymentPanel.setMaximumSize( new Dimension( 250, monthPaymentPanel.getMinimumSize().height ) );
    	
    	pane.add( payDetailPanel );
    		payDetailPanel.add( payDetailLabel );
    		payDetailPanel.setMaximumSize( new Dimension( 250, payDetailPanel.getMinimumSize().height ) );
    
    
    		JScrollPane scrollPane = new JScrollPane(payDetailField);
    	add(scrollPane, BorderLayout.CENTER);	
    
    	
    }
    
    // Define action event
    public void actionPerformed( ActionEvent event ) {
    	Object source = event.getSource();
    	int counter = 0;
    	
       if( source == calcButton1 ) {
       	String mA = mortgageAmountField.getText();
       	double mortAmt = Double.valueOf(mA);
       	double monthInterest1 = ((5.35 / 12) / 100);
       	double numPeriods1 = 7*12;       
       	double monthPmt1 = mortAmt * ( monthInterest1 /(1 - Math.pow(1 + monthInterest1, -numPeriods1)));
    
    	monthPaymentField.setText(valueFormat.format(monthPmt1));
    	
    	//while statement to calculate monthly interest and balance on first button
    	while (counter <= 2) {
       	   String mA2 = mortgageAmountField.getText();
       	   double mortAmt2 = Double.valueOf(mA2);
       	   double monthPmt2 = mortAmt2 * ( monthInterest1 /(1 - Math.pow(1 + monthInterest1, -numPeriods1)));
       	   double Balance0 = mortAmt2 * ((1 + monthInterest1) - monthPmt2); //Balance calculation
               double Interest0 = mortAmt2 * monthInterest1; //Interest calculation
               
               payDetailField.setText(" " + counter + "               "  + (valueFormat.format(Interest0))+ "               " + (valueFormat.format(Balance0)));
                    
               mortAmt2 = Balance0; //Balance of previous month
               counter++; //Increment counter after each payment
    
    		}//End of while statement
       }//End of calcButton1 if statement
       
       if( source == calcButton2 ) {
       	   String mA = mortgageAmountField.getText();
       	   double mortAmt = Double.valueOf(mA);
       	   double monthInterest1 = ((5.5 / 12) / 100);
       	   double numPeriods1 = 15*12;       
       	   double monthPmt1 = mortAmt * ( monthInterest1 /(1 - Math.pow(1 + monthInterest1, -numPeriods1)));
    
       	   monthPaymentField.setText(valueFormat.format(monthPmt1));
       
       }//End of calcButton2 if statement
       
       if( source == calcButton3) {
       	   String mA = mortgageAmountField.getText();
       	   double mortAmt = Double.valueOf(mA);
       	   double monthInterest1 = ((5.75 / 12) / 100);
       	   double numPeriods1 = 30*12;       
       	   double monthPmt1 = mortAmt * ( monthInterest1 /(1 - Math.pow(1 + monthInterest1, -numPeriods1)));
    
       	   monthPaymentField.setText(valueFormat.format(monthPmt1));
      
       }//End of calcButton3 if statement
    }//End of Action event
    
    public static void main( String[] arguments ) {
    	jTaylor3 mtg = new jTaylor3();
    	
    }//End of main class
    
    // Declaration of user input for each variable
    public void validateUserInput( JTextField mortgageAmountField, JTextField interestRateField, JTextField termYearsField ) {
    
    	}//End of input validation
    }
    Last edited by jaire; 03-10-2011 at 10:37 AM. Reason: code tags added

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Use code tags.

    [ code]
    Your code
    [/code]

    Remove the space in [ code] also.

  3. #3
    jaire is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default java problem

    Do you mean in the post itself, or in my code? Thanks.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    In the post so I can read your code more easily.

  5. #5
    jaire is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default hava help

    I apologize for my ignorance, but are you just needing an indication of where the code starts and ends in the post?

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by jaire View Post
    I apologize for my ignorance, but are you just needing an indication of where the code starts and ends in the post?
    I've done it for you this time only. But please pay your attention next time. It may off topic to the original if I explain how to use code tags in details right now. So please have a look my forum signature, you can find the relevant FAQ section to do that. Please read it before posting a code segment again.

  8. #8
    jaire is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default code tags

    The post is edited with the code tags. I see what you're saying now. I'm new to this forum as well.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Never mind.

    So what's your question here. If you could explain in a simple way we can help you more. Because your code is too long and hard to read. Could be specific, please?

  10. #10
    jaire is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    How do I get the jscroll pane the same size as the other fields? Does it need to go in a different spot in the code or is there a way to adjust it? Also, I am trying to get a list of interest paid and balance for each payment, but I just get one calculation.

Similar Threads

  1. Problem with my assignment - in need of help!
    By Chopaan in forum New To Java
    Replies: 1
    Last Post: 01-17-2011, 02:19 AM
  2. While Loop Problem with Java Assignment Program
    By welsh_rocker in forum New To Java
    Replies: 9
    Last Post: 01-12-2011, 01:55 PM
  3. assignment problem with List<T>
    By Newbie666 in forum New To Java
    Replies: 11
    Last Post: 01-21-2010, 12:12 PM
  4. The Assignment Problem
    By bumblyb33 in forum New To Java
    Replies: 5
    Last Post: 03-04-2009, 04:21 AM
  5. assignment problem help needed
    By tiggz1980 in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 11:14 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
  •