I am having some problems with a simple GUI program that I am writing for school. I have read the text and examples a few times, but I can't seem to figure out what I am doing wrong.
If someone can take a look at this and give me some feedback, I would appreciate it.
From the stack trace, it looks like you're trying to run your program with the appletviewer. But it's not an applet, so that's one part of the problem right there. You need to run your program from the command line using
The only reason you're getting a GUI on screen is because you're setting everything up in the constructor, which would be a no-no for an applet, anyway.
Another problem is that you carefully display the output in the TextArea "displayArea", which isn't displayed on the screen. You've got "resultsField" on the bottom, there, but it isn't used to display anything!
Otherwise, I'm stumped. Sure looks like it should work, and I can't see what could cause those stack traces. Are you sure you're showing us exactly the same code you're running?
I have made a few changes to the RationalTester class, but I have not made any changes to the Rational class. The code is posted below. I really appreciate your assistance. I am getting this stack trace:
I only get the error when I click on the "Divide" button. Nothing happens at all when I enter numbers into the text field and press enter or when I click on the other three buttons.
The tough thing about debugging, is that if there is code like qoutient.setDemon(Method call in here to get value), it is difficult to know the exact value returned. Now if the code was like
It is easy to System.out.println(denom); or System.out.println(ratValueNumerator) to make sure that they return values that you expect.
Also there is a check
This might not return the results you expect. It is better to use the equals method to check the value of Strings to one another.
You bring up a good point. If I assign 0 to numerator and 1 to denominator, then try to multiply by the reciprocal of that, then 0 ends up as the denominator.
That is another good point about using .equals to compare strings.
I do feel like I am learning this.
I am going to go to bed. It's 2:34am here and I have been going at this way too long. I'll be back on here tomorrow if I have any more questions about this program.
The way you've written that event handler now, you are required to hit Enter after making every entry in a text field. If you don't, the values won't be transferred, and you'll have zeroes instead. There's the error you're seeing right there.
Moral of the story: make sure you show people the exact code you're running, or they won't be able to help!
I'm am not sure how to fix the problem with the ActionListener. When I try to put the set methods in the else if statements, I get a StackOverflowError.
Below is my latest code. The Rational class has not changed.
out of the "if", put them into their own method, and call them from each one of the "else"s. There's no need to call it from the "if" but you could if you wanted.
But these lines don't handle the possibility that a text field contains not-a-number, so you're going to see NumberFormatExceptions all over the place. That little method would be a good place to put those handlers.
That's because getGCD() is recursive; it calls itself, and if it never stops calling itself, then the stack will overflow. You need to make sure that the exit condition will always occur -- i.e., that numer % denom always becomes zero (hint -- it clearly doesn't!)
That makes sense. Thank you for pointing that out.
I have fixed the recursive gcd method. Here is the code.
I have updated the RationalTester class again. The code is below. The program is now accepting input, except it is assigning the value of the second rational to the first rational also.
OK, still no luck. I have been through it a few times. I have made some changes. I would like to allow the user to specify the number of decimal points shown. Am I on the right path here?
I wonder why the value of rat2 is also getting assigned to rat1?
OK, I feel dumb. I copied and pasted some of the code as I was writing the program and I forgot to change the names so the JTextFields were all named "second".
I still can't get the decimal format working. It looks like everything else is working fine.
How do I use this to apply it to a sum of two fractions or something similar? I am wanting to be able to let a user input two rational numbers (two numerators and two denominator), and enter the number of decimal places they prefer to see. Then the user can add, subtract, multiply, or divide the two fractions. I have everything working except for this part. I just want to understand how this works. Sorry for all the questions.
Thanks, J
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.