• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Factorial GUI

 
Jackson Andrew
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!












 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fred Kleinschmidt
Bartender
Posts: 451
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
Campbell Ritchie
Marshal
Posts: 52507
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. . . .
Agree 100%.
Incidentally, why do you include one in your factorial computation loop? x*1 is always x. . . .
Maybe to use this definition of factorials: 0! = 1?

[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: 451
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


there is no need oor I>=1 here; I>1 will suffice, even for factorial=0;
 
Campbell Ritchie
Marshal
Posts: 52507
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!