Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Rookie Help Requested

 
Hunter Rose
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Requirements: Write a Java Program using a GUI that calculates and displays the mortgage amount from user input of the amount of mortgage, the term of the mortgage, and the interest rate. Allow the user to loop back and enter new data or quit.
Problem: I am sure I have everything covered except how to get the program to calculate. Is there anyone out there who can check over my code and tell me what mistake I am making?
I am a rookie Java programmer and a rookie to this type of forum. I am seeking help so if I am asking in the wrong place, please point me to the appropriate area for this type of question.
My code follows:
//Mortgage Calculator
//Ver 2.3
//Programmer: Havok
//Email: Havok099@hotmail.com
//Date: 10 Mar 04

/*This program is a Java GUI applet that will calculate the
monthly mortgage amount for a loan. The user will input the loan amount,
term of the loan, the interest rate, and activate the program with the
'Calculate' button. A reset button has been provided to clear all user fields
in the event of a user mistake. */
// calls all Java Utilities for use
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.border.*;
public class MortCalc003 extends JApplet implements ActionListener
{
// declare all the graphic objects that will be used in the GUI
JLabel title = new JLabel("Mortgage Calculator");
JLabel mortgagePrompt = new JLabel("Mortgage:");
JLabel termPrompt = new JLabel("Term in Years:");
JLabel interestRate = new JLabel("Intrest Rate:");
JLabel monthlyPayLabel = new JLabel("Payment = ");
// declares the user fields
JTextField mortgageInput = new JTextField(15);
JTextField termInput = new JTextField(15);
JTextField interestInput = new JTextField(15);
// declares the user buttons
JButton computeButton = new JButton("Calculate");
JButton resetButton = new JButton("RESET");
int counthit = 0;
// initializes applet
public void init()
{
// place all the objects in applet
Container con = getContentPane();
con.setLayout(null);
title.setBounds(5, 5, 200, 25);
title.setForeground(Color.blue);
mortgagePrompt.setBounds(5, 30, 100, 25);
mortgageInput.setBounds(100, 30, 50, 25);
termPrompt.setBounds(5, 55, 100, 25);
termInput.setBounds(100, 55, 50, 25);
interestRate.setBounds(5, 80, 100, 25);
interestInput.setBounds(100, 80, 50, 25);
computeButton.setBounds(5, 120, 100, 25);
resetButton.setBounds(150, 120, 100, 25);
monthlyPayLabel.setBounds(5, 160, 300, 25);

// Add them to the container
con.add(title);
con.add(mortgagePrompt);
con.add(mortgageInput);
con.add(termPrompt);
con.add(termInput);
con.add(computeButton);
con.add(resetButton);
con.add(monthlyPayLabel);
con.add(interestRate);
con.add(interestInput);
computeButton.addActionListener(this);
resetButton.addActionListener(this);
}
// handler for user buttons
public void actionPerformed(ActionEvent e)
{
Object pickone = e.getSource();
if (pickone == resetButton)
{
// sets user fields to 'empty'
mortgageInput.setText(null);
interestInput.setText(null);
termInput.setText(null);
}
else if (pickone == computeButton)
{
// Calculates answer
// View Feedback
double mortgage = Double.parseDouble(mortgagePrompt.getText());
double interest = Double.parseDouble(interestRate.getText());
double term = Double.parseDouble(termPrompt.getText());
if (mortgage <= 0)
{
monthlyPayLabel.setText("Please Try Again - Re-Enter Mortgage");
}
else if ((interest <= 0) && (interest >= 100))
{
monthlyPayLabel.setText("Please Try Again - Re-Enter Interest");
}
else if (term <= 0)
{
monthlyPayLabel.setText("Please Try Again - Re-Enter Term");
}
else
{
term = term * 12;
interest = interest / 1200;
// mortgage calulation formula (Thanks Hack!)
double One = interest * Math.pow((1 + interest), term);
double Two = (Math.pow((1 + interest), term) - 1);
double Three = (mortgage * (One / Two));
NumberFormat four = NumberFormat.getCurrencyInstance();
String answer = four.format(Three);
monthlyPayLabel.setText("Payment Amount = $" + answer);
}
}
}
}//EOF
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you were so very, very nearly right.
I'll tell you want, I'll pick three lines of code, you spot the mistake, then go and correct it okay?

And repeat for interest and rate.

To quote the sheriffs from these parts:
When posting code, please be sure to surround the code with the code UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
Welcome to the ranch.
[ March 16, 2004: Message edited by: Carlos Failde ]
 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is called Data Redundancy(Mistake Redundancy)
heck out all these lines..
JLabel mortgagePrompt = new JLabel("Mortgage:"); //okay
JLabel termPrompt = new JLabel("Term in Years:");//okay
JLabel interestRate = new JLabel("Intrest Rate:");//okay
JTextField mortgageInput = new JTextField(15); //okay
JTextField termInput = new JTextField(15); //okay
JTextField interestInput = new JTextField(15); //okay
Check Out below with Your BIG EYES
double mortgage = Double.parseDouble(mortgagePrompt.getText());//
double interest = Double.parseDouble(interestRate.getText());//
double term = Double.parseDouble(termPrompt.getText());//
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic