aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Java Rookie Help Requested Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java Rookie Help Requested" Watch "Java Rookie Help Requested" New topic

Java Rookie Help Requested

Hunter Rose

Joined: Mar 15, 2004
Posts: 2
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
//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();
title.setBounds(5, 5, 200, 25);
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
// handler for user buttons
public void actionPerformed(ActionEvent e)
Object pickone = e.getSource();
if (pickone == resetButton)
// sets user fields to 'empty'
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");
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);

Thanx,<p>Havok<p>ONWARD TO MAYHEM!!!
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
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

Joined: Nov 26, 2002
Posts: 211
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());//

subject: Java Rookie Help Requested