Results 1 to 10 of 10
Thread: problem with java assignment
- 03-10-2011, 02:16 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
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
- 03-10-2011, 03:57 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Use code tags.
[ code]
Your code
[/code]
Remove the space in [ code] also.
- 03-10-2011, 10:10 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
java problem
Do you mean in the post itself, or in my code? Thanks.
- 03-10-2011, 10:19 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
In the post so I can read your code more easily.
- 03-10-2011, 10:29 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
hava help
I apologize for my ignorance, but are you just needing an indication of where the code starts and ends in the post?
- 03-10-2011, 10:29 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@OP, you can edit the post and insert code tags. Don't post it again.
- 03-10-2011, 10:32 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-10-2011, 10:39 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
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.
- 03-10-2011, 10:45 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 03-10-2011, 11:00 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Problem with my assignment - in need of help!
By Chopaan in forum New To JavaReplies: 1Last Post: 01-17-2011, 02:19 AM -
While Loop Problem with Java Assignment Program
By welsh_rocker in forum New To JavaReplies: 9Last Post: 01-12-2011, 01:55 PM -
assignment problem with List<T>
By Newbie666 in forum New To JavaReplies: 11Last Post: 01-21-2010, 12:12 PM -
The Assignment Problem
By bumblyb33 in forum New To JavaReplies: 5Last Post: 03-04-2009, 04:21 AM -
assignment problem help needed
By tiggz1980 in forum New To JavaReplies: 2Last Post: 02-06-2008, 11:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks