Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Greenhorn porgrammer needs help

 
Patty Edens
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a mortgage calculator program for school. I cannot get the Event to work properly and exhausted myself trying to figure out what I'm doing wrong - which is probably something really stupid - cause I'm feeling really stupid right now. My code is below - can someone give me an idea of what (probably everything) I'm doing wrong. I was referred to a program in "Sams, Teach Yourself Java 2 in 24 hours that is pulling a Thread - I don't think I need a thread but now don't know how to get rid of it. My double.parseDouble is not working either. Any help would be appreciated.

The code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MortgageEvent extends JFrame implements ActionListener {

Pattys_Mortgage gui;
Thread calculating;

public MortgageEvent(Pattys_Mortgage in) {
gui = in;
}

void startCalculating() {
calculating = new Thread(this);
calculating.start();
gui.calculate.setEnabled(false);
gui.quit.setEnabled(true);
gui.reset.setEnabled(false);
}

void stopCalculating() {
gui.quit.setEnabled(false);
gui.calculate.setEnabled(true);
gui.reset.setEnabled(true);
calculating = null;
}

void clearAllFields() {
gui.mortgage.setText(null);
gui.term.setText(null);
gui.interest.setText(null);
}

public void actionPerformed(ActionEvent calculate) {
String command = calculate.getActionCommand();
if(command == "Calculate")
startCalculating();
if(command == "Quit")
stopCalculating();
if(command == "Reset")
clearAllFields();
//declare variables
double amount = Double.parseDouble(mortgage.getText());
double term = Double.parseDouble(term.getText());
double rate = Double.parseDouble(rate.getText());
double results = Double.parseDouble(results.getText());
double noOfPayments = Double.parseDouble(noOfPayments());
}

public static double results(double amount, double noOfPayments,
double rate) {
double payment = ((amount*rate)/12/
(1-(Math.pow((1+(rate/12)),(-1*noOfPayments)))));
return payment;
//double.parseDouble(results.getText());
}

static public double parseDouble(results.getText())
{
return doubleValueOf(results);
}
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(command == "Calculate")

should be
if(command.equals("Calculate"))

same for the others
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are these ever used?

 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you need to start by getting syntactically correct Java. I'll focus on one area. You have code:


static public double parseDouble(results.getText())
{
    return doubleValueOf(results);
}


Now in other areas you've declared methods correctly, so I assume you're OK with that stuff. So, can you see a problem with the bit in bold above? When you're declaring a method, you need to tell it the type of the parameters. You need to change the part in bold, and then make a corresponding change in the method body.

OK, so regarding the bit in italics, where is this function "doubleValueOf" defined? I think you're intending to call the function 'valueOf' on class 'Double', in which case you want something like "Double.valueOf(myStringVariable)". But this returns a Double object, not a double basic type (yup, 'Double' and 'double' are different), so you probably really want "Double.valueOf(myStringVariable).doubleValue()".

These explanations are brief, but hopefully they're enough to get you going - otherwise write back



--Tim


(Edited to use [ quote ] rather than [ code ], so the bold and italic stuff shows up. Has the UBB code changed? Stuff looks different in FireFox!)
[ May 20, 2004: Message edited by: Tim West ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[OFF-TOPIC]

(Edited to use [ quote ] rather than [ code ], so the bold and italic stuff shows up. Has the UBB code changed? Stuff looks different in FireFox!)

Many months ago, we changed the code tag to not use a reduced size font tag. That's all that I'm aware of.

[/OFF-TOPIC]

Welcome to JavaRanch, Patty!

What does, "I cannot get the Event to work properly," mean? I'd guess that Michael's suggestion above will clear this issue, but it's not clear from your question.

Also, you might want to simply remove that whole static public double parseDouble(results.getText()) section of code.
[ May 27, 2004: Message edited by: Dirk Schreckmann ]
 
Patty Edens
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion - I can't get the MortgageEvent program to compile. I tired the recommended changes but the program is still not working.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the specific compiler error? if you're getting more than one, just try and fix the first one - often this will fix others as well.
 
Patty Edens
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I got it to work Thanks everyone for the help!! I had an abstract error and then got a exception thrown - main message. I had the main class coded incorrectely.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic