Java Factorial GUI
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The goal is to create a program which utilizes a GUI to calculate a factorial. I need to create a jPanel or jFrame to display the results of the program. Then I need to get the text from JTextField and parse it to an Integer so it can be passed to a constructor.
Thirdly, an object class needs to be created with an instance variable of type int called factorial. This object class needs a single argument constructor named Factorial that accepts an int and assigns the value to the instance variable. Then it should have a method that calculates the factorial using a for loop with a return type of int. Finally, it should display the value of the object after calling the method that computes the factorial. Here is my work so far.
And my next file is the Object class:
My code compiles and runs, but when the frame shows up, the panel that is displayed is empty. Have I called the computeFactorial() method incorrectly? And is my usage of the parameter variable in the constructor incorrect?
Thank you! Any help is appreciated!
Thirdly, an object class needs to be created with an instance variable of type int called factorial. This object class needs a single argument constructor named Factorial that accepts an int and assigns the value to the instance variable. Then it should have a method that calculates the factorial using a for loop with a return type of int. Finally, it should display the value of the object after calling the method that computes the factorial. Here is my work so far.
And my next file is the Object class:
My code compiles and runs, but when the frame shows up, the panel that is displayed is empty. Have I called the computeFactorial() method incorrectly? And is my usage of the parameter variable in the constructor incorrect?
Thank you! Any help is appreciated!
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Your Factorial Class has the wrong name on its constructor, it has to be Factorial, the same as the class name, to be recognized as the constructor.
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Your Factorial class should have nothing to do with the GUI - its sole purpose should be to compute the factorial of a number.
You should follow the instructions and create a Factorial class with an instance variable named factorial:
Incidentally, why do you include one in your factorial computation loop? x*1 is always x.
You might consider using a long instead of an it.
Then you create a GUI driver that extends JPanel.
You should follow the instructions and create a Factorial class with an instance variable named factorial:
Incidentally, why do you include one in your factorial computation loop? x*1 is always x.
You might consider using a long instead of an it.
Then you create a GUI driver that extends JPanel.
posted 9 years ago
[edit]Now I have looked at the code, that can't be the explanation. You realise that you will get overflow with an int for 13! and long overflow for 22L!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Agree 100%.Fred Kleinschmidt wrote:Your Factorial class should have nothing to do with the GUI - its sole purpose should be to compute the factorial of a number. . . .
Maybe to use this definition of factorials: 0! = 1?Incidentally, why do you include one in your factorial computation loop? x*1 is always x. . . .
[edit]Now I have looked at the code, that can't be the explanation. You realise that you will get overflow with an int for 13! and long overflow for 22L!
Fred Kleinschmidt
Bartender
Posts: 732
10
posted 9 years ago
there is no need oor I>=1 here; I>1 will suffice, even for factorial=0;
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
there is no need oor I>=1 here; I>1 will suffice, even for factorial=0;
Campbell Ritchie
Marshal
Posts: 81618
593
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Agree. But i am not used to seeing factorials calculated like that; I am used to seeing the recursive version:-Yes, I know l is a bad parameter name.
| Every plan is a little cooler if you have a blimp. And a tiny ad. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










