• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Factorial GUI

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!












 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic