Need some guidance with Java assignment


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Need some guidance with Java assignment

Threaded View

  1. #1
    Join Date
    Nov 2010
    Posts
    1

    Need some guidance with Java assignment

    I'm a complete newbie, not just to Java, but programming in general so please have mercy. I have gotten this far but I am having trouble with other things. The assignment is to create a mortgage calculator with a GUI that gets loan amount, interest rate and the term of the loan in years from the user. The user presses the Calculate button and it should output the monthly mortgage payment that one would pay based on the loan amount, interest rate and number of years entered. For example if I borrowed $12 for 1 year at an interest rate of 0% then it should output 1.00 to pay back $1 per month for a year. The output is all kinds of messed up, as you can see from this screenshot. I am pretty sure I have the calculation correct. Also, I am supposed loop back so the user can enter new data or quit. I am having a lot of trouble figuring out where to start with that. Any guidance is appreciated, thank you for any help!
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /*	Student Name: Jenna Garcia
    	Class: PRG421 - Week 2 SR-mf-003
    	Description: Write the program in Java (with a graphical user 
    	interface) and have it calculate and display the mortgage 
    	payment amount from user input of the amount of the mortgage,
    	the term of the mortgage, and the interest rate of the
    	mortgage. Allow the user to loop back and enter new data or
    	quit. Please insert comments in the program to document the
    	program.
    	Date: 11/28/10
     */
    
    public class Calculator extends JFrame implements ActionListener {
    
    	private JPanel panelAdder;
    	private JLabel labela;
    	private JLabel labelt;
    	private JLabel labelr;
    	private JTextField textFieldAmount;
    	private JTextField textFieldTerm;
    	private JTextField textFieldRate;
    	private JTextField textFieldResult;
    	private JButton buttonCalc;
    
    	public Calculator() {
    
      		initComponents();
      		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      		setVisible(true);
      		pack();
      		// Add Listeners
      		buttonCalc.addActionListener(this);
    	}
    
    	public void initComponents() {
    
     		//Initialize Components
     		panelAdder = new JPanel();
     		labela = new JLabel("Amount");
     		textFieldAmount = new JTextField();
     		labelt = new JLabel("Term");
     		textFieldTerm = new JTextField();
     		labelr = new JLabel("Rate");
     		textFieldRate = new JTextField();
     		textFieldResult = new JTextField();
     		buttonCalc = new JButton("Calculate");
    
     		//Set Object Attributes
     		textFieldResult.setEditable(false);
     		textFieldResult.setColumns(10);
     		textFieldAmount.setColumns(10);
     		textFieldTerm.setColumns(5);
     		textFieldRate.setColumns(5);
    
     		Container contentPane = getContentPane();
     		contentPane.setLayout(new FlowLayout());
    
     		//Lets add the components to the panel
     		panelAdder.add(labela);
     		panelAdder.add(textFieldAmount);
     		panelAdder.add(labelt);
     		panelAdder.add(textFieldTerm);
     		panelAdder.add(labelr);
     		panelAdder.add(textFieldRate);
     		panelAdder.add(buttonCalc);
     		panelAdder.add(textFieldResult);
    
     		contentPane.add(panelAdder);
    	}
    
    public static void main(String[] args) {
    
    	Calculator frame = new Calculator();
    	}
    
    private void setResultValue() {
    
    	double amount = Double.parseDouble(textFieldAmount.getText());
    	double term = Double.parseDouble(textFieldTerm.getText());
    	double rate = Double.parseDouble(textFieldRate.getText()) / 100;
    	double result = amount * (rate * Math.pow ((1 + rate), term))/(Math.pow((1 + rate), term) - 1);
    
    	textFieldResult.setText(Double.toString(result));
    	}
    
    public void actionPerformed(ActionEvent event) {
    
    	if (event.getSource() == buttonCalc) {
    	setResultValue();
    	}
    	}
    }
    Attached Images Attached Images

Similar Threads

  1. Java Database and Java Report for Assignment
    By Peter_APIIT in forum Java
    Replies: 1
    Last Post: 05-26-2008, 09:05 AM
  2. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 06:58 AM
  3. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center